Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

Download:

Datei 1: Import_File_as_Text.txt
Datei 2: Word_PDF_einlesen.docm

Word: read text files with vba macro

 

 

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

Mobile
»
Word: read text files with vba macro
»
Insert the temporary storage as an image with a width of 19 centimeters
»
MS Word: Paste Paste paste event into Word
»
Word macro: Select all the records and delete markers
»
Word macro: Inserting photos with file selection in a list
»
Word Macro: Set all Images to left alignment with a surrounding margin
»
Word macro: Automatic correction of wrong translation links
»
Word macro: delete all buttons and ActiveX controls
»
Word: Add a button with a macro
»
Word macro: Inserting new lines after a bookmark

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de