Paragraph deletion based on style

I

ilya.ayzenshtok

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.
 
H

Helmut Weber

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/
 
I

ilyaa1

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.
 
H

Helmut Weber

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/
 

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