本文介绍了VBA Excel用Excel单元格值替换Word文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我想使用放置在Excel单元格中的地址替换Word中的地址.

I would like to replace the address in Word using the one, placed in the Excel cell.

我的情况如下图所示:

我在Excel中有一个地址,我想粘贴在表格单元格内的Word括号中.

I have got an address in Excel and I want to paste in in the Word bracket, based inside the table cell.

有一些解决方案:

从Excel VBA写入Word文档

使用word将字符串从excel粘贴到Word-VBA

从Word到Excel的VBA

VBA宏:Excel到Word文本替换

与我的情况不同.

到目前为止,我的代码如下:

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

Word文档正在打开,但我不知道我的文本已复制到哪里.

The Word document is opening, but I don't know, where my text has been copied.

神的暗示似乎在这里:

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

但适用于我认为空白的Word文档.

but applies to the blank Word document I think.

推荐答案

我知道将数据插入Word文档的最简单方法是使用 Bookmarks 对象(此处的文档)( wordmvp此处写入-非常易于遵循).

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

考虑到这一点,当您通过Excel控制此操作时,我会将您的地址值放入 String 变量中,并将该变量分配给文档上的书签.

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.

类似:

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

这篇关于VBA Excel用Excel单元格值替换Word文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:18
查看更多