PDA

View Full Version : [SLEEPER:] VBA code in Word for identifying Headings and going to them



scndlsbob
09-05-2017, 11:50 AM
I know that the headings fall under style. I am trying to find example VBA code that will help me loop through a word document to each of the headings that I can see under the navigation pane. I want to jump to them in code and extract out the text for that heading.

So on the left we have the navigation pane. Under that the headings is selected. I can see in the word document things like Title:Big new story or Abstract: This story, blah, blah blah. When I click on it in the navigation pane, it jumps to that spot in the document. I tried recording a macro to see how word would store the code, but I get something that looks like a cassette around my mouse and the macro is usually blank.


Thanks for any help you might pass along,

Bob

macropod
09-05-2017, 02:44 PM
You don't need VBA for this. Simply insert a Table of Contents if you don't already have one, then copy & paste that to your other document as unformatted text.

scndlsbob
09-08-2017, 06:28 AM
I don't want to insert into a current word document, I want to be able to use a macro or VBA to jump to the parts of the Headings.
When the navigation pane is open, and the Headings tab is displayed, you can click on the portions of the headings and word jumps that portion of the document. I want to be able to cycle through the Headings and extract the second line of the headings. I am trying to automate this as I have hundreds of documents to scan in a directory of reports and test papers. There are several Headings in the navigation pane in which Word will jump to as you click them. I want to be able to use code to look at each one like the Title, the abstract, authors, keywords and so forth. Word already has them as Headings, I just want to know how to automate looping through the portions of the Headings to extract what I need.

Example:
Title: (heading, I want code to jump to this point)
The greatest book ever (the portion I want to extract using code).

macropod
09-08-2017, 07:10 AM
I want to be able to cycle through the Headings and extract the second line of the headings ...
Example:
Title: (heading, I want code to jump to this point)
The greatest book ever (the portion I want to extract using code).
Your description makes no sense at all to me. The content you appear to be saying you want to extract doesn't look like it's part of the heading at all (second line or otherwise); it appears to be in a completely separate paragraph. Furthermore, your original post clearly said you wanted to extract the headings, not something else:

I know that the headings fall under style. I am trying to find example VBA code that will help me loop through a word document to each of the headings that I can see under the navigation pane. I want to jump to them in code and extract out the text for that heading.
Creating a Table of Contents - as I suggested - and copying that to a new document will do exactly that (you don't have to save any changes to the document). Indeed, if you want, you could use TOC & RD fields in a completely independent document to import all the headings to there without ever opening the source documents. With VBA, even that could be automated.

scndlsbob
09-08-2017, 10:31 AM
I want to use VBA code like a macro to extract parts of text in the document that has already been selected as Heading 1 or Heading 2 from the styles.

In most of our documents, there is a Title, authors, abstract, keywords and references. I want to use code to populate a spreadsheet or database with the title, authors so on and so forth. I want to use the VBA code to automate this where hopefully I can jump to the part of Word where the object for the headings is stored, then loop through each item and compare the heading text (IE select case activedocument.headings.(x).text) to see if it matches Title or Authors. If so, the next line should have the information I a need to population a spreadsheet or Database table.

so something like
For each MyHeading in activedocument.headings
select case myheading

case "Title"
'Grab next line and push to spreadsheet

end select

macropod
09-08-2017, 04:16 PM
Here is some code to get you started. I'll leave it to you to add the code for automating Excel or whatever - something you conveniently failed to mention when you started this thread. Anyway, a search of these forums will turn up plenty of code for that. The following code simply reports however much of the found content will fit in a message box.

Sub GetHeadingNextText()
Application.ScreenUpdating = False
Dim RngHd As Range, h As Long, i As Long, strOut As String, ArrExpr()
ArrExpr = Array("Abstract:", "Author:", "Keywords:", "References:", "Title:")
For i = 0 To UBound(ArrExpr)
strOut = strOut & vbCr & ArrExpr(i)
For h = 1 To 2
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ArrExpr(i)
.Style = "Heading " & h
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set RngHd = .Paragraphs.Last.Range.Next.Paragraphs.Last.Range
With RngHd
.End = .End - 1
strOut = strOut & vbCr & .Text
End With
.Start = RngHd.End + 1
.Find.Execute
Loop
End With
Next
Next
Set RngHd = Nothing
MsgBox "The following text is associated with -" & strOut
Application.ScreenUpdating = True
End Sub
You can change the ArrExpr content if you want to look for something else and you can set .MatchCase = False if case usage is inconsistent.

scndlsbob
09-11-2017, 10:01 AM
Thank you. This code got me into the ball park. I am able to loop through. The problem I am running into is that the document is set up that at the end of titles is a carriage return, then another carriage return, then the text I am looking for. IE Prepared Date:vbcr, vbcr, 09/11/2017, vbcr.
I am not sure how word defines a paragraph Vs a sentence. So what I get from the code above is " " when the message box appears. So I may have to try to extract the .end from the With rnghd into an integer placeholder to try to find the additional vbcr's and then set a new range to extract the information.

macropod
09-11-2017, 03:06 PM
Well, you did say the content you're after was on the 'next line', so that's what I coded for. If it's actually on the one after that, you'll need to change:

Set RngHd = .Paragraphs.Last.Range.Next.Paragraphs.Last.Range
to:

Set RngHd = .Paragraphs.Last.Range.Next.Paragraphs.Last.Range.Next.Paragraphs.Last.Rang e

scndlsbob
09-12-2017, 06:30 AM
The items I modified I tried to make in red. I found that some headings were normal, some were Headings 1. I found that some had a total of three vbcr's, some had two, and some had one.
The code example you provided has gotten me where I wanted to be where I could at least loop through the headings or at least set up a search for the headings by name through the array.
Thank you again.


Sub GetHeadingNextText()
Application.ScreenUpdating = False
Dim RngHd As Range, h As Long, i As Long, strOut As String, ArrExpr()
ArrExpr = Array("Abstract:", "Author:", "Keywords:", "References:", "Title:")
For i = 0 To UBound(ArrExpr)
strOut = strOut & vbCr & ArrExpr(i)
For h = 1 To 1
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Normal"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set RngHd = .Paragraphs.Last.Range.Next
If RngHd= Vbcr then
'Report Date:(vbcr)
'March 12, 2012 (vbcr)
Set RngHd = .Paragraphs.Last.Range.Next.Paragraphs.Last.Range
End If
If RngHd = vbcr then
'Report Date:(vbcr)
'(vbcr)
'March 12, 2012 (vbcr)
Set RngHd = .Paragraphs.Last.Range.Next.Paragraphs.Last.Range.Next.Paragraphs.Last.Rang e
End If
If RngHd = vbcr then
'Document Title: Summary of meeting on Feburary 29, 2012.(vbcr)
Set RngHd = .Paragraphs.Last.Range
End If
With RngHd
.End = .End - 1
strOut = strOut & vbCr & .Text
End With
.Start = RngHd.End + 1
.Find.Execute
Loop
End With
Next
Next
Set RngHd = Nothing
MsgBox "The following text is associated with -" & strOut
Application.ScreenUpdating = True
End Sub

macropod
09-12-2017, 01:46 PM
First you asked for advice on how to extract headings, then it emerged it wasn't the headings you wanted, but text following headings with particular content, but then it turned out the content you want might not actually be in the 'next line' as you claimed but in one after that; now you don't even know if you're working with headings...

I've wasted enough time on this.