fre4k
fre4k

Reputation: 15

Insert a RTF/DOC/DOCX file to a bookmarked place in a Word file

I use Excel VBA code in a userform to open a Word file and insert information from textboxes into the Word file.

I would like to also insert text files with the same macro in a specific place in the Word file. I would like to prepare the text files as separate RTF or DOC(X) files.

Is there a way to import text files to a bookmarked place in a Word file with Excel VBA code?

'Word Kommandos

Dim wordApp As Object
Dim wordDoc As Object
Dim hwnd As Long
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0
'wordApp.documents.Open VorlagePfad
wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad, ReadOnly:=True)

With wordDoc
    'Empfänger Adressfeld
    .Bookmarks("Zeile1").Range.Text = Erstellen.TextBox1.Value
    .Bookmarks("Zeile2").Range.Text = Erstellen.TextBox5.Value
End With

Upvotes: 0

Views: 316

Answers (1)

egghead1313
egghead1313

Reputation: 26

Try to use this:

pathToFile = "c:\temp\doc2.docx"
With wordDoc
   .Bookmarks("newFileHere").Range.InsertFile pathToFile
end with

Upvotes: 1

Related Questions