Changing font style in specific texts using macro?

Amzad Mohammad
Amzad Mohammad used Ask the Experts™
on
I was wondering that is it possible find specific text with certain style applied in it and convert with another text and style?

For example:
i) If we find "5" text with font MathematicalPi-One applied in it than it should be converted into "=" with style Arial Unicode MS. And highlight the changes.

ii) And we have a list of items which we have to convert with them, than what will be the shortest and fastest macro to do the same?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rob HensonFinance Analyst

Commented:
In the search and replace dialogue in MS Word you can specify formats for both search and replace criteria.
Amzad MohammadSoftware Professional

Author

Commented:
Thanks Rob,

But I need a macro.
GrahamSkanRetired
Top Expert 2012

Commented:
This macro pair shows how to do it. It assumes that you will hard-code the arguments one be one in the first macro. You might already have them in a file or in a Word table. In such a case you might want modify the first macro to step through the file or table.

Option Explicit

Sub CallAutoReplace()
    Dim i As Integer
    Dim strArguments(3, 0) As String ' second index needs to be edited to suit
    
    ' set up argument array
    'First set
    strArguments(0, 0) = "5"
    strArguments(1, 0) = "MathematicalPi-One"
    strArguments(2, 0) = "="
    strArguments(3, 0) = "Arial Unicode MS"
    'Second set
    'strArguments(0, 1) = "5"
    'strArguments(1, 1) = "MathematicalPi-One"
    'strArguments(2, 1) = "="
    'strArguments(3, 1) = "Arial Unicode MS"
    '...
    
    For i = 0 To UBound(strArguments, 2)
        AutoReplace ActiveDocument.Range, strArguments(0, i), strArguments(1, i), strArguments(2, i), strArguments(3, i)
    Next i
End Sub

Sub AutoReplace(rng As Range, strOldText As String, strOldFont As String, strNewText As String, strNewFont As String)
    With rng.Find
        .Text = strOldText
        .Font.Name = strOldFont
        With .Replacement
            .Text = strNewText
            .Font.Name = strNewFont
        End With
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Amzad MohammadSoftware Professional

Author

Commented:
Fabulous,

Nice work as usual.

GrahamSkan, Will it be effected if the number of arguments comes in thousands.

And, what line will be change if the Replacing font will remain same "Arial Unicode MS" for all text.

Thanks a on in advance.
Amzad MohammadSoftware Professional

Author

Commented:
We also need to highlight the changes with yellow color.
Retired
Top Expert 2012
Commented:
As I indicated, if there are many variations, you would want to automate access to the the data. You are a developer, so you know that how that is done will depend on how the data is held.

If the font name is always the same, it can either be passed into the AutoReplace macro each time or that macro can be modified to ignore the font name.

Sub AutoReplace(rng As Range, strOldText As String, strOldFont As String, strNewText As String, strNewFont As String)
    rng.HighlightColorIndex = wdYellow
    With rng.Find
        .Text = strOldText
        '.Font.Name = strOldFont
        With .Replacement
            .Text = strNewText
            '.Font.Name = strNewFont
        End With
        .Execute Replace:=wdReplaceAll
        .Highlight = True
    End With
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial