从 Excel VBA 更新 Word 文档中的链接字段

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

Update linked fields in Word document from Excel VBA

excelvbaexcel-vbams-wordword-vba

提问by some_guy

I am trying to automatically update certain information (such as names, dates and numbers) across 3 different Word documents by putting the data into a spreadsheet and linking to the respective cells from Word. The spreadsheet has some Macros in it which auto-update parts of the spreadsheet internally.

我试图通过将数据放入电子表格并链接到 Word 中的相应单元格来自动更新 3 个不同 Word 文档中的某些信息(例如姓名、日期和数字)。电子表格中有一些宏可以在内部自动更新电子表格的某些部分。

Everything is working fine, except for updating the links in the Word documents. When trying to update a link in Word by right-clicking on it and selecting the "update link" option it brings up the Macro warning dialog for the spreadsheet, asking whether I want to activate Macros or not. It doesn't do this just once but constantly during the 20s or so the update process takes (which seems unusually long to me). So updating the link works, but only if you're willing to click the "activate Macros" button of a few dozen times.

一切正常,除了更新 Word 文档中的链接。当尝试通过右键单击并选择“更新链接”选项来更新 Word 中的链接时,它会显示电子表格的宏警告对话框,询问我是否要激活宏。它不会只执行一次,而是在 20 多岁左右的更新过程中不断执行此操作(这对我来说似乎异常长)。所以更新链接是有效的,但前提是你愿意点击几十次“激活宏”按钮。

I tried to automate updating all fields in a document from Word with VBA, but that has the same problem, it also brings up the Macros dialog constantly for half a minute. Here's my code for that:

我尝试使用 VBA 从 Word 自动更新文档中的所有字段,但这有同样的问题,它还会持续半分钟显示宏对话框。这是我的代码:

Sub UpdateFields()
    ActiveDocument.Fields.Update
End Sub

I also tried to update the Word documents directly from the spreadsheet, but that does not work either, because when Excel tries to open a Word document via VBA the program stops executing and trows this error:

我还尝试直接从电子表格更新 Word 文档,但这也不起作用,因为当 Excel 尝试通过 VBA 打开 Word 文档时,程序停止执行并引发此错误:

"Excel is waiting for another application to complete an OLE action."

“Excel 正在等待另一个应用程序完成 OLE 操作。”

Clicking ok and waiting does not help because the error message reappears after a few seconds, and the only way to stop it is to manually kill the Excel process.

单击确定并等待无济于事,因为错误消息在几秒钟后再次出现,而停止它的唯一方法是手动终止 Excel 进程。

Here's my Excel Macro code:

这是我的 Excel 宏代码:

Sub LoopThroughFiles()
    Path = Application.ActiveWorkbook.Path
    Dim WordFile As String
    WordFile = Dir(Path & "\*.doc")
    Do While Len(WordFile) > 0
        Run Update(Path & "\" & WordFile)
        WordFile = Dir
    Loop
End Sub

Function Update(Filepath As String)

    Dim WordDoc As Word.Document
    Set WordApplication = CreateObject("Word.Application")
    Set WordDoc = WordApplication.Documents.Open(Filepath) 'This produces the error

    ActiveDocument.Fields.Update

End Function

Note that the only files in the folder are the 3 documents and the spreadsheet, and the program does find the files without any problems.

请注意,文件夹中唯一的文件是 3 个文档和电子表格,程序确实可以毫无问题地找到这些文件。

I have searched for solutions online but I did not really find anything, which I found odd, since it seems like a pretty common thing that someone would do with VBA. Then again, I have very little experience with VBA, so I might be completely missing the point and there is a super simple solution I am just not aware of.

我在网上搜索了解决方案,但我没有真正找到任何东西,我觉得这很奇怪,因为这似乎是有人用 VBA 做的一件很常见的事情。再说一次,我对 VBA 的经验很少,所以我可能完全没有抓住重点,有一个我不知道的超级简单的解决方案。

采纳答案by David Zemens

I think I see the error, which is a silent failure, becuase the document contains links, there is an open dialog waiting for you to say "yes" or "no" to update the links.

我想我看到了错误,这是一个无声的失败,因为文档包含链接,有一个打开的对话框等待您说“是”或“否”以更新链接。

We can suppress this dialog by disabling the automatic link updates (WordApplication.Options.UpdateLinksAtOpen = False).

我们可以通过禁用自动链接更新 ( WordApplication.Options.UpdateLinksAtOpen = False)来抑制此对话框。

Function Update(Filepath As String)
    Dim WordApplication As Word.Application
    Dim WordDoc As Word.Document
    Dim updateLinks As Boolean

    Set WordApplication = CreateObject("Word.Application")
        updateLinks = WordApplication.Options.UpdateLinksAtOpen 'capture the original value
        WordApplication.Options.UpdateLinksAtOpen = False      'temporarily disable

    Set WordDoc = WordApplication.Documents.Open(Filepath)
        WordDoc.Fields.Update
        'MsgBox "Links updated in " & WordDoc.Name
        '## Save and Close the Document
        WordDoc.Save
        WordDoc.Close

    '## reset the previous value and Quit the Word Application
    WordApplication.Options.UpdateLinksAtOpen = updateLinks             '
    WordApplication.Quit

End Function

Also, remember to Save and Close the document, and Quit the word application inside the function.

另外,记得保存并关闭文档,并退出函数内的单词应用程序。

I made this other modification:

我做了另一个修改:

In your function, ActiveDocumentis not an object in Excel, so you would need to qualify it, otherwise that line will also throw an error. Rather than refer to WordApplication.ActiveDocument, I just simply refer to the WordDocwhich you have already assigned.

在您的函数中,ActiveDocument它不是 Excel 中的对象,因此您需要对其进行限定,否则该行也会引发错误。而不是参考WordApplication.ActiveDocument,我只是简单地参考WordDoc您已经分配的 。