VBA: Select text between Heading1 items

M

Mike Cook

I'd like to locate Heading 1 items and select the text between them, and do this consecutively from the beginning to end of a Word document.

I've been using the following in a VBA macro, but need help with the guts of the loop. It finds the Headings, but I can't figure out how to select the text from one to the next.

Thanks for any help,
Mike


Selection.GoTo What:=wdGoToCharacter, Which:=wdGoToAbsolute, Count:=1

For Each oPara In ActiveDocument.Paragraphs

' Want to move to next Heading
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend

If oPara.Style = "Heading 1" Then

' Somehow select text from previous Heading

Selection.Range.Copy
End If

Next oPara
 
S

Stefan Blom

What are you going to do with the text in between the headings? If you
explained this more in detail, I'm sure someone could suggest an approach.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Mike Cook" wrote in message
I'd like to locate Heading 1 items and select the text between them, and do
this consecutively from the beginning to end of a Word document.

I've been using the following in a VBA macro, but need help with the guts of
the loop. It finds the Headings, but I can't figure out how to select the
text from one to the next.

Thanks for any help,
Mike


Selection.GoTo What:=wdGoToCharacter, Which:=wdGoToAbsolute, Count:=1

For Each oPara In ActiveDocument.Paragraphs

' Want to move to next Heading
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend

If oPara.Style = "Heading 1" Then

' Somehow select text from previous Heading

Selection.Range.Copy
End If

Next oPara
 
M

Mike Cook

I would like to copy the range of text to new, separate documents.

I've been able to copy text to new documents, but my problem is actually getting the range of text between two Heading 1 items. (or, Heading 2, for example)

Thanks, Mike
 
S

Stefan Blom

Are you saying that you just want to get rid of the headings, or should a
certain piece of text (for example between the first and second headings) be
inserted in a particular place in the new document?

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Mike Cook" wrote in message
I would like to copy the range of text to new, separate documents.

I've been able to copy text to new documents, but my problem is actually
getting the range of text between two Heading 1 items. (or, Heading 2, for
example)

Thanks, Mike
 

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