通过VBA在书签后的Word中插入Excel单元格文本 [英] Insert Excel cell text in Word after bookmark via VBA

查看:442
本文介绍了通过VBA在书签后的Word中插入Excel单元格文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Excel在A1单元格中为我提供了自定义的文本,然后应将其传递到特定部分(在书签之后)的单词字母.

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).

刚从VBA开始,发现并编辑了能够提供一半解决方案的代码.我遇到的困难是如何将表OutputText的A1单元格中的文本插入书签之后的word文档?

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

谢谢!

推荐答案

这对您有用吗? (我不能相信该代码):

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

这篇关于通过VBA在书签后的Word中插入Excel单元格文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆