gpt4 book ai didi

vba - Excel VBA 打开 Word 模板,填充,然后在其他位置另存为 .docx 文件

转载 作者:行者123 更新时间:2023-12-01 22:48:25 25 4
gpt4 key购买 nike

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

这是我的代码:

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

最佳答案

虽然 @wahwahwah 的方法有效,但它仍然将模板作为文档打开进行编辑,然后以其他格式保存,同时抑制警报。我怀疑您想要实现的是从外壳打开模板时的行为,它会根据模板生成"new"文档。您可以使用“添加”方法来实现此目的;

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

关于vba - Excel VBA 打开 Word 模板,填充,然后在其他位置另存为 .docx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26042519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com