Solved

VBA Microsoft Word Find the Heading 1 immediately before each table

Posted on 2016-08-22
4
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Suggested Courses

630 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