Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Macro to delete text set to particular style

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dawn Rhoads - 22 Nov 2005 21:16 GMT
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
Helmut Weber - 22 Nov 2005 21:40 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Dawn Rhoads - 22 Nov 2005 22:01 GMT
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!

> Hi Dawn,
>
[quoted text clipped - 32 lines]
>
> HTH
Helmut Weber - 22 Nov 2005 22:36 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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



Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.