Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Search for tables that contain a certain paragraph style using VBA

Posted on 2015-01-27
8
Medium Priority
?
110 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
[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
  • 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
Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 

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
 
LVL 53

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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

618 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