PDA

View Full Version : [SOLVED:] Retaining Formatting When Extracting Text for Footnotes (Office 2007)



Opv
09-13-2016, 09:58 AM
I am exploring possible solutions for retaining formatting when text is extracted from a document via VBA to automatically create footnotes in Word 2007. I'm basically looping through the paragraphs and grabbing strings based on a predefined tagging system.



Selection.Range.Footnotes.Add Range:=Selection.Range, Text:=str


Everything works great except that the italic used for book titles, etc., is lost once the strings are extracted and inserted within the footnotes. I am just curious as to whether there is a method I am overlooking before I take the time needed to code a work-around.

Thanks

gmayor
09-13-2016, 09:24 PM
You'll need the workaround e.g.

Dim oRng As Range
Dim oFootNote As Footnote
Set oRng = Selection.Range
Set oFootNote = oRng.Footnotes.Add(Range:=oRng, Text:=oRng.Text)
oFootNote.Range.FormattedText = oRng.FormattedText

Opv
09-14-2016, 07:52 AM
Ah, indeed. Thanks!

gmaxey
09-14-2016, 11:44 AM
If you are looping through the document then you don't really have to select anything:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim oFN As Footnote
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "<test>"
While .Execute
oRng.End = oRng.End - 1
oRng.Start = oRng.Start + 1
Set oFN = ActiveDocument.Footnotes.Add(oRng)
oFN.Range.FormattedText = oRng.FormattedText
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub

Assumes "<" and">" are your opening and closing tags.