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 / December 2004

Tip: Looking for answers? Try searching our database.

Paragraph deletion based on style

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ilya.ayzenshtok@gmail.com - 11 Dec 2004 04:36 GMT
Hello,

I have a document with the following repeating structure:

- Paragraph with Style A
- Paragraph with Style B
- Paragraph with Style C

The thing is that the paragraphs with Style B are essentially a blank
line dividers between the other two paragraphs. I would like to be able
to search through the document and delete those extra blank lines, but
only if they are of Style B and are surrounded by Style A and Style B
paragraphs.

I suspect I'm getting at least into VBA, with possibly some regex
involved.

Could anyone please give me a pointer on this?
Thanks a lot.
Ilya Ayzenshtok.
Helmut Weber - 11 Dec 2004 11:14 GMT
Hi Ilya,
have a look at this one:

Sub Test444()
Dim rTmp As Range
Dim rDcm As Range
Dim iPrg As Integer
Set rDcm = ActiveDocument.Range
With rDcm.Find
  .Style = "PStyle B"
  While .Execute And rDcm.Text = vbCr
     Set rTmp = rDcm
     rTmp.start = 0
     iPrg = rTmp.Paragraphs.Count
     If ActiveDocument.Paragraphs(iPrg - 1).Style = "PStyle A" Then
     If ActiveDocument.Paragraphs(iPrg + 1).Style = "PStyle C" Then
        ActiveDocument.Paragraphs(iPrg).Range.Delete
        rDcm.Collapse Direction:=wdCollapseEnd
     End If
     End If
  Wend
End With
End Sub

You might find resetting search options beforehand useful.

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

>Could anyone please give me a pointer on this?

As usual, I am only trying to show the way,
improvements and stripping off possibly useless code lines
is up to you and the co-readers.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
ilyaa1 - 12 Dec 2004 17:09 GMT
Helmut,

Thanks a lot for the answer. Two issues:

1. .Style can only access the standard styles in word. My document
contains copy-and-pasting from the web, so it has a tyle called
"XStyle", for example. That style shows up in the Styles & Formatting,
but not in .Styles list. How can I access it?

2. I got around the previous problem by selecting all instances of
"XStyle" and converting to a style that is listed in .Styles. When I
ran the script, Word lost responsiveness, was consuming 98% of the CPU,
and stayed that way for about 10 minutes, at which point in time I
killed it. Now, I do have a fairly large document, but scripts that
would remove all hyperlinks in the same size dicument work fine...

I've tried to google for vba-based style modification, but with not
much success. Could you please help me with these issues?
Thanks a lot.

Ilya Ayzenshtok.
Helmut Weber - 12 Dec 2004 22:56 GMT
Hi Ilya,

I am very sorry, I can't reproduce this problem.
If I copy some text (plus pictures) from a web site, the styles,
that come with it, _are_ listed in the styles list in the dialog
"edit find", here and now.

I think I've seen a site on tips on how to clean text that was
copied from the internet, but can't find it again.
Maybe somebody else will know.

Just, at a very long shot, i am doing something like this,
when converting internet content to word-docs:

With .Find
     .Text = "^011{1,}"
     .Replacement.Text = "^p"
     '...
End With

and apply my standard style afterwards.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
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.