我有一个宏,它根据单词模板创建实验报告,然后将其保存到远程服务器。在我的 Excel 工作表中选择一个单元格,运行宏,打开一个带有空白模板的 Word 实例,并根据所选单元格和其他数据保存文件。我想要做的是编辑空白模板中的标题并将其更新为位于所选单元格行第一列中的实验名称。
当我从本地文件打开文件时,下面的代码有效,但当文件位于服务器上时无效。
Dim oWord As Object
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Activate
' Open a new instance of the ExperimentTemplate and save it
oWord.Documents.Add Template:="ExperimentTemplate", NewTemplate:=False, DocumentType:=0
oWord.ActiveDocument.SaveAs FileName:=fPath & "example.docx", FileFormat:=wdFormatXMLDocument 'fPath is the path to the folder where file will be saved on the server
oWord.ActiveDocument.Close
' Re-open file and change Experiment Name
oWord.Documents.Open FileName:=fPath & "example.docx"
Set oSelection = oWord.Selection
oSelection.Find.Text = "Experiment Name"
oSelection.Find.Replacement.Text = name 'defined as the text …Run Code Online (Sandbox Code Playgroud)