简体   繁体   English

VBA Excel 用 Excel 单元格值替换 Word 文本

[英]VBA Excel replace Word text with Excel cell value

Good morning,早上好,

I would like to replace the address in Word using the one, placed in the Excel cell.我想用 Excel 单元格中的地址替换 Word 中的地址。

My situation looks like in the image below:我的情况如下图所示:

在此处输入图像描述

I have got an address in Excel and I want to paste in in the Word bracket, based inside the table cell.我在 Excel 中有一个地址,我想粘贴在表格单元格内的 Word 括号中。

There are some solutions:有一些解决方案:

Write to Word Document from Excel VBA 从 Excel VBA 写入 Word 文档

Pasting a String from excel to Word using word-VBA 使用 word-VBA 将字符串从 excel 粘贴到 Word

VBA from Word to Excel VBA 从字到 Excel

VBA macros: Excel to Word text replacement VBA 宏:Excel 到 Word 文本替换

which differs from my situation.这与我的情况不同。

My code so far looks like this:到目前为止,我的代码如下所示:

 Sub RamsOpen()
 Dim appWD As Word.Application
 Set appWD = New Word.Application
 Dim docWD As Word.Document
 Set docWD = appWD.Documents.Open(ActiveWorkbook.path & "\RAMS.docx.docm")
 appWD.Visible = True

 Sheets("Frontsheet").Range("D18").Copy

 docWD.Content.InsertAfter Range("A1")

 End Sub

The Word document is opening, but I don't know, where my text has been copied. Word 文档正在打开,但我不知道我的文本被复制到了哪里。

The god hint appears to be here:上帝的提示似乎在这里:

https://exceloffthegrid.com/controlling-word-from-excel-using-vba/ https://exceloffthegrid.com/controlling-word-from-excel-using-vba/

but applies to the blank Word document I think.但适用于我认为的空白Word文档。

The easiest way I know of inserting data to a Word document is by using the Bookmarks object ( Documentation here ) ( wordmvp writeup here - very easy to follow).我知道的将数据插入 Word 文档的最简单方法是使用Bookmarks object( 此处为文档)(此处为 wordmvp writeup - 非常容易理解)。

Taking that into account, as you are controlling this from Excel, I'd put your address value into a String variable and assign that variable to a bookmark on the document.考虑到这一点,当您从 Excel 控制它时,我会将您的地址值放入一个String变量中,并将该变量分配给文档上的书签。

Something like:就像是:

Dim appWD As Word.Application
Set appWD = New Word.Application
Dim docWD As Word.Document
Set docWD = appWD.Documents.Open(ActiveWorkbook.path & "\RAMS.docx.docm")
appWD.Visible = True
Dim ExcelAddressValue as String

ExcelAddressValue = Sheets("Frontsheet").Range("D18").Value

docWd.Bookmarks("YourBookmarkNameHere").Range.Text = ExcelAddressValue

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

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