This code sample uses Microsoft Word to read files into the Word document using a vba macro.
The import file is opened as TextStream and read in line by line.
This vba code has the advantage that you can filter or customize special text lines or areas during import.
Subject: Import also applies to Excel, Powerpoint, Access
File format should be text-based.
This test actually reads any files with different formats.
However, the shown reading results are only useful in text files, txt, csv and similar.
Option Explicit On
Sub Read_PDF_File() '------------------< Read_PDF_File() >------------------ '<# Late-binding > 'Dim fso 'Set fso = CreateObject("Scripting.FileSystemObject") '</# Late-binding >
'< Reference Binding > Dim fs As FileSystemObject Set fs = New FileSystemObject '</ Reference Binding >
Dim sFilename As String sFilename = "C:\_Daten\Desktop\Demo\Word\2018-10-08_PDF_einlesen\Import_File_as_Text.txt" 'Import_File_as_PDF.pdf
Dim file_to_Read As TextStream Set file_to_Read = fs.OpenTextFile(sFilename, 1) '1:open as readonly
Dim sText_Line As String Dim iLine As Integer iLine = 0
'< output > Selection.TypeText "Filename=" & sFilename Selection.TypeParagraph '</ output >
'----< @Loop: Read Line-by-Line >---- Do While file_to_Read.AtEndOfStream <> True '--< Read Line >-- iLine = iLine + 1
'< read > sText_Line = file_to_Read.ReadLine() '</ read >
'< add in word > Selection.TypeText sText_Line Selection.TypeParagraph
'Selection.InsertAfter vbCrLf 'Selection.InsertAfter sText_Line
'If sText_Line Like "*ine*" Then ' MsgBox "Line" ' Selection.InsertAfter sText_Line 'End If '</ add in word >
'--</ Read Line >-- Loop '----</ @Loop: Read Line-by-Line >----
'< End > file_to_Read.Close Set file_to_Read = Nothing MsgBox "Done Lines=" & iLine '</ End > '------------------</ Read_PDF_File() >------------------ End Sub
|
Results at
Text files Import_File_as_Text.txt
ODT: Open Document ODT read in directly
RTF: RTF document read directly line-based