1
votes

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
1
what do you mean by 'placeholders'??Kazimierz Jawor
I mean fake endnotes. like document 1 will have the right body content, but the endnote will say "endnote 1 goes here." the other document has the correct value for endnote 1 and im trying to replace it.Brandon

1 Answers

1
votes

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