简体   繁体   中英

Insert Excel cell text in Word after bookmark via VBA

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM