PDA

View Full Version : Solved: Help with Search and Replace faulty code



bcn
07-02-2013, 05:01 AM
Hi,

I'm running this code without success:


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

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

ayltonasc
07-02-2013, 04:01 PM
Hi Daniel, try this:

Sub RemoveCaptionFields()
ActiveDocument.Fields.Unlink
End Sub

Regards.
Ailton

bcn
07-02-2013, 11:43 PM
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




Hi Daniel, try this:

Sub RemoveCaptionFields()
ActiveDocument.Fields.Unlink
End Sub

Regards.
Ailton

ayltonasc
07-03-2013, 01:14 PM
Daniel, try this code:


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

bcn
07-05-2013, 04:40 AM
Thank you very much, Ayltonasc. Highy appreciated!!!

ayltonasc
07-05-2013, 06:20 AM
Thank you very much, Ayltonasc. Highy appreciated!!!

cool man
;-)

ayltonasc
07-05-2013, 06:21 AM
cool man
Regards.

Ailton