Solved

Search for tables that contain a certain paragraph style using VBA

Posted on 2015-01-27
8
103 Views
Last Modified: 2015-01-27
Dear Experts:

I would like to run a macro that performs the following action:

Run thru all the tables of the current document (this document only consists of individual tables, nothing else) ...
... and search for those tables where paragraphs / cell entries are formatted with a user-defined paragraph style named 'MyTableStyle' (just one cell entry formatted with this paragraph style is enough)

A msgbox should show the index numbers of all these tables such as ... "Tables 4, 7, 9, 13 feature the user-defined paragraph style 'MyTableStyle'."

 These index numbers (i.e. the msgbox text)  should also be copied to a newly created document.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
  • 4
  • 2
  • 2
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572570
Try:

Sub MyAnswer()
strAnswer = ""
For idx = 1 To ActiveDocument.Tables.Count ' new requirement: the macro should only run on table 5, 13, 19, 23, 27, 44, 54, 73
    Select Case ActiveDocument.Tables(idx).Style
    Case "MyTableStyle"
        If strAnswer <> "" Then
            strAnswer = strAnswer & ", "
        End If
        strAnswer = strAnswer & idx
    End Select
Next
MsgBox "Tables " & strAnswer & " feature the user-defined paragraph style 'MyTableStyle'."
End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 40572579
Hi Phlllip, thank you very much for your swift reply. I am afraid to tell you that nothing happens, i.e. the msgbox comes up but with no index numbers whatsoever.

Regards, Andreas
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572581
Diagnostics. Try this:

Sub MyAnswer()
strAnswer = ""
For idx = 1 To ActiveDocument.Tables.Count
        If strAnswer <> "" Then
            strAnswer = strAnswer & ", "
        End If
        strAnswer = strAnswer & ActiveDocument.Tables(idx).Style
    End Select
Next
MsgBox "Styles: " & strAnswer
Debug.print "Styles: " & strAnswer
End Sub

What is the answer? (It should be in your immediate window - Ctrl+G).
0
 

Author Comment

by:AndreasHermle
ID: 40572599
Hi Phillip, thank you very much for your swift support.

The table style does not have any relevance in my macro, it is an individual custom-made paragraph style (MyTableStyle) for which all the tables should be checked.

Andreas
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40572645
HI,

pls try ( if myTableStyle is in the first cell of each table required table

Sub gg()
   With ActiveDocument
   Set a = ActiveDocument.Tables
        For Idx = 1 To .Tables.Count ' new requirement: the macro should only run on table 5, 13, 19, 23, 27, 44, 54, 73
            Set rng = .Tables(Idx).Cell(1, 1).Range

            If rng.Paragraphs(1).Style.NameLocal = "MyTableStyle" Then
                    Set rng = .Tables(Idx).Cell(2, Bookmark1).Range 'first cell
                    rng.MoveEnd wdCharacter, -1
                    .Bookmarks.Add ("bookmark_" & b), rng
            
                    Set rng = .Tables(Idx).Cell(2, Bookmark2).Range 'fourth cell
                    rng.MoveEnd wdCharacter, -1
                    .Bookmarks.Add ("bookmark_" & b + 1), rng
                    b = b + 2
            End If
        Next
        End With
End Sub

Open in new window

Regards
0
 

Author Comment

by:AndreasHermle
ID: 40572758
Hi Rgonzo,

thank you very much for your swift help. I think you got something wrong.

My initial requirement is:

A msgbox should show the index numbers of all these tables (i.e. tables where the first cell features this specific paragraph style 'MyTableStyle') such as ... "Tables 4, 7, 9, 13 feature the user-defined paragraph style 'MyTableStyle'."

There is no need to do bookmarking. That applied to the macro before.

Regards, Andreas
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40572818
then try

Sub Macro()
   With ActiveDocument
   Set a = ActiveDocument.Tables
        For Idx = 1 To .Tables.Count ' new requirement: the macro should only run on table 5, 13, 19, 23, 27, 44, 54, 73
            Set rng = .Tables(Idx).Cell(1, 1).Range

            If rng.Paragraphs(1).Style.NameLocal = "Subtitle" Then ' MyTableStyle
                    strList = strList & CStr(Idx) & ", "
            End If
        Next
    End With
    strList = "Tables " & Left(strList, Len(strList) - 2) & " feature the user-defined paragraph style 'MyTableStyle'."
    MsgBox strList
    Documents.Add
    Selection.TypeText Text:=strList
    End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 40572877
Very, very good Rgonzo, I am really impressed! Thank you very much for your superb help.

Andreas
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
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…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now