vba Excel VBA打开word模板,填充,然后另存为其他地方的.docx文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26042519/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:59:09  来源:igfitidea点击:

Excel VBA to open word template, populate, then save as .docx file somewhere else

excelvbaexcel-vba

提问by Brian

I created a word template with placeholders such as <> that I am then able to replace automatically with my excel macro. When I tried this process again, the word document now opens saying it is a read only document. How am I supposed to save my Word Template so it can be edited? Also, when I open the word template through my excel macro, how does it know to save it as a new word document, and not save it as an updated template?

我创建了一个带有诸如 <> 之类的占位符的单词模板,然后我可以用我的 excel 宏自动替换它。当我再次尝试这个过程时,word 文档现在打开说它是一个只读文档。我应该如何保存我的 Word 模板以便可以对其进行编辑?另外,当我通过我的excel宏打开word模板时,它如何知道将其另存为新的word文档,而不是将其另存为更新的模板?

Here is my code:

这是我的代码:

Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Open("file name here")

With wDoc
    .Application.Selection.Find.Text = "<<name>>"
    .Application.Selection.Find.Execute
    .Application.Selection = Range("A5")
    .Application.Selection.EndOf

    .Application.Selection.Find.Text = "<<dob>>"
    .Application.Selection.Find.Execute
    .Application.Selection = Range("A6")

    .SaveAs2 Filename:=("file name goes here"), _
    FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With

End Sub

采纳答案by Valiante

While @wahwahwah's approach works, it is still opening the template as a document for editing, then saving it in another format, while suppressing alerts. What I suspect you want to achieve is the behaviour when opening a template from the shell, which generates a "new" document based on the template. You can achieve this with the "Add" method thus;

虽然@wahwahwah 的方法有效,但它仍将模板作为文档打开以进行编辑,然后将其保存为另一种格式,同时抑制警报。我怀疑您想要实现的是从 shell 打开模板时的行为,它根据模板生成一个“新”文档。您可以使用“添加”方法来实现这一点;

Set wDoc = wApp.Documents.Add(Template:="file path here", NewTemplate:=False, DocumentType:=0)

回答by wahwahwah

If you indicate that the file is ReadOnly while setting the file name, and you turn off alerts, this should solve the issue of the prompt:

如果您在设置文件名时指示该文件为只读,并且您关闭警报,这应该可以解决提示问题:

Set wApp = CreateObject("Word.Application")
wApp.DisplayAlerts = False
Set wDoc = wApp.Documents.Open Filename:="C:\Documents\SomeWordTemplate.dot", ReadOnly:=True

And when you go to save the file, just save it with the ".doc" file extension instead of ".dot" so its saved as a word file type. You can also change the file name and output directory path if you so choose. (Also, remember to turn the alerts back on)

当你去保存文件时,只需使用“.doc”文件扩展名而不是“.dot”来保存它,以便将其保存为word文件类型。如果您愿意,您还可以更改文件名和输出目录路径。(另外,记得重新打开警报)

With wDoc

.ActiveDocument.SaveAs Filename:="C:\Documents\NewWordDocumentFromTemplate.doc"

End With

wApp.DisplayAlerts = True

Hope this helps!

希望这可以帮助!