Macro to replace text that includes field codes

T

Tarallen

Hello,

I created a macro that would remove the word "Figure," the figure number,
and the period that follows it in a caption. I just want the caption without
"Figure #-#" before it. The problem is, obviously, that the number is a field
code, not a fixed number.

I tried recording a macro that searched for "Figure ^#-^#. " and replaced it
with nothing. It worked fine when I practiced it manually using the Find and
Replace feature in Word. However, when I tried to record it, suddenly, it
would not find any instances of "Figure ^#-^#. "

Despite the difficulty with recording it the way I wanted it, I modified the
macro to accomplish this, but it is still not working. So, my guess is it's
something to do with the field codes. But why would it work manually and not
as part of the macro? And how do I fix it? Thanks, in advance, for any help
you can give me.

The macro is below.

Sub RemoveFigure()
'
' RemoveFigure Macro
' Macro recorded 11/20/2006 by TAllen.
' "&chr(10)&"Removes all instances of "Figure," the figure number and the
period and space before the text.
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Figure ^#-^#. "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
G

Greg Maxey

Finding with VBA is a bit more complex than finding with the UI.

However, a simple example and not very robust, you could use:

Sub ScratchMacro()
Dim oRng As Range
ActiveDocument.Fields.ToggleShowCodes
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Figure ^d."
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
ActiveDocument.Fields.ToggleShowCodes
End Sub

Prior to running the code, ensure that field results are displayed and
not the codes. It will search the main text of the document only. If
you need to search in headers or footers, drawing layer, etc. then post
back.
 
T

Tarallen

Thanks Greg.

I tried this and it did not work. The document just appeared to remain
unchanged but no error message was displayed.

I guess my biggest question is - why can't I use the UI for this type of
find-replace. It seems simple enough. Why does the Macro recorder refuse to
work when I try to perform this action? And, why doesn't the VB code I used
work?

Thanks,
Tara
 
G

Greg Maxey

Tara

I don' t know if I can provide a suitable answer to all of your questions.

First you are right. The method I proposed will not solve your problem. I
tested with a simple find for Figure { Seq Figure }. You need to find the
more complex string of Figure { StyleRed 1 \s }-{ Seq Figure }.

When field code is diplayed, ^d finds the field code. However, and I just
learned this myself, a find seems to fall apart if there is text adjacent to
the the ^d. For example, Find "Figure ^d" will find "Figure { Seq
Figure }" but Find "Figure ^d." will not find "Figure { Seq Figure }."

By adding the "." adjacent to the ^d seems to blow it apart.

For that reason, Find"Figure ^d-^d." doesn't work as is seems it should.

Using the recorder is alway chancey at best. See:
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm

Your code wouldn't find "Figure 1-1." because despite what shows on the
screen, what is actually there is "Figure { StyleRef 1 \s }-{ Seq Figure }

Base on the the issue above, what we will have to do is use a wild card
seard to find "Figure<and everything up to>." Then analyse that found range
to see if it has a field containing the string STYLEREF. So try the below
macro on a copy of your document and see what result you get:

Sub Scratchmacro()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Figure*."
.MatchWildcards = True
While .Execute
oRng.Select
If InStr(oRng.Fields(1).Code, "STYLEREF") > 1 Then
oRng.Text = ""
End If
Wend
End With
End Sub

D
 
J

Jezebel

When you search for a field, you can include the field code after the ^d, if
you want to be precise about which particular fields you want to match. Eg,
use "^d Seq" to match SEQ fields only.

To match the field plus what follows, you need to spell it out, eg to match
"Figure { Seq Figure }." use

Figure ^d Seq Figure ^?.

The '^?' matches the closing brace of the field. The text between ^d and ^?
must be exactly the field code, spaces and all. To match "Figure { StyleRef
1 \s }-{ Seq Figure }" use

Figure ^d StyleRef 1 \s ^?.^d
 
G

Greg Maxey

Jezebel,

Thanks for the explanation of ^?. However, I still can't find the complete
string the OP is looking for.

Using "Figure 1-1." as the string to find and replace with nothing. (Note
this includes the period)

Find: "Figure ^d STYLEREF 1 \s ^?-^d SEQ Figure \* ARABIC \s 1^?" (No
period)

Finds "Figure 1-1"

but

Find: "Figure ^d STYLEREF 1 \s ^?-^d SEQ Figure \* ARABIC \s 1^?." (Has an
ending period)

Finds nothing!
 
J

Jezebel

I think it will be a subtlety of spaces at the end of the second field, eg
between the \1 and the ^? -- try one or more inserting additional ^?s before
the period.
 
T

Tarallen

Hi Greg (and Jezebel),

Thank you both so much for your help.

I am out of my depth here, having only recently begun working with macros
and the macro recorder. VBA is still very new to me but I am learning.

I had success with your macro, Greg. I was also able to modify it in a
couple of ways to allow for some other nuances in my needs. There is only one
remaining issue that I have spent some time trying to address with my limited
knowledge but to no avail. The macro has to replace only caption-style text,
not body text or any other style text. I tried to insert a line from a
recently recorded find-replace macro I created: 'Selection.Find.Style =
ActiveDocument.Styles("Caption")' but, no matter where I put it in the macro,
it is not working. It does not stop the macro or make it malfunction. Can you
please help me figure out where this goes or if there's another change I can
make?

Jezebel - I am going to also experiment with your suggestions for future
reference and I thank you sincerely for your input.

Thank you!

Tara
 
G

Greg Maxey

Try:

Sub Scratchmacro()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Figure*."
.Style = "Caption"
.MatchWildcards = True
While .Execute
oRng.Select
On Error Resume Next
If InStr(oRng.Fields(1).Code, "STYLEREF") > 1 Then
oRng.Text = ""
End If
On Error GoTo 0
Wend
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top