简体   繁体   中英

VBA Macro to Replace Endnotes in Word

I am hoping to use a macro to replace the endnotes in a word document. 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. I just dont know how to go about replacing the endnotes in old.docx with the value of ftstr (please see below).

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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