Hi All,
I have an Excel routine that populates a Word template by searching codes and replacing them with the relevant text. This is working fine.
Embedded within the replacement text, I have additional codes that relate to footnotes - the Excel tool can correctly identify these codes, look up the relevant footnote text and add a new footnote with the correct reference and text. All good.
What I cannot get it to successfully do is place the footnote at the correct location in the body of the Word document. Currently, my best solution is as follows:
Dim Rge as Object, SearchCode$ 'wDoc Object passed-into the procedure as open Word Doc
SearchCode = "{1}" 'unique dummy code to find in body of text
Set Rge = wDoc.Content 'set the range object to the whole body of the doc
Rge.Start = InStr(Rge.Text, SearchCode) 'set the start of the range to be where the instance of the unique SearchCode appears <--- THIS IS THE PROBLEM
With Rge.Find
.ClearFormatting
.Text = SearchCode
.Replacement.Text = ""
.Forward = True
.Wrap = 1
.Execute Replace:=2 'remove the searchcode
End With
Rge.End = Rge.Start 'define the range as the one character space where the unique code was originally
Rge.Footnotes.Add Range:=Rge, Reference:="1", Text:="Test" 'insert the footnote here
The problem is in correctly defining the location of the SearchCode within the body of the document - using the InStr() function doesn't work because this picks up all the nonprintable characters in the document, so the character count is never correct.
Any suggestions for how to get around this? Perhaps one of the following:- Is there some way of me defining Rge in terms of an output from the .Find function?
- Alternatively, can I replace the SearchCode directly with the footnote?
- Or, instead, is there a simple way of identifying the accurate character count / insertion point for the replaced text?
Many thanks in advance,
AL
Bookmarks