Solved

VBA Microsoft Word Find the Heading 1 immediately before each table

Posted on 2016-08-22
4
96 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
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…

829 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