Using StoryRanges to select specific text in word docs

N

Nick Transier

I am new to VBA scripting in Word, but not to programming. I need to know
how to select the text that is under each headings in a document and port it
out to a cell in excel. In other words, if I have
---------------
Heading 1
Text under heading 1
Heading 2
Text under heading 2
-----------------
in a word document, I need to be able to send that to an excel document that
looks like

Heading 1 | Text under heading 1
Heading 2 | Text under heading 2

etc.

please help!

Thanks,
Nick
 
D

DA

Hi Nick

I'd try to work from your heading styles and select the
paragraphs following. You could use an array to cycle
through a list of styles, but this will at least give you
some ideas - For example:

----------------------
Sub GetMyTextAfterH1()
Dim lngCounter As Long
With ActiveDocument
For lngCounter = 1 To .Paragraphs.Count
If .Paragraphs(lngCounter).Style = "Heading 1" Then
.Paragraphs(lngCounter + 1).Range.Select
End If
Next
End With
End Sub
---------------------

Check this link for details on how to control Excel
http://word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm

Hope that helps,
Dennis
 
N

Nick Transier

Dennis,

Thanks for the help. My only question with that script is whether or not it
will select all text under a certain heading type, or just iterate through
different headings. The problem is that if I have two consecutive headings
that are at the same level, it will group all of that text together. I need
each heading to dump information, regardless of level.

Thanks again-
Nick
 
N

Nick Transier

OK, please disregard most of my previous post. I am testing this script and
I realized that it needs some more nested loops. It only selects the first
sentence under the heading it has identified. I need a loop that continues
selecting until it has reached a new heading. I think I can do this by
checking the next paragraph is NOT a new heading or I can say select all
that is heading type NORMAL. However, if there is any formatting changes, it
will not qualify as normal so that will probably not work well. Also, it
will not identify the heading type unless the heading type hasn't been
altered by the user, regardless of the fact that microsoft still keeps its
original heading name in the altered name (like Heading 1, User Specified
name).

Also, once I have a range selected, how do I assign that to an object so
that I can export it, view it, etc?

Finally, do you know how to select and copy images that are within the
document as it is stepping through the document?

Thanks again for all the help. This VBA noob needs all he can get.

Nick
 
D

DA

Hi Nick

You seem to be on the right track. I wouldn't bother
checking for things like normal. Here's and extended
example; note I don't use an array, just a lookup string
for my defined heading styles which avoids having to
setup a loop to check for a match:

--------------------------
Sub GetMyText()
Dim lngCounter As Long
Dim strMyStyles As String

ReDim astrMyStyles(1)
strMyStyles = "Heading 1," + _
"Heading 2"

With ActiveDocument
Selection.HomeKey Unit:=wdStory
For lngCounter = 1 To .Paragraphs.Count
If InStr(strMyStyles, .Paragraphs(lngCounter).Style) Then
'Flush previous selection
MsgBox "Previous selection was :" + Selection.Text
'Do a selection.copy here to grab everything
'including graphics.
Selection.Collapse Direction:=wdEnd
Selection.MoveDown Unit:=wdParagraph, Count:=1
Else
Selection.MoveDown Unit:=wdParagraph, _
Count:=1, Extend:=wdExtend
End If
Next
End With
End Sub
 
N

Nick Transier

Dennis, Thanks for your continued help. Just FYI, here is the code I am
using now. The For-Each loop is more efficient and it also gets rid of the
problem of checking the next paragraph (when using counters) that doesn't
exist which throws an error. Here it is.

Sub GetText2()
Dim para As Paragraph, rng As Range
Dim DocA As Document, DocB As Document
Dim iHeaderLevel As Integer

'testing vars
Dim iParagraphCount As Integer
'testing vars

Set DocA = ActiveDocument
Set DocB = Word.Documents.Add
Set rng = DocB.Range

For Each para In DocA.paragraphs
DoEvents
'testing stuff
iParagraphCount = iParagraphCount + 1
'testing stuff

If (InStr(para.Format.Style, "TOC") > 0) Then
'testing stuff
para.Range.Select
'testing stuff
ElseIf para.Format.Style Like "Heading [0-9]" Then
'testing stuff
para.Range.Select
'testing stuff
iHeaderLevel = para.OutlineLevel
rng.Collapse wdCollapseEnd
rng.Text = "Heading level: " & iHeaderLevel & " Name: " &
para.Range.Text
Else
'testing stuff
para.Range.Select
'testing stuff
rng.Collapse wdCollapseEnd
rng.Text = para.Range.Text
End If
Next para
MsgBox "Done"
End Sub
 

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