Macro to delete text set to particular style

D

Dawn Rhoads

With the help of past posts on this newsgroup (thank you!) I am working on a
macro that will find all text set in a particular style, and then delete it.
(BTW, I am still just a record-a-macro person, but I can sometimes cobble
together other people's code to do more.)

The problem I am having is that if any one of the three styles is not
present in the document, the macro fails on the "selection.find.style" line
of code and tells me it can't find the requested member. I guess I'm looking
for a way to make the macro "skip" the section when there is no such style in
the document?

I found "error handling" code from other newsgroup posts that I stuck in
that seems like it might be the right idea, but right now, it just pops up a
message box saying what the problem is. I would like to make sure if some
other error besides the missing style comes up, that the error will still
show up, since goodness knows what other problems I might run into later! :)

Any thoughts are appreciated!


On Error GoTo ErrHandler
With Selection.Find
.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Hidden")
Text = ""
With .Replacement
.ClearFormatting
.Text = ""
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
With Selection.Find
.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Hidden Text Instruction")
Text = ""
With .Replacement
.ClearFormatting
.Text = ""
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
With Selection.Find
.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Hidden Text Instruction
Char")
Text = ""
With .Replacement
.ClearFormatting
.Text = ""
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With

Exit Sub

ErrHandler:
If Err.Number = 5941 Then
MsgBox "Requested style is not in the document!"
Else
MsgBox ("Error " & CStr(Err.Number) & ": " & Error)

End If

End Sub
 
H

Helmut Weber

Hi Dawn,

tested for a paragraph style:

Sub Macro1()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
resetsearch
With rDcm.Find
.Style = "Heading 3"
While .Execute
rDcm.Delete
Wend
End With
resetsearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Dawn Rhoads

Thanks Helmut -- Hmm...I tried to use this, but must be doing something
wrong. If I copy and past this as-is (I changed the style name to "hidden")
it seems to go into some kind of infinite loop and I have to force quit. I
also am not sure how to alter the code so that it looks for three different
styles in the same document.

Thanks for any further help you can offer, appreciate it!
 
H

Helmut Weber

Hi Dawn,
I tried to use this, but must be doing something wrong.

Either You or me or MS. ;-)

You get the error,
if the style name is not in the list of the style's names.

Sub Macro1()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
ResetSearch
On Error Resume Next
With rDcm.Find
.Style = "xxxxx"
While .Execute
rDcm.Delete
Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

HTH

Different style names would not be a problem,
as long as there is some regularity in their names.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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