Can't record a macro which contains a replace of a style

D

doonkhan

I'm using Word 2007. I'm trying to convert a document into a plain
text format without losing style information. I'd like to insert the
style name in text at the beginning of each paragraph tagged with that
style.

This should be easy, right?

I have created a macro using the macro recorder, then done a search
operation, which searches for the style (the style is named "action").
The "Find what" box is empty - it's just the style. "Replace with" is
set to [action]^& - ie, the style name, followed by the "Find what"
text.

It works fine as a replace operation, but if I record it with the
Macro recorder, then play it back, it does nothing.

I used to use this technique quite a bit in older versions of Word,
and I can't figure out why it no longer works. Has something changed
in Word 2007? Is there some setting I may have overlooked?

Duncan McKenzie
(e-mail address removed)
 
N

Neil Cumfer

I tried this in Word 2007 and it seems to work fine.
(I only searched for one style, a paragraph style).

I suspect you might have edited the code generated
by the macro recorder, and deleted too many lines.

These are the critical statements:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Style = ActiveDocument.Styles("action")
.Replacement.Text = "[action]^&"
.Format = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

..
 
D

doonkhan

Thanks so much for the quick response, Neil!

No, I haven't touched the code created by the macro recorder. I just
record it as a new macro each time, and try to run it. The replace
operation works when I record it, but when I try to repeat it by
running the macro, it runs, but does nothing.

The code you posted works perfectly, but the code created by the macro
recorder adds a number of odd formatting lines, and doesn't work. (See
below.) Any idea what's going on?

Duncan

Here's what the Macro Recorder creates when I record the replace
operation:

Sub Convert2()
'
' Convert2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Action")
With Selection.Find.ParagraphFormat
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorBlack
.BackgroundPatternColor = wdColorBlack
End With
.Borders.Shadow = False
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[Action]^&"
.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



I tried this in Word 2007 and it seems to work fine.
(I only searched for one style, a paragraph style).

I suspect you might have edited the code generated
by the macro recorder, and deleted too many lines.

These are the critical statements:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = ""
    .Style = ActiveDocument.Styles("action")
    .Replacement.Text = "[action]^&"
    .Format = True
    .Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll


.
I'm using Word 2007. I'm trying to convert a document into a plain
text format without losing style information. I'd like to insert the
style name in text at the beginning of each paragraph tagged with that
style.
This should be easy, right?
I have created a macro using the macro recorder, then done a search
operation, which searches for the style (the style is named "action").
The "Find what" box is empty - it's just the style. "Replace with" is
set to [action]^& - ie, the style name, followed by the "Find what"
text.
It works fine as a replace operation, but if I record it with the
Macro recorder, then play it back, it does nothing.
I used to use this technique quite a bit in older versions of Word,
and I can't figure out why it no longer works. Has something changed
in Word 2007? Is there some setting I may have overlooked?
Duncan McKenzie
(e-mail address removed)
 
N

Neil Cumfer

No, I haven't touched the code created by the macro recorder.

Oh, well in that case, maybe you didn't delete enough lines ... LOL

Your problem was not really a bug in the macro recorder, or
even a feature, but simply a limitation on what the recorder
can do.

It can record your keystrokes and mouse clicks when you are
editing a document, but not when you are using a dialog box.
What is does instead is to check, after you dismiss the dialog
box, all the settings that were available for you to change
while the dialog box was open, and then it inserts VBA
statements for all those settings, reflecting their state when
you closed the dialog box. So after you finish recording a
macro, you will need to edit the code by deleting the
settings that you didn't change.

You might have to make additional changes to the
recorder's code as well. In this case, I moved this line:
Selection.Find.Style = ActiveDocument.Styles("Action")
which the recorder placed before the With block, so that
it is including inside the With block.

It is always a good idea, after recording a macro, to undo
the changes that you made to the document which you were
recording, and then play back the macro to be sure it will
really work.

In addition to dialog boxes, you will find that many other macros
need to be edited before they will work, particularly if you access
any of the special or advanced features in Word, such as headers
and footers, styles, text boxes, graphics, field codes etc.

The macro recorder does work well with simple editing tasks,
and is useful for that reason. For more complex macros, it
is useful mainly because it reveals the VBA objects and
properties that you need to learn how to manipulate yourself
so you can write a macro that will work as intended when
you play it back.

..
Thanks so much for the quick response, Neil!

No, I haven't touched the code created by the macro recorder. I just
record it as a new macro each time, and try to run it. The replace
operation works when I record it, but when I try to repeat it by
running the macro, it runs, but does nothing.

The code you posted works perfectly, but the code created by the macro
recorder adds a number of odd formatting lines, and doesn't work. (See
below.) Any idea what's going on?

Duncan

Here's what the Macro Recorder creates when I record the replace
operation:

Sub Convert2()
'
' Convert2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Action")
With Selection.Find.ParagraphFormat
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorBlack
.BackgroundPatternColor = wdColorBlack
End With
.Borders.Shadow = False
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "[Action]^&"
.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



I tried this in Word 2007 and it seems to work fine.
(I only searched for one style, a paragraph style).

I suspect you might have edited the code generated
by the macro recorder, and deleted too many lines.

These are the critical statements:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Style = ActiveDocument.Styles("action")
.Replacement.Text = "[action]^&"
.Format = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll


news:0b19bb5b-76a2-44fa-9a37-620be44db1f5@t54g2000hsg.googlegroups.com..
.

I'm using Word 2007. I'm trying to convert a document into a plain
text format without losing style information. I'd like to insert the
style name in text at the beginning of each paragraph tagged with that
style.
This should be easy, right?
I have created a macro using the macro recorder, then done a search
operation, which searches for the style (the style is named "action").
The "Find what" box is empty - it's just the style. "Replace with" is
set to [action]^& - ie, the style name, followed by the "Find what"
text.
It works fine as a replace operation, but if I record it with the
Macro recorder, then play it back, it does nothing.
I used to use this technique quite a bit in older versions of Word,
and I can't figure out why it no longer works. Has something changed
in Word 2007? Is there some setting I may have overlooked?
Duncan McKenzie
(e-mail address removed)
 

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