Microsoft Word Macro - Adding Footnote to Specific Text

leever

New Member
Joined
Jun 29, 2023
Messages
12
Hello. I am trying to create a macro that adds footnotes in a Word document when there is specific text. For example, I have a list of names and addresses with common occurring typos.

1735612244528.png


From the data, the text that I would want footnoted are “SMith”, “NEw”, “ORLeans”.

This is the code I have but it is not giving me what I want.
---
Public Sub HighlightMultipleWords()
Application.ScreenUpdating = False

Dim sArr() As String
Dim rTmp As Range
Dim x As Long

Set rTmp = ActiveDocument.Range
sArr = Split("SMith”, “NEw”, “ORLeans ")
For x = 0 To UBound(sArr)

With rTmp.Find
.Text = sArr(x)
.Replacement.Text = sArr(x)
ActiveDocument.Footnotes.Add rTmp, Text:="Written in data"
End With
Next

End Sub
---

Any help would be greatly appreciated. Thank you!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
First, correct the line to the following.
VBA Code:
sArr = Split("SMith,NEw,ORLeans", ",")
And then keep fighting.

Artik
 
Upvote 0
First, correct the line to the following.
VBA Code:
sArr = Split("SMith,NEw,ORLeans", ",")
And then keep fighting.

Artik
Thank you Artik. I can't get the footnote numbers to come after the words. "123" shows up after the block of text. Any further tips would be greatly appreciated. Thank you.
 
Upvote 0
Try this
VBA Code:
Sub HighlightMultipleWords_2()
    Dim sArr() As String
    Dim rTmp As Range
    Dim x As Long

    Application.ScreenUpdating = False

    sArr = Split("SMith,NEw,ORLeans", ",")

    For x = 0 To UBound(sArr)

        Set rTmp = ActiveDocument.Range

        With rTmp.Find
            .Text = sArr(x)
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .MatchCase = True

            Do While .Execute
                ActiveDocument.Footnotes.Add Range:=rTmp, Text:="Note to the phrase '" & sArr(x) & "'" 
                rTmp.Collapse Direction:=wdCollapseEnd
            Loop

        End With

    Next x

    Application.ScreenUpdating = True
End Sub
Artik
 
Upvote 0
Hi there. Thank you so much for this. Very helpful. I have a follow-up question I’m hoping you can help me with. I have the data below:

1736735998280.png


I have edited the code to sArr = Split("Wa,Ne,Ky", ",")

Is there a way to ignore footnotes for specific words (Walter, Newman, Kylie). The footnote numbers appear after every “Wa”, “Ne”, and “Ky”. Thank you again.
 
Upvote 0
Try:
VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<[A-Z]{2}[!^13]@>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
  End With
  Do While .Find.Execute
    i = i + 1
    .Footnotes.Add Range:=.Duplicate.Characters.Last, Text:="Written in data"
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
 
Upvote 0
The previous macro handles text such as you have in your first post, without the need for an array of possible errors. The following macro extends that to deal with tabs and manual line-breaks in the data as well as with erroneous content in tables and at line ends, per post #5, above - again, without the need for an array of possible errors.
VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "<[A-Z]{2}[!^t^13^l]@>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
  End With
  Do While .Find.Execute
    i = i + 1: .Footnotes.Add Range:=.Duplicate.Characters.Last, Text:="Written in data"
    .Collapse wdCollapseEnd
  Loop
  .Start = ActiveDocument.Range.Start
  .Find.Text = "<[A-Z][a-z]>"
  Do While .Find.Execute
    If .Information(wdWithInTable) = True Then
      If .Characters.Last = .Cells(1).Range.Characters.Last.Previous Then
        i = i + 1: .Footnotes.Add Range:=.Duplicate.Characters.Last, Text:="Written in data"
      End If
    ElseIf .Characters.Last.Next Like "[" & Chr(11) & vbCr & vbTab & "]" Then
      i = i + 1: .Footnotes.Add Range:=.Duplicate.Characters.Last, Text:="Written in data"
    End If
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,493
Messages
6,185,311
Members
453,287
Latest member
Emeister

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top