我对VBA还是很陌生,试图用Excel中的VBA从Excel中填写单词模板。我正在使用以下代码将Date放入word文档的标签中: 昏暗的模板作为字符串
Dim WordDoc, WordApp As Object
Dim Template as String
On Error Resume Next
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
End If
Template = "C:\Users\me\Dropbox\me - Summer 2019\RMA_Log\00059-001_E_24May19_Form Returned Authorization.docx"
Set WordDoc = WordApp.Documents.Open(FileName:=Template, ReadOnly:=False)
With WordDoc.Content.Find
.Text = "<Date>"
.Replacement.Text = "06-06-2019"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
但这不会替换我的word文档主体中的任何文本。
我尝试做WordDoc.ActiveDocument.Content.Find
,但是那也不起作用。如果我完全复制了代码并将WordDoc
替换为ActiveDocument
并在VBA单词中运行它,则文本将被替换。
Dim Template As String
Template = "C:\Users\me\Dropbox\me - Summer 2019\RMA_Log\00059-001_E_24May19_Form Returned Authorization.docx"
With ActiveDocument.Content.Find
.Text = "<Date>"
.Replacement.Text = "06-06-2019"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
如果我在Word VBA中运行此代码,它将替换标记,为什么不能从Excel运行此代码?
编辑:Office 2016
答案 0 :(得分:6)
Excel不知道wdReplaceAll
或wdFindContinue
是什么。
放置对Word对象模型(Tools > References > Microsoft Word XX Object Model
)的引用。
如果代码顶部有Option Explicit
,则会突出显示该内容。