0
votes

I'm trying to fill a Word document with plain text files in VBA. Here is what I have :

  1. The Word document has bookmarks with default text inside (this helps me to be sure all bookmarks are replaced)
  2. The following VBA does the job of inserting what is inside the txt file in the bookmark
strt = bookmark.Range.Start
bookmark.Select
Selection.InsertFile Filename:=Filename, ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.Bookmarks.Add Name:=bookmarkname, Range:=ActiveDocument.Range(strt, Selection.Range.End)

The main issue is that after I run the VBA, the font name, font size, bullet points (if any)... are changed to something different (Courier New 10,5pts). I recreate the bookmark to be able to run the macro several times if the text files are modified.

I found an ugly (imho) solution:

  1. Saving the Style Name and applying it after the insertfile. For the bullet points, applying the style does not set the correct font and size hence the 2 last lines
strt = bookmark.Range.Start
bookmark.Select
myStyle = (Selection.Style)
Selection.InsertFile Filename:=Filename, ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.Bookmarks.Add Name:=bookmarkname, Range:=ActiveDocument.Range(strt, Selection.Range.End)
ActiveDocument.Range(strt, Selection.Range.End).Style = myStyle
ActiveDocument.Range(strt, Selection.Range.End).Font.Name = "Calibri"
ActiveDocument.Range(strt, Selection.Range.End).Font.Size = 11

Do you have any idea to do something more "professional"?

The other issue is that the insertfile always adds a new line at the end even if there is no new line at the end of the file. Any idea to prevent it or at least to remove it after insertion?

Thanks for any idea to help me!

2

2 Answers

0
votes

Plain text files don't have a style. They will take on whatever the style is applied to the selection where you are inserting the file. Create a style that looks like what you want the final text to look like, apply that to the receiving document, then re-run your code.

Try to get out of the habit of programming the selection object. It's slow and unreliable. Ranges are better than selections. Here is Microsoft's introduction to Ranges: Working with Range Objects

Instead of using InsertFile, you can use Range.Text to insert the text:

Sub Text2Doc()
    Dim iFreeFileNum As Integer
    Dim strPath As String
    Dim strFileContent As String

    strPath = "C:\Test.txt"
    'Get the next file number available for use by the FileOpen function
    iFreeFileNum = FreeFile
    Open strPath For Input As iFreeFileNum
    strFileContent = Input(LOF(iFreeFileNum), iFreeFileNum)
    ActiveDocument.Bookmarks("BookmarkName").Range.Text = strFileContent
    Close iFreeFileNum
End Sub

A trailing paragraph mark will not get added.

0
votes

For my future self or anyone with the same UTF-8 issue, here is my solution in VBA. It's a merge between accepted answer from @john-korchok and some code found here: Read & Write UTF8 file in VBA

Dim bookmark As bookmark
Dim Filename As String
Dim fs
Dim bookmarkname As String
Dim rngBookmark As Word.Range
Dim objStream

Set objStream = CreateObject("ADODB.Stream")
Set fs = CreateObject("Scripting.FileSystemObject")

For Each bookmark In ActiveDocument.Bookmarks
    If bookmark.Name <> bookmarkname Then ' Avoids infinity loop because of Selection.Bookmarks.Add
        bookmarkname = bookmark.Name
        Filename = Path + bookmarkname + ".txt"
        If fs.FileExists(Filename) Then
            ' Records the bookmarks position to recreate it after
            Set rngBookmark = ActiveDocument.Bookmarks(bookmarkname).Range
            objStream.Charset = "utf-8"
            objStream.Open
            objStream.LoadFromFile (Filename)
            rngBookmark.Text = objStream.ReadText()
            ' Recreation of the bookmark
            ActiveDocument.Bookmarks.Add bookmarkname, rngBookmark
            objStream.Close
        End If
    End If
Next