Consulting

Results 1 to 7 of 7

Thread: Solved: Help with Search and Replace faulty code

  1. #1
    VBAX Regular
    Joined
    Nov 2012
    Posts
    37
    Location

    Solved: Help with Search and Replace faulty code

    Hi,

    I'm running this code without success:

    [vba]
    Sub RemoveCaptionFields()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .text = "Figure ^d STYLEREF 1 \s ^21-^d SEQ Figure \* ARABIC \s 1 ^21: "
    .Replacement.text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With
    End Sub
    [/vba]
    What I'm trying to do is remove all the "Figure { chapter number }-{ page number }: " in the figure captions of a document.

    The funny thing is that if I use Word's Search and Replace function to look for that string, it finds it. But if I click "Replace all" (using an empty Replace field), it says it could not find a single occurrence of the string.

    Any help will be highly appreciated!

    Daniel
    Last edited by bcn; 07-02-2013 at 05:33 AM.

  2. #2

    Smile

    Hi Daniel, try this:

    Sub RemoveCaptionFields()
    ActiveDocument.Fields.Unlink
    End Sub

    Regards.
    Ailton

  3. #3
    VBAX Regular
    Joined
    Nov 2012
    Posts
    37
    Location
    Hi Ailton,

    Thanks for your answer.
    I don't want to remove the whole caption. I just want to remove everything except the caption's text. So, in the following example:

    Figure 3-23: Installing the software

    I just want to remove the part in red.
    (including the space after the colon)

    I think my macro is not working well because of the field special characters. If I try it with normal text, it does the trick. But with the special characters it finds the first occurrence of the string and then it says there are no more.

    Regards,

    Daniel



    Quote Originally Posted by ayltonasc
    Hi Daniel, try this:

    Sub RemoveCaptionFields()
    ActiveDocument.Fields.Unlink
    End Sub

    Regards.
    Ailton

  4. #4
    Daniel, try this code:

    [VBA]
    Sub RemoveCaptionFields()

    ' the line below is need to transform field in plain text, so you have no more problem with your search.

    ActiveDocument.Fields.Unlink

    'this search Figure with two digits, for example: Figure 2: Installing the software
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Figure ^#: "
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With

    'this search Figure with two digits, for example: Figure 13: Installing the software
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Figure ^#^#: "
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    End With

    End Sub
    [/VBA]

  5. #5
    VBAX Regular
    Joined
    Nov 2012
    Posts
    37
    Location
    Thank you very much, Ayltonasc. Highy appreciated!!!

  6. #6
    Quote Originally Posted by bcn
    Thank you very much, Ayltonasc. Highy appreciated!!!
    cool man
    ;-)

  7. #7

    Smile

    cool man
    Regards.

    Ailton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •