简体   繁体   English

通过VBA在书签后的Word中插入Excel单元格文本

[英]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). Excel在单元格A1中为我提供了自定义文本,然后应将其传递到特定部分的单词字母(在书签之后)。

Just started with VBA and found+edited a code that was able to provide half of the solution. 刚从VBA开始,发现并编辑了能够提供一半解决方案的代码。 Where I struggle is how to insert the text in cell A1 in sheet OutputText to the word document after the bookmark? 我在哪里挣扎的是如何将表OutputText的单元格A1中的文本插入书签之后的word文档?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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