Solved

Search for tables that contain a certain paragraph style using VBA

Posted on 2015-01-27
8
107 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
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.

 

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 51

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 51

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

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

Suggested Solutions

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…
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…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Suggested Courses

734 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