Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro does not work

Status
Not open for further replies.

scauser

Technical User
Aug 9, 2002
54
US
I am trying to create a macro that will delete text that is formatted with a certain Word Style. I created the following macro, which is supposed to replace text with the format style "RedText" with the format style "Normal". The actions work when I am recording the macro, but nothing happens when I run the macro.

Sub RedText()
'
' RedText Macro
' Macro recorded 7/26/2005 by
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Redtext")
Selection.Find.ParagraphFormat.Borders.Shadow = False
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Normal")
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

Any help of this would greatly be appreciated...!
 
Code:
Sub ReplaceStyle()
Dim oStyle As Word.Style
Set oStyle = ActiveDocument.Styles("Redtext")
Selection.HomeKey unit:=wdStory
With Selection.Find
   .Style = oStyle
   Do While (.Execute(Forward:=True, _
         Wrap:=wdFindContinue) = True) = True   
      Selection.Style = "Normal"
   Loop
End With
Set oStyle = Nothing
End Sub

This replaces all instances of the style Redtext with Normal.

HOWEVER...comment: Normal style....no. no. no! Normal style? BAD dog, bad. Proper use of styles in Word means never using Normal style. Ever.

Gerry
 
Thanks Fumei, but your code did not work either! However, it did place the cursor at the top of the doc, so it must have done something. Hhhhmmmmmm.....

In the Find/Replace box, I actually placed a space in the "Replace with" text box to replace the "Redtext" formatted text with a space. The interesting thing is that i used this macro in a former life (and probably former Word version), but cannot get it to work now.
 
OK, please state EXACTLY what you did, plus - what version are you using?

The code works perfectly for me. I created a style named Redtext (the font color is red...duh); made a bunch of paragraphs Redtext style; ran the code and it replaced every instance of Redtext style with normal. I am using Word 2002, and it works fine. So tell me exactly what you did.

WHERE did you place the code?

In particular...you mention the Find/Replace box. If you put this procedure into the ThisDocument code module you can run it from there. It does not use the Find/Replace dialog box at all. So I am not quite getting what you did.

Gerry
 
Search the Word VBA help file for NextStoryRange and look at the last example.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

When recording, I was doing a Replace of all instances of text with "Redtext" style with a space witht he style "Normal". I inserted a space in the Replace With text box so the text would be deleted (this is guideline text in a user template).

I finally got it to work just by deleting what appeared to be extraneous code (which was good, because i was screwing around with this thing for two days and was ready to lose my freakin mind).

Sub RedText()
'
' RedText Macro
' Macro recorded 7/26/2005 by
'

Selection.Find.Style = ActiveDocument.Styles("Redtext")


Selection.Find.Replacement.Style = ActiveDocument.Styles("Normal")
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


Thanks Everyone!
 
Actually, I forgot a line. If you try this it should work.
Code:
Sub ReplaceStyle()
Dim oStyle As Word.Style
Set oStyle = ActiveDocument.Styles("Redtext")
Selection.HomeKey unit:=wdStory
With Selection.Find
[COLOR=red]   .ClearFormatting[/color red]
   .Style = oStyle
   Do While (.Execute(Forward:=True, _
         Wrap:=wdFindContinue) = True) = True   
      Selection.Style = "Normal"
   Loop
End With
Set oStyle = Nothing
End Sub

PHV's suggestion will help if you are searching across multiple Stories....are you?

But try using the amended code. I think it should work and it more efficient.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top