How to delete a portion of doc defined by heading levels?

S

sdjwalls

Hello,

I've been struggling with how to delete a portion of a document by the
heading levels. I can return a collection of all the instances of a
particular style:

ActiveDocument.Styles("Int Heading 1")

But what I need to do is delete everything from the first instance "Int
Heading 1", up to, but not including, the second instance of "Int Heading 1".

Is there a way to do this? Is it possible to index into a styles collection
object and set a range (or some other way)?

Much appreciated,
SW
 
G

Greg Maxey

Sub ScratchMacro()
Dim oRng As Range
Dim oRng2 As Range
Set oRng = ActiveDocument.Range
With oRng.Find
'Find the first instance of the heading style
.Style = "Heading 1"
If .Execute Then
'Wrap the text in a range object
Set oRng2 = oRng.Duplicate
End If
'Fine the second instance of the heading style
If .Execute Then
'Extend the range object to include the text in between the two found
headings.
oRng2.End = oRng.Start
oRng2.Delete
End If
End With
End Sub
 
G

Greg Maxey

Macropod,

In my test that text isn't affected. I created a new blank document, typed
in some normal style text, typed in Heading 1 text, more normal text, more
Heading 1 text, more normal text and ran the code. After running the code
the only the text between the first two intances of Heading 1 and the first
Heading 1 text is deleted. The leading and trailing normal text is not
affected.

Are you seeing differenct results?



--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org

macropod said:
Hi Greg,

What happens if there's text with a different Style at the start or end of
the document?

At http://www.vbaexpress.com/forum/showthread.php?t=21576, the OP wanted
to delete all text except for that in the specified Style.

--
Cheers
macropod
[MVP - Microsoft Word]


Greg Maxey said:
Sub ScratchMacro()
Dim oRng As Range
Dim oRng2 As Range
Set oRng = ActiveDocument.Range
With oRng.Find
'Find the first instance of the heading style
.Style = "Heading 1"
If .Execute Then
'Wrap the text in a range object
Set oRng2 = oRng.Duplicate
End If
'Fine the second instance of the heading style
If .Execute Then
'Extend the range object to include the text in between the two found
headings.
oRng2.End = oRng.Start
oRng2.Delete
End If
End With
End Sub
 
M

macropod

Hi Greg,

That's as I expected.

I think there's an issue with how the OP is specifying the requirements. At VBA express they were specified somewhat differently.

--
Cheers
macropod
[MVP - Microsoft Word]


Greg Maxey said:
Macropod,

In my test that text isn't affected. I created a new blank document, typed
in some normal style text, typed in Heading 1 text, more normal text, more
Heading 1 text, more normal text and ran the code. After running the code
the only the text between the first two intances of Heading 1 and the first
Heading 1 text is deleted. The leading and trailing normal text is not
affected.

Are you seeing differenct results?



--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org

macropod said:
Hi Greg,

What happens if there's text with a different Style at the start or end of
the document?

At http://www.vbaexpress.com/forum/showthread.php?t=21576, the OP wanted
to delete all text except for that in the specified Style.

--
Cheers
macropod
[MVP - Microsoft Word]


Greg Maxey said:
Sub ScratchMacro()
Dim oRng As Range
Dim oRng2 As Range
Set oRng = ActiveDocument.Range
With oRng.Find
'Find the first instance of the heading style
.Style = "Heading 1"
If .Execute Then
'Wrap the text in a range object
Set oRng2 = oRng.Duplicate
End If
'Fine the second instance of the heading style
If .Execute Then
'Extend the range object to include the text in between the two found
headings.
oRng2.End = oRng.Start
oRng2.Delete
End If
End With
End Sub


sdjwalls wrote:
Hello,

I've been struggling with how to delete a portion of a document by the
heading levels. I can return a collection of all the instances of a
particular style:

ActiveDocument.Styles("Int Heading 1")

But what I need to do is delete everything from the first instance
"Int Heading 1", up to, but not including, the second instance of
"Int Heading 1".

Is there a way to do this? Is it possible to index into a styles
collection object and set a range (or some other way)?

Much appreciated,
SW
 
S

sdjwalls

Hi Greg,

Sorry for the late reply...I didn't think my post actually had made it in
for some reason.

Anyway, many thanks...your example worked brilliantly. I hadn't realized you
can (sort of) index into a collection of styles (it wasn't clear from the
online help).

To tweak it a bit (and I should've been clear on this in the beginning), how
could I set oRng2.start to be at a different style? I should've originally
stated:

I think my problem lies somewhere in not fully understanding how the Execute
works in the With/End With.

Thanks again,
SW
 
G

Greg Maxey

Hmm. This is a little trickier as you want to find any "Heading" style
following the first instance of Heading 1.

AFAIK you can't use .Find to look for "some" variety of a Heading style, so
we will have to evaluate each paragraph following the initial Heading 1
style. Something like this:

Sub ScratchMacroII()
Dim oRng As Range
Dim oRng2 As Range
Dim bBingo As Boolean
Set oRng = ActiveDocument.Range
bBingo = False
With oRng.Find
'Find the first instance of the heading style
.Style = "Heading 1"
If .Execute Then
'Wrap the text in a range object
Set oRng2 = oRng.Duplicate
End If
'Fine the second instance of a the heading style
Set oRng = oRng2.Duplicate
Do
oRng.Move wdParagraph, 1
If InStr(oRng.Style, "Heading") > 0 Then
bBingo = True
End If
Loop Until bBingo
'Extend the range object to include the text in between the two found
headings.
oRng2.End = oRng.Start
oRng2.Delete
End With
End Sub
 
S

sdjwalls

Hi Greg,

Many thanks. With a few tweaks to what you have below, I've got my code to
do exactly what it needs to do.

The tweaks I added were to solve the problems of having it remove every
instance of Int Heading #, and also to remove it if was the last chapter
before the appendix (or could've done end-of-document, but my attempts didn't
work...anyway this works).

What I ended up with (a portion of it):

'
' Int Heading 1 instances
'
Set oRng = ActiveDocument.Range
Do

bBingo = False
bNotFound = True

' Eliminate "Int Heading 1" instances
With oRng.Find

'Find the first instance of the heading style
.Style = "Int Heading 1"

If .Execute Then
'Wrap the text in a range object
Set oRng2 = oRng.Duplicate
bNotFound = False
End If

If bNotFound = False Then
'Find the second instance of a heading style
Set oRng = oRng2.Duplicate
Do
oRng.Move wdParagraph, 1
If (StrComp(oRng.Style, "Heading 1", 1) = 0) Or _
(StrComp(oRng.Style, "Appendix 1", 1) = 0) Then
bBingo = True
End If
Loop Until bBingo

'Extend the range object to include the text in between the
two found headings.
oRng2.End = oRng.Start
oRng2.Delete
End If

End With

Loop Until bNotFound

Many thanks!!
SW
 

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