I'm trying to fill a Word document with plain text files in VBA. Here is what I have :
- The Word document has bookmarks with default text inside (this helps me to be sure all bookmarks are replaced)
- 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:
- 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!