Find and replace other than given style

Mohamed iliyas
Mohamed iliyas used Ask the Experts™
on
Dear All,
This is Iliyas, I have this query, i.e., in word document I have style named 'F-Caption' and other styles. I need to find and replace other than the style 'F-Caption' (should not find in the text with style 'F-Caption'). I'm looking for your quick reply. Thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

Check this code
Sub FindAndChangeStyle()
Dim i As Paragraph
'Find Normal style and format it to be Heading 1 style
  For Each i In ActiveDocument.Paragraphs
        If i.Style.NameLocal = "Normal" And Len(i.Range.Text) > 1 Then
            i.Range.Style = ActiveDocument.Styles("Heading 1")

        End If
    Next
   
End Sub

Author

Commented:
Thank you for your effort, but I don't want to replace the style instead I want to replace text
Hi,
Share sample of the word file and let me know what do you need to replace with what?
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
Hi
In the attached document, should replace "p." into "S." except the text with style "Float-Caption".
Thanks in advance.
329063_0_En_31-1_Chapter.DOCX
Top Expert 2016

Commented:
Hi,

pls try
Sub Macro()
    Dim r As Range
    Set r = ActiveDocument.Range
    With r.Find
        Do While .Execute(Findtext:="p.", Forward:=True) = True
            If r.Style.NameLocal <> "Float-Caption" Then
                r.Text = "S."
                r.Collapse 0
            End If
        Loop
    End With

End Sub

Open in new window

Regards

Author

Commented:
Tried but loops continuously
Top Expert 2016
Commented:
then try
Sub Macro()
    Dim r As Range
    
    Set r = ActiveDocument.Range
    Selection.EndKey Unit:=wdStory, Extend:=wdMove
    With r.Find
        Do While .Execute(Findtext:="p.", Forward:=False) = True
            If r.Style.NameLocal <> "Float-Caption" Then
                r.Text = "S."
                r.Collapse 0
            End If
        Loop
    End With

End Sub

Open in new window

Author

Commented:
Wow thanks, it does the trick.
Top Expert 2016

Commented:
Solution according to author's comment

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