Formatting a Word 2003 footnote using VBA

I have inserted a footnote using VBA, but the text of the reference (the footnoote itself) needs to be formatted, with part of it in italics. How do I do this?

I have a footnote object from adding the footnote, viz

Set oFootnote = oDoc.Footnotes.Add(Range:=Selection.Range, Text:="Some text")
LVL 9
crescendoAsked:
Who is Participating?
 
Antagony1960Connect With a Mentor Commented:
Try something like this:
Dim oFootNote As Object
Dim rng As Range
    Set oFootNote = ActiveDocument.Footnotes.Add(Range:=Selection.Range)
    Set rng = oFootNote.Range
    With rng
        'Make the first word bold'
        .Collapse wdCollapseEnd
        .InsertAfter "Some "
        .Font.Bold = True
        
        'Make the second word italic but not bold'
        .Collapse wdCollapseEnd
        .InsertAfter "text"
        .Font.Bold = False
        .Font.Italic = True
    End With
    Set rng = Nothing
    Set oFootNote = Nothing

Open in new window

0
 
crescendoAuthor Commented:
That's really good yet again. One last detail: the cursor is left in the footnote, how can I get it back to the original insertion point just before the footnote was added?
0
 
Antagony1960Commented:
Thinking about it, you could do away with the oFootNote variable and just use the range object by changing line 3 to this:

    Set rng = ActiveDocument.Footnotes.Add(Selection.Range).Range
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Antagony1960Commented:
Ah, we cross-posted there. :-)

Yes, we need to insert a temporary bookmark and return to that. Try this:
Dim rng As Range, bmk As Bookmark
    Set bmk = Selection.Bookmarks.Add("Temp")
    Set rng = ActiveDocument.Footnotes.Add(Selection.Range).Range
    With rng
        'Make the first word bold'
        .Collapse wdCollapseEnd
        .InsertAfter "Some "
        .Font.Bold = True
        
        'Make the second word italic but not bold'
        .Collapse wdCollapseEnd
        .InsertAfter "text"
        .Font.Bold = False
        .Font.Italic = True
    End With
    Selection.GoTo wdGoToBookmark, , , "Temp"
    Selection.Bookmarks("Temp").Delete
    Set rng = Nothing
    Set bmk = Nothing

Open in new window

0
 
crescendoAuthor Commented:
An excellent answer, great detail.
0
 
Antagony1960Commented:
Glad to have helped. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.