Link to home
Start Free TrialLog in
Avatar of Amzad Mohammad
Amzad MohammadFlag for India

asked on

Changing font style in specific texts using macro?

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.
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

In the search and replace dialogue in MS Word you can specify formats for both search and replace criteria.
Avatar of Amzad Mohammad

ASKER

Thanks Rob,

But I need a macro.
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

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.
We also need to highlight the changes with yellow color.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial