简体   繁体   English

VBA宏替换Word中的尾注

[英]VBA Macro to Replace Endnotes in Word

I am hoping to use a macro to replace the endnotes in a word document. 我希望使用宏替换Word文档中的尾注。 Here is my situtation: 这是我的观点:

I have two word docs. 我有两个单词文档。 Both documents have the exact same number of endnotes. 两个文件的尾注数量完全相同。 One document is full of the correct body content, but has placeholders for the end notes. 一个文档中包含正确的正文内容,但末尾有占位符。 The other document has outdated content, but has the correct endnotes to to fill the placeholders in the first document. 另一个文档的内容已经过时,但是具有正确的尾注以填充第一个文档中的占位符。

I have setup a macro below that can loop through all of the endnotes in the correct file, and then opens the other document called "old.docx" below. 我在下面设置了一个宏,该宏可以循环浏览正确文件中的所有尾注,然后在下面打开另一个名为“ old.docx”的文档。 I just dont know how to go about replacing the endnotes in old.docx with the value of ftstr (please see below). 我只是不知道如何用ftstr的值替换old.docx中的尾注(请参见下文)。

Any help would be great! 任何帮助将是巨大的!

Sub endnoteReplacer()

Dim ft As Endnote
Dim wrdApp As Object
Dim wrdDoc As Object

Dim r1 As Range, ftstr As String, mark

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False
    Set wrdDoc = wrdApp.Documents.Open("C:\Desktop\old.docx")

For Each ft In Word.ActiveDocument.Endnotes

    ftstr = ft.Range.Text

    wrdDoc.Endnotes(ft.Index).Range.Text = ftstr

Next ft

End Sub

If I get you right you need this simple solution to add within your loop: 如果我正确,您需要在循环中添加以下简单解决方案:

For Each ft In Word.ActiveDocument.Endnotes

    ftstr = ft.Range.Text
    'change value of corresponding footnote in old.docx to value of ftstr
    '!! new line !!
    wrdDoc.Endnotes(ft.Index).Range.Text = ftstr
Next ft

But I assumed that you need to change endnotes(1) to endnotes(1), 2 to 2, etc... 但是我假设您需要将endnotes(1)更改为endnotes(1),将2更改为2,依此类推...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM