Solved

VBA Microsoft Word Find the Heading 1 immediately before each table

Posted on 2016-08-22
4
118 Views
Last Modified: 2016-08-22
Screenshot of Microsoft Word document
http://www.screencast.com/t/nX1J5m85XFuY


Dim tablecount, glkey
tablecount = 0

tablecount = ActiveDocument.Tables.Count

For t = 1 To tablecount

glkey = Find the Heading 1 content immediately before each table
'something like: ActiveDocument.Tables(t).closest("Heading 1")

'I am using glkey here to pass on to a REST call for each table
 
   
Next
0
Comment
Question by:maxdbase
  • 2
4 Comments
 
LVL 18

Expert Comment

by:xtermie
ID: 41765466
If I understand your issue....here is a function that will get you the level of a heading
that would help you in your task above

Private Function GetLevel(strItem As String) As Integer
    ' Return the heading level of a header from the
    ' array returned by Word.

    ' The number of leading spaces indicates the
    ' outline level (2 spaces per level: H1 has
    ' 0 spaces, H2 has 2 spaces, H3 has 4 spaces.

    Dim strTemp As String
    Dim strOriginal As String
    Dim intDiff As Integer

    ' Get rid of all trailing spaces.
    strOriginal = RTrim$(strItem)

    ' Trim leading spaces, and then compare with
    ' the original.
    strTemp = LTrim$(strOriginal)

    ' Subtract to find the number of
    ' leading spaces in the original string.
    intDiff = Len(strOriginal) - Len(strTemp)
    GetLevel = (intDiff / 2) + 1
End Function

Open in new window

0
 
LVL 18

Accepted Solution

by:
xtermie earned 500 total points
ID: 41765470
You can call it from some other generic function that does something like:
Public Sub mysub3()
    Dim docSource As Word.Document
    Dim rng As Word.Range

    Dim astrHeadings As Variant
    Dim strText As String
    Dim intLevel As Integer
    Dim intItem As Integer

    Set docSource = ActiveDocument
    astrHeadings = _
     docSource.GetCrossReferenceItems(wdRefTypeHeading)

    For intItem = LBound(astrHeadings) To UBound(astrHeadings)
        ' Get the text and the level.
        strText = Trim$(astrHeadings(intItem))
        intLevel = GetLevel(CStr(astrHeadings(intItem)))
     Next intItem
... ' anything else you wanna do
End Sub

Open in new window

0
 

Author Comment

by:maxdbase
ID: 41765509
This is great, how do I get the closest Heading 1.
Sort of like jquery's closest function that steps outside of the table in the dom then
goes down and finds the first occurrence of a node.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41765916
This code finds each table and walks backwards until it finds a Heading 1 paragraph:
Sub FindHeadings()
Dim doc As Word.Document
Dim tbl As Word.Table
Dim rng As Word.Range
Dim para As Word.Paragraph

Set doc = ActiveDocument
For Each tbl In doc.Tables
    DoEvents
    Set rng = tbl.Range
    rng.Collapse wdCollapseStart
    rng.Move wdCharacter, -1
    rng.Expand wdParagraph
    Set para = rng.Paragraphs.Last
    Do Until para.Style = "Heading 1"
        DoEvents
        rng.Collapse wdCollapseStart
        rng.Move wdCharacter, -1
        rng.Expand wdParagraph
        Set para = rng.Paragraphs.Last
    Loop
    MsgBox para.Range.Text
Next tbl
End Sub

Open in new window

1

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question