PDA

View Full Version : Search Word document using Heading numbers



debkev1010
07-30-2008, 10:36 AM
I have an Excel spreadsheet that contains information from a Word document that I manually typed in. The information is sorted by the heading number which i got from the document (manually)...

Heading ........ Requirement No.
3.2.5.2.1 .............. 1
3.2.5.2.1 .............. 3
3.2.5.2.2 .............. 3

and so on.

I would like my VBA code to find these headings in the word document and eventually the Requirement No. within the section. I will paste some attributes to each Requirement No. but right now I have no idea of how to search for heading numbers.

Thanks,
Kevin

macropod
07-31-2008, 03:27 AM
Hi Kevin,

Assuming you're using one of Word's Heading Styles to generate the paragraph numbers, try:
Sub Test()
MsgBox Selection.Paragraphs(1).Range.ListFormat.ListString
End Sub

debkev1010
08-01-2008, 06:18 AM
Hey macropod, thanks for the code. It works great. Here's the code the you gave me in action:


'Search for the heading number that matches the heading number from the spreadsheet
for sentencecount = 1 to currentSrs.Sentences.Count
If headingNumber = currentSrs.Sentences(sentenceCount).ListFormat.ListString Then
.....
End if


Thanks again,
Kevin

macropod
08-01-2008, 09:21 PM
i Kevin,

You'd probably find the code runs quicker if you use the paragraphs collection, rather than the sentences collection. For example:
Sub Test()
Dim RngPara As Paragraph
Dim Level As String
Level = "1.1.1.1"
For Each RngPara In ActiveDocument.Paragraphs
If RngPara.Range.ListFormat.ListString = Level Then _
MsgBox "Paragraph " & Level & " uses the """ & RngPara.Style & """ Style."
Next
End Sub

mdmackillop
08-02-2008, 03:07 AM
Coincidentally, I've been working on a problem here (http://vbaexpress.com/forum/showthread.php?t=21214) to send data to excel based on finding a search string, which could probably be configured to suit your requirement.