Excel宏-打开特定的Word文件 [英] Excel macro - open specific word file
问题描述
我没有找到任何可以帮助我的东西.
I haven't found anything that can help me.
我正在尝试打开某个word文件,并在其中写入一些数据并以其他名称保存.这是我到目前为止的内容:
I'm trying to open a certain word file, have some data written in it and saved under a different name. This is what I have so far:
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application.8")
Set appWD = New Word.Application
Dim docWD As Word.Document
Set docWD = appWD.Documents.Open("C:\Documents and Settings\Excel macro\Standaard.docx")
appWD.Visible = True
'
' Data is selected and copied into "Design"
'
Copy all data from Design
Sheets("Design").Select
Range("A1:G50").Copy
' Tell Word to create a new document
appWD.Documents.Add
' Tell Word to paste the contents of the clipboard into the new document
appWD.Selection.Paste
' Save the new document with a sequential file name
Sheets("Sheet1").Select
appWD.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "/" & "TEST" & Range("C8").Text
' Close this new word document
appWD.ActiveDocument.Close
' Close the Word application
appWD.Quit
目前它所要做的只是;打开Standaard.docx文件,打开一个新文件,然后将所有内容粘贴到新文件中并保存.它应该打开Standaard.docx文件,将其粘贴到其中并以新名称保存.
At the moment all it does is; open the Standaard.docx file, open a new file and paste everything in the new file and saves. It should open the Standaard.docx file, paste it in there and save under a new name.
非常感谢!
推荐答案
它打开一个新文档的原因是因为您有以下一行:
The reason that it opens a new document is because you have the line:
appWD.Documents.Add
在您的代码行之前:
appWD.Selection.Paste
如果您删除 appWD.Documents.Add
,则Word将粘贴到您的活动文档(即"Standaard.docx")中.
if you remove the appWD.Documents.Add
Word will paste into your active document (i.e. "Standaard.docx").
只需一点,您就不需要以下行:
Just one other point, you do not need the line:
Set appWD = CreateObject("Word.Application.8")
当您使用以下命令立即在其下面的行中初始化一个新的Word应用程序时:
as you immediately initialise a new Word application in the line below it with:
Set appWD = New Word.Application
这篇关于Excel宏-打开特定的Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!