Solved

Search for tables that contain a certain paragraph style using VBA

Posted on 2015-01-27
8
104 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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) …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

911 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

17 Experts available now in Live!

Get 1:1 Help Now