Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 377425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:41:43+00:00 2026-05-12T14:41:43+00:00

I have a VBA macro for Microsoft Word that I am trying to improve.

  • 0

I have a VBA macro for Microsoft Word that I am trying to improve.

The purpose of the macro is to bold and italicize all words in a document that match the search terms in the first table of the document.

The problem is the search terms include wildcards which are the following:

the hyphen “-“: between letters a wildcard for either a space or a period

asterisk “&”: (the site is not letting me put in asterisks as this is the markdown for italicize, so I’ll put in the & symbol instead to get around the filters) a wildcard for any number of characters at the beginning of a word or at the end. Unlike normal programming languages though, when it is used in the middle of the word it needs to be combined with the hyphen to be a wildcard for a range of characters. For example “th&-e” would pick up “there” while “th&e” would not.

question mark “?”: wildcard for a single character

What I am doing so far is just testing for these characters and if they are present I either lop them off in the case of the asterisk, or I alert the user that they have to search for the word manually. Not ideal 😛

I have tried the .MatchWildcard property in VBA but have not yet gotten it to work. I have a feeling it has something to do with the replacement text, not the search text.

A working macro will take the following as its input (the first row is intentionally ignored and the second column is the one with the target search terms):

Imagine this in a table all in the second column (as the html allowed here doesn’t allow tr and td etc)

First row: Word
Second row: Search
Third row: &earch1
Fourth row: Search2&
Fifth row: S-earch3
Sixth row: S?arch4
Seventh row: S&-ch5

And it will search the document and replace with bold and italicized content like so:

Search Search1 Search2 Search3 Search4 Search5

Note: S-earch3 could also pick up S.earch3 and replace with Search3

As one might assume the search terms will usually not be right next to each other – the macro should find all instances.

I will include my attempted but nonfunctional code as well after the first working macro.

The code for the working macro will be on pastebin for a month from today, which is 9/17/09, at the following url.

Thanks again for any thoughts and help you might have to offer!

Sara

Working VBA Macro:

Sub AllBold()

Dim tblOne As Table

Dim celTable As Cell

Dim rngTable As Range

Dim intCount As Integer

Dim celColl As Cells

Dim i As Integer

Dim rngLen As Integer

Dim bolWild As Boolean

Dim strWild As String


Set tblOne = ActiveDocument.Tables(1)

intCount = tblOne.Columns(2).Cells.Count

Set celColl = tblOne.Columns(2).Cells

strWild = ""

For i = 1 To intCount

    If i = 1 Then

    i = i + 1

    End If

    Set celTable = ActiveDocument.Tables(1).Cell(Row:=i, Column:=2)

    Set rngTable = ActiveDocument.Range(Start:=celTable.Range.Start, _
        End:=celTable.Range.End - 1)

    rngLen = Len(rngTable.Text)

    bolWild = False

    If (Mid(rngTable.Text, rngLen, 1) = "&") Then 'remember to replace & with asterisk!'

    rngTable.SetRange Start:=rngTable.Start, End:=rngTable.End - 1

    End If

    If (Mid(rngTable.Text, 1, 1) = "&") Then 'remember to replace & with asterisk!'

    rngTable.SetRange Start:=rngTable.Start + 1, End:=rngTable.End

    End If

    If InStr(1, rngTable.Text, "-", vbTextCompare) > 0 Then

    strWild = strWild + rngTable.Text + Chr$(13)

    bolWild = True

    End If

    If InStr(1, rngTable.Text, "?", vbTextCompare) > 0 Then

    strWild = strWild + rngTable.Text + Chr$(13)

    bolWild = True

    End If

    If (bolWild = False) Then

        Dim oRng As Word.Range

            Set oRng = ActiveDocument.Range

            With oRng.Find

            .ClearFormatting

            .Text = rngTable.Text

            With .Replacement

            .Text = rngTable.Text

            .Font.Bold = True

            .Font.Italic = True

            End With

            .Execute Replace:=wdReplaceAll

    End With

    End If

Next

If bolWild = True Then

MsgBox ("Please search the following strings with - or ? manually:" + Chr$(13) + strWild)

End If

End Sub

Attempted Nonfunctional VBA Macro:

Sub AllBoldWildcard()

Dim tblOne As Table

Dim celTable As Cell

Dim rngTable As Range

Dim intCount As Integer

Dim celColl As Cells

Dim i As Integer

Dim rngLen As Integer

Dim bolWild As Boolean

Dim strWild As String

Dim strWildcard As String


Set tblOne = ActiveDocument.Tables(1)

intCount = tblOne.Columns(2).Cells.Count

Set celColl = tblOne.Columns(2).Cells

strWild = ""

For i = 1 To intCount

    If i = 1 Then

    i = i + 1

    End If

    Set celTable = ActiveDocument.Tables(1).Cell(Row:=i, Column:=2)

    Set rngTable = ActiveDocument.Range(Start:=celTable.Range.Start, _
        End:=celTable.Range.End - 1)

    rngLen = Len(rngTable.Text)

    bolWild = False

    If (Mid(rngTable.Text, 1, 1) = "&") Then 'remember to replace & with asterisk!'

    rngTable.SetRange Start:=rngTable.Start + 1, End:=rngTable.End

    End If

    If InStr(1, rngTable.Text, "&", vbTextCompare) > 0 Then 'remember to replace & with asterisk!'

    strWildcard = rngTable.Text

    rngTable.Text = Replace(rngTable.Text, "&", "", 1) 'remember to replace & with asterisk!'

    bolWild = True

    End If

    If InStr(1, rngTable.Text, "-", vbTextCompare) > 0 Then

    strWildcard = Replace(rngTable.Text, "-", "[.-]", 1)

    bolWild = True

    End If

    If InStr(1, rngTable.Text, "?", vbTextCompare) > 0 Then

    strWild = strWild + rngTable.Text + Chr$(13)

    strWildcard = Replace(rngTable.Text, "?", "_", 1)


    bolWild = True

    End If

    If (bolWild = False) Then

        Dim oRng As Word.Range

            Set oRng = ActiveDocument.Range

            With oRng.Find

            .ClearFormatting

            .Text = strWildcard

            .MatchAllWordForms = False

            .MatchSoundsLike = False

            .MatchFuzzy = False

            .MatchWildcards = True


            With .Replacement

            .Text = rngTable.Text

            .Font.Bold = True

            .Font.Italic = True

            End With

            .Execute Replace:=wdReplaceAll

    End With

    End If

Next

'    If bolWild = True Then'

'    MsgBox ("Please search the following strings with - or ? manually:" + Chr$(13) + strWild)'

'    End If'

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:41:43+00:00Added an answer on May 12, 2026 at 2:41 pm
    Sub AllBold()
    
    Dim tblOne As Table
    Dim celTable As Cell
    Dim rngTable As Range
    Dim intCount As Integer
    Dim intMatch As Integer
    Dim celColl As Cells
    Dim i As Integer
    Dim strRegex As String
    Dim Match, Matches
    
    Set tblOne = ActiveDocument.Tables(1)
    intCount = tblOne.Columns(2).Cells.Count
    Set celColl = tblOne.Columns(2).Cells
    Set objRegEx = CreateObject("vbscript.regexp")
    objRegEx.Global = True
    objRegEx.IgnoreCase = True
    objRegEx.MultiLine = True
    
    For i = 1 To intCount
        If i = 1 Then
            i = i + 1
        End If
    
        Set celTable = ActiveDocument.Tables(1).Cell(Row:=i, Column:=2)
        Set rngTable = ActiveDocument.Range(Start:=celTable.Range.Start, _
                                            End:=celTable.Range.End - 1)
    
        If rngTable.Text <> "" Then
            strRegex = rngTable.Text
            strRegex = Replace(strRegex, "*-", "[\w]{0,}[^\w]{0,1}[\w]{0,}", 1)
            strRegex = Replace(strRegex, "*", "\w+", 1)
            strRegex = Replace(strRegex, "-", "[^\w]{0,1}", 1)
            strRegex = Replace(strRegex, "?", ".", 1)
            objRegEx.Pattern = "\b" + strRegex + "\b"
    
            Dim oRng As Word.Range
            Set oRng = ActiveDocument.Range
            Set Matches = objRegEx.Execute(ActiveDocument.Range.Text)
    
            intMatch = Matches.Count
            If intMatch >= 1 Then
                rngTable.Bold = True
                For Each Match In Matches
                    With oRng.Find
                        .ClearFormatting
                        .Text = Match.Value
                        With .Replacement
                            .Text = Match.Value
                            .Font.Bold = True
                            .Font.Italic = True
                        End With
    
                        .Execute Replace:=wdReplaceAll
                    End With
                Next Match
            End If
        End If
    Next i
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VBA Word Macro that gets words from .txt list and color
I have written a VBA macro in an excel spreadsheet that opens all workbooks
I'm trying to write a VBA macro in Word that will extract shapes and
I'm working on creating a macro in Microsoft Word (2007) for a document that
I have a VBA macro that uses Microsoft MapPoint to calculate the distance between
Right now, I have a VBA macro for Word which parses a document for
I'm trying to load a file in a VBA macro that has been copied
I’m trying to create a VBA macro that saves an email attachment to folder
I have an Excel file that has a bunch of VBA and macro code
I have a VBA macro that validates user entered data (I didn't use data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.