[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

Search for tables that contain a certain paragraph style using VBA

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
AndreasHermle
Asked:
AndreasHermle
  • 4
  • 2
  • 2
1 Solution
 
Phillip BurtonCommented:
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
 
AndreasHermleAuthor Commented:
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
 
Phillip BurtonCommented:
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
AndreasHermleAuthor Commented:
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
 
Rgonzo1971Commented:
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
 
AndreasHermleAuthor Commented:
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
 
Rgonzo1971Commented:
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
 
AndreasHermleAuthor Commented:
Very, very good Rgonzo, I am really impressed! Thank you very much for your superb help.

Andreas
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now