We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Formatting a Word 2003 footnote using VBA

Medium Priority
1,332 Views
Last Modified: 2012-06-27
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")
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Author

Commented:
An excellent answer, great detail.
Glad to have helped. :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.