Macro Recording Process Acieved Macro Goal, Macro Itself Fails

A

ArthurOfAlbion

I recorded a macro as I simultaneously (1) searched for all paragraphs of
outline level 8 and beginning with the text ‘i.’ followed by a tab character
and (2) replaced each instance of the above text with ‘[a]’ followed by a tab
character, and applied various formatting characteristics to each paragraph
found (including outline level 9). I was successful during the recording
process, but the resulting macro doesn’t work. Instead, I am informed that
‘0 replacements were made’. (Of course, I had restored the search target to
the text.) The code, recorded automatically, is as follows:

Sub i__DemotedByJAW()
'
' i__DemotedByJAW Macro
' Macro recorded 11/23/2007 by James Watson
'
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.OutlineLevel = wdOutlineLevel8
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.LeftIndent = InchesToPoints(1.75)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevel9
.CharacterUnitLeftIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "i.^t"
.Replacement.Text = "[a]^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Many thanks in advance for whomever assists me.

James Watson
 
R

Russ

Arthur,
After running this macro, open the menu Edit/Replace...
The dialog box should reflect the criteria that your macro setup for
searching, since you are using a selection.find rather than a range.find.
When everything looks good, then click on find next to test criteria again.
I recorded a macro as I simultaneously (1) searched for all paragraphs of
outline level 8 and beginning with the text Œi.¹ followed by a tab character
and (2) replaced each instance of the above text with Œ[a]¹ followed by a tab
character, and applied various formatting characteristics to each paragraph
found (including outline level 9). I was successful during the recording
process, but the resulting macro doesn¹t work. Instead, I am informed that
Œ0 replacements were made¹. (Of course, I had restored the search target to
the text.) The code, recorded automatically, is as follows:

Sub i__DemotedByJAW()
'
' i__DemotedByJAW Macro
' Macro recorded 11/23/2007 by James Watson
'
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.OutlineLevel = wdOutlineLevel8
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.LeftIndent = InchesToPoints(1.75)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevel9
.CharacterUnitLeftIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "i.^t"
.Replacement.Text = "[a]^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Many thanks in advance for whomever assists me.

James Watson
 
A

ArthurOfAlbion

Russ said:
Arthur,
After running this macro, open the menu Edit/Replace...
The dialog box should reflect the criteria that your macro setup for
searching, since you are using a selection.find rather than a range.find.

Hello, Russ. Thanks for examining my problem.

I think that your advice has revealed an important clue. In the 'Find what'
field, after running the macro, I find the correct text. But underneath the
text I read (after 'Options: Search Down') 'Format: Left, Level 8, Border:
Top: (Single solid line, Auto, 0.75 pt Line width), B...'. Now, when I
recorded the macro (and when the operation I intend for the macro took place)
I specified nothing pertaining to border formatting.
When everything looks good, then click on find next to test criteria again.

Once I specified all the paragraph formatting details, the line I quoted
above had changed to 'Format: Indent: Left: 1.5", Left Line spacing: single,
Space Before: 0 pt, After:...'. This information accurately describes the
formatting of the target paragraph. Nevertheless, when I searched, nothing
was found. (Note that when I originally recorded the macro, and when the
macro's purpose was achieved for the first and only time, the only formatting
I specified was that the outline level should be 8.)
I recorded a macro as I simultaneously (1) searched for all paragraphs of
outline level 8 and beginning with the text Œi.¹ followed by a tab character
and (2) replaced each instance of the above text with Œ[a]¹ followed by a tab
character, and applied various formatting characteristics to each paragraph
found (including outline level 9). I was successful during the recording
process, but the resulting macro doesn¹t work. Instead, I am informed that
Œ0 replacements were made¹. (Of course, I had restored the search target to
the text.) The code, recorded automatically, is as follows:

Sub i__DemotedByJAW()
'
' i__DemotedByJAW Macro
' Macro recorded 11/23/2007 by James Watson
'
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.OutlineLevel = wdOutlineLevel8
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.LeftIndent = InchesToPoints(1.75)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevel9
.CharacterUnitLeftIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "i.^t"
.Replacement.Text = "[a]^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Many thanks in advance for whomever assists me.

James Watson
 
R

Russ

A couple of more suggestions below:
Hello, Russ. Thanks for examining my problem.

I think that your advice has revealed an important clue. In the 'Find what'
field, after running the macro, I find the correct text. But underneath the
text I read (after 'Options: Search Down')

If your cursor is already past any potential text that meets the criteria
and you set things up to Search Down, then you won't find any text from
where your cursor is to the end of the document. Usually you want to search
all the document or make sure the cursor is at the top of the document.
(Searching will only be in the main body of document and not in the headers
or footers, unless your cursor is already in a header or footer. You have to
resort to VBA code to iterate through and search all parts of document,
other than the main body text.)
'Format: Left, Level 8, Border:
Top: (Single solid line, Auto, 0.75 pt Line width), B...'. Now, when I
recorded the macro (and when the operation I intend for the macro took place)
I specified nothing pertaining to border formatting.


Once I specified all the paragraph formatting details, the line I quoted
above had changed to 'Format: Indent: Left: 1.5", Left Line spacing: single,
Space Before: 0 pt, After:...'. This information accurately describes the
formatting of the target paragraph. Nevertheless, when I searched, nothing
was found. (Note that when I originally recorded the macro, and when the
macro's purpose was achieved for the first and only time, the only formatting
I specified was that the outline level should be 8.)

Only specify the minimum criteria to find your text. Outline level 8 already
assumes certain formatting, you may be also specifying formatting that is
working at cross purposes from what Outline level 8 uses, and thus, the
combination of formatting to search for is impossible to find.

I recorded a macro as I simultaneously (1) searched for all paragraphs of
outline level 8 and beginning with the text Œi.¹ followed by a tab character
and (2) replaced each instance of the above text with Œ[a]¹ followed by a
tab
character, and applied various formatting characteristics to each paragraph
found (including outline level 9). I was successful during the recording
process, but the resulting macro doesn¹t work. Instead, I am informed that
Œ0 replacements were made¹. (Of course, I had restored the search target to
the text.) The code, recorded automatically, is as follows:

Sub i__DemotedByJAW()
'
' i__DemotedByJAW Macro
' Macro recorded 11/23/2007 by James Watson
'
Selection.Find.ClearFormatting
With Selection.Find.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.OutlineLevel = wdOutlineLevel8
End With
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.LeftIndent = InchesToPoints(1.75)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevel9
.CharacterUnitLeftIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "i.^t"
.Replacement.Text = "[a]^t"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Many thanks in advance for whomever assists me.

James Watson
 
A

ArthurOfAlbion

Russ said:
Only specify the minimum criteria to find your text. Outline level 8 already
assumes certain formatting, you may be also specifying formatting that is
working at cross purposes from what Outline level 8 uses, and thus, the
combination of formatting to search for is impossible to find.

This is the heart of the matter. Once I began minimising my search
criteria, I discovered that even certain (perhaps all) left margin settings
are associated with other features (e.g., the presence of borders)! This
seems very much like bad design to me, but maybe there's a reason for it. I
ended up searching only for the characters corresponding to a given
paragraph's outline level (e.g., 'IV.^t').

Besides wondering why one cannot use a macro to search for individual
formatting criteria in isolation, I also wonder why the search I used for the
initial recording of the macro, which used the 'Outline level' formatting
criterion, actually did find the paragraphs with the correct outline level,
even though those paragraphs did not have the other characteristics
associated with that outline level.

Thank you again for solving my problem.

James Watson
 

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

Similar Threads


Top