Problem executing recorded style change macro - nothing happens.

K

k2000s

Hi,

New to VBA (today)! Recorded 2 macros: T1 find/replace text and CS1
find/replace style. Both runs fine during After recording and running tools >
macro > macros > xx > run, only T1 works but CS1 does nothing.

What step am I missing or are there restrictions on changing styles with
a macro?

- k2ks

Sub T1()
'
' T1 Macro
' Macro recorded 7/17/2008
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Hello"
.Replacement.Text = "Hello2"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub CS1()
'
' CS1 Macro
' Macro recorded 7/17/2008
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading2")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 2")
Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
D

Doug Robbins - Word MVP

What are you trying to do with CS1?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Your code is replacing Heading2 with Heading2

Selection.Find.Style = ActiveDocument.Styles("Heading2")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 2")

which is why it does not appear to work, though I would have to admit, doing
that is about as simple an operation as you can perform.

What do you really want it to do?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

k2000s

Yes code is to replace one style with another (Heading2 with Heading 2 being
one of many), and to say this is the reason why it does not appear to work is
not helpful to me at all!

Anyways, Found out the problem is with Selection works find during macro
recording but not subsequent macro run. Now aware (still surprised) that
generated macros can be buggy and managed to fix problem using ActiveDocument
instead of Selection.

Thanks regardless,
- k2ks
 
F

fumei via OfficeKB.com

Not sure what you mean by "using ActiveDocument instead of Selection."

You can not use ActiveDocument.Find...as there is no such thing.

It is better to use Range instead of Selection. Is this what you used?

Recorded macros ONLY use Selection. If you are going to use Find a lot,
again, it is better to use a Range object.

It could be that your recorded macro did not work the way you were expecting
is because of:

.Forward = True

Depending on where you have the Selection this may affect things. Again,
another reason to use Range, rather than Selection.
Yes code is to replace one style with another (Heading2 with Heading 2 being
one of many), and to say this is the reason why it does not appear to work is
not helpful to me at all!

Anyways, Found out the problem is with Selection works find during macro
recording but not subsequent macro run. Now aware (still surprised) that
generated macros can be buggy and managed to fix problem using ActiveDocument
instead of Selection.

Thanks regardless,
- k2ks
Your code is replacing Heading2 with Heading2
[quoted text clipped - 11 lines]
 
K

k2000s

Yes Fumei. I used ActiveDocument.Range.Find.

Do you mean Selection with .Forward = False will work?

Thanks
- k2ks

:

....
 
D

Doug Robbins - Word MVP

Sorry, I did not pickup the difference between Heading2 and Heading 2

Try

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading2")
With Selection.Find
Do While .Execute(Findtext:="", Forward:=True, MatchWildcards:=False,
Wrap:=wdFindStop) = True
Selection.Paragraphs(1).Style = "Heading 2"
Loop
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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