Re: Macro to replace text that includes field codes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Yahoo - it worked. Thanks, Greg, for this very educational exchange.

Sincerely,
Tara

"Greg Maxey" wrote:

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

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

"Tarallen" <Tarallen@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:D0A9EFAE-09D4-4A62-B6A5-EE7196D2EE0F@xxxxxxxxxxxxxxxx
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

"Greg Maxey" wrote:

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


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Tarallen wrote:
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

"Greg Maxey" wrote:

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.



Tarallen wrote:
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






.



Relevant Pages

  • Re: Find Text, Replace Text and Bold It in a Macro
    ... slight modification of your macro worked perfectly! ... Dim oRng As Word.Range ... "Greg Maxey" wrote: ... Trying to create a macro through keystrokes that does this: ...
    (microsoft.public.word.vba.general)
  • Re: Macro to replace text that includes field codes
    ... Dim oRng As Range ... I had success with your macro, ... When field code is diplayed, ... Using the recorder is alway chancey at best. ...
    (microsoft.public.word.vba.beginners)
  • Re: Calling a macro
    ... Greg, thanks so much. ... I have a range called noticeplus, A macro named PrintNOH and I want to call ... instance the word TEST on a line by itself doesn't make sense to me. ... Dim oRng As Word.Range ...
    (microsoft.public.word.vba.general)
  • Re: Calling a macro
    ... Greg, for some reason, this has just confused me. ... I have a range called noticeplus, A macro named PrintNOH and I want to call ... instance the word TEST on a line by itself doesn't make sense to me. ... Dim oRng As Word.Range ...
    (microsoft.public.word.vba.general)
  • Re: How do I delete all words that are mispelled in a document?
    ... Thanks Greg for the rapid response. ... I open a target file manually in Word 2007 and launch the macro. ... Dim myErrors As ProofreadingErrors ...
    (microsoft.public.word.docmanagement)