Search code examples
vbams-wordformattingfootnotes

How do I make the last two words of each footnote bold using Word VBA?


I've changed balloon comments to footnotes, taking the author's name too. I need the author's name to be in bold but I can't get my code to read the footnotes. My problem is in setting : oFootnote

I've tried calling on the strAuthor and making that bold but because it is no longer a comment.author I can no longer set it as it's now in the footnote. I've tried many examples on the internet but I just can't get them to work: StackOverflow's How do i make a string bold; Insert bold text into Word using VBA also

 Set oFootnote = oDoc.Footnotes.Add(Range:=Selection.Range, Text:="Some text") 

I am a trainee so please don't judge me too harshly

'Convert comments to footnotes with Author name in bold
Dim i As Long
Dim oDoc As Document
dim oComment as Comments
Dim oFootnote As Footnotes

'Document is the ActiveDocument
Set oDoc = Application.ActiveDocument

'the author's name needs to be bold (the last two words in each footnote)
Set oFootnote = oDoc.Footnotes

    With oFootnote
      Selection.Range.Words.Last.Words (2)
        'Make the last two words bold'
        With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Font.bold = True
        End With
    End With
    Selection.Find.Execute
    'Set oFootnote = Nothing
  Next

I tried

 Set oFootnote = oDoc.Footnotes Range:=Selection.Words.Last.Words(2) 

but it doesn't like "Range:= onwards" so I did

 Selection.Range.Words.Last.Words (2)                invalid use of a property

Solution

  • There is usually more than one way to achieve something like this, but the key is usually to work with a dedicated Range object.

    In the code below, that bases on the code in the question, the Range object is assigned to each individual Footnote object in a loop of the Footnotes. It is then collapsed to its end-point and the start extended backwards by two words. (To better understand how this works, think of selecting the footnote, pressing right-arrow, then pressing ctrl+shift+left arrow twice to select the last two words.)

    Dim oDoc As Document
    Dim oFootnotes As Footnotes
    Dim Ftnote As Footnote
    Dim rngFootnote As Word.Range
    
    'Document is the ActiveDocument
    Set oDoc = Application.ActiveDocument
    
    'the author's name needs to be bold (the last two words in each footnote)
    Set oFootnotes = oDoc.Footnotes
    For Each Ftnote In oFootnotes
        Set rngFootnote = Ftnote.Range
        rngFootnote.Collapse wdCollapseEnd
        rngFootnote.MoveStart wdWord, -2
        rngFootnote.Font.Bold = True
    Next
    

    Note that the reason for one of the errors in the question is because Words.Last returns a Range object containing the last word. Since it contains only one word - the last - Words(2) can't find anything it can work with.

    The reason for the other error is that it's not possible to assign a Range to a Footnote or Footnotes object. They're different things, entirely...