vba Excel 宏 - 打开特定的 word 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5310376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel macro - open specific word file
提问by CustomX
I haven't found anything that can help me.
我还没有找到任何可以帮助我的东西。
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:
我正在尝试打开某个 word 文件,在其中写入一些数据并以不同的名称保存。这是我到目前为止:
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
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.
目前它所做的就是;打开 Standard.docx 文件,打开一个新文件并将所有内容粘贴到新文件中并保存。它应该打开 Standaard.docx 文件,将其粘贴在那里并以新名称保存。
Many thanks!
非常感谢!
回答by i_saw_drones
The reason that it opens a new document is because you have the line:
它打开一个新文档的原因是因为您有以下行:
appWD.Documents.Add
in your code before the line:
在您的代码行之前:
appWD.Selection.Paste
if you remove the appWD.Documents.Add
Word will paste into your active document (i.e. "Standaard.docx").
如果删除appWD.Documents.Add
Word 将粘贴到您的活动文档中(即“Standaard.docx”)。
Just one other point, you do not need the line:
还有一点,您不需要该行:
Set appWD = CreateObject("Word.Application.8")
as you immediately initialise a new Word application in the line below it with:
当您立即在其下方的行中初始化一个新的 Word 应用程序时:
Set appWD = New Word.Application
回答by MelindaNC
This macro opens a file then saves it as a new file name in a different folder based on info updated in the sheet1 of the Excel file
此宏打开一个文件,然后根据 Excel 文件的 sheet1 中更新的信息将其另存为不同文件夹中的新文件名
Sub OpenDocFileNewName()
'
' OpenDocFileNewName Macro
'
'
Set WordApp = CreateObject("Word.Application.8")
Set WordDoc = WordApp.Documents.Open("C:\Users\mmezzolesta\Documents\_TestDataMerge\STANDARD.docx")
WordApp.Visible = True
'
'
'Save as new file name
Sheets("Sheet1").Select
WordApp.ActiveDocument.SaveAs Filename:=("C:\Users\mmezzolesta\Documents\_TestMailMergeAuto") & "/" & Range("A2") & "Standard-Grounding-" & Range("e2").Text
WordApp.ActiveDocument.Close
WordApp.Quit
'
'
End Sub