0
votes

Excel provides me a customized text in cell A1, which then should be passed on to a word letter at a specific section (after a bookmark).

Just started with VBA and found+edited a code that was able to provide half of the solution. Where I struggle is how to insert the text in cell A1 in sheet OutputText to the word document after the bookmark?

Here is my code so far:

Function FnBookMarkInsertAfter()
       Dim objWord
       Dim objDoc
       Dim objRange
         Set objWord = CreateObject("Word.Application")
         Set objDoc = objWord.Documents.Open("C:\Users\[...]")
         objWord.Visible = True
         Set objRange = objDoc.Bookmarks("bookmark_1").Range
         objRange.InsertAfter ("Cell A1 from Sheet OutputText")
    End Function

Thank you!

1

1 Answers

0
votes

Would this work for you? (I can't take credit for the code):

Sub test()

Dim objWord As Object
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets(1)
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\test.docx" ' change as required
With objWord.ActiveDocument
    .Bookmarks("bookmark_1").Range.Text = ws.Range("A1").Value
End With
Set objWord = Nothing

End Sub