2

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
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Debra
  • 35
  • 5

2 Answers2

1

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

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Cindy, thank you so much, that works perfectly. I truly appreciate you explaining your code too. Have a great day! – Debra Jul 11 '19 at 07:07
0

Not super familiar with word objects, but try this. Worked for my couple of tests.

Basically it loops through all foot notes. And uses the index of the word to set that word's bold property to true.

Sub Test()

    Dim oFootNote As Footnote
    Dim oLastIndex As Long

    For Each oFootNote In ActiveDocument.Footnotes

        oLastIndex = oFootNote.Range.Words.Count

        If oLastIndex > 2 Then
            oFootNote.Range.Words(oLastIndex).Bold = True
            oFootNote.Range.Words(oLastIndex - 1).Bold = True
        End If
    Next

End Sub
JosephC
  • 917
  • 4
  • 12
  • Hi JosephC, thanks for that. I've tried it but I get "Method or data member not found" ; it doesn't like the oFootNote.Range.Words(oFootNote.Range. I do appreciate your help though. Debbie – Debra Jul 10 '19 at 15:09
  • Guessing not all foot notes are at least 2 words long? edited code to check for that. So it should now only bold last two words if footnote is longer than 2 words. – JosephC Jul 10 '19 at 15:41
  • Hi, thanks again but it still doesn't like oFootNote.Range; I'm guessing it's because there is no intellisense for Range for oFootNote. It's so frustrating I know, I've been trying to work this out for a whole day. Thanks and have a good evening – Debra Jul 10 '19 at 15:48