如何为Excel编程添加对Excel VBA的引用 [英] How to add references to Excel VBA for Word programming
问题描述
我正在尝试编写一个宏,该宏使用Word对象基于Word模板文档创建Word文档.
I am trying to write a macro which uses the Word objects to create a Word document based on a Word template document.
使用Option Explicit时遇到一些困难.没有它,我就可以省略Dim ...行,但是我想在那行.
I am having some difficulties when I use Option Explicit. Without it I can just omit the Dim ... line but I would like that there.
这是到目前为止的代码:
Here is the code so far:
Option Explicit
Sub PopulateWordDoc()
Dim wApp As Object
Set wApp = CreateObject("Word.Application")
Dim wdDoc As Word.Document ' User-defined type not defined error on this line
Set wDoc = wApp.Documents.Open("C:\path\WordTestTemplateDoc.dotx", ReadOnly:=False)
With wDoc
.Content.Find.Execute FindText:="<Project ID>", ReplaceWith:="This is the project id....."
.SaveAs2 Filename:=("C:\path\NewWordDoc.docx"), FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With
结束子
推荐答案
如本文所述,在VBE中将引用添加到 Microsoft Word xx.0对象库
:
Add a reference in the VBE to Microsoft Word xx.0 Object Library
as described in this article:
https://msdn.microsoft.com/en-us/library/office/gg264402.aspx
然后,VBE甚至会像您为Excel编写代码时一样提供自动完成功能.
Afterwards, the VBE will even offer auto-completion as it does when you write code for Excel.
这篇关于如何为Excel编程添加对Excel VBA的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!