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

x
?
Solved

Retrieve the index number of specific tables and display them in a msgbox

Posted on 2014-04-29
4
Medium Priority
?
156 Views
Last Modified: 2014-05-01
Dear Experts:

Below macro searches for the string 'Single Products' in the active Document. If this string is located in a table, the macro will come up with a msgbox telling the user that the string has been found.

Could somebody please tweak this macro so that all the index numbers of the tables where the string has been found are listed in a msgbox, e.g.
"The string 'Single Products' has been found in tables (1), (3), (5)."

Help is much appreciated. Thank you very much in advance for your valuable help.

Regards, Andreas


Sub Search_Table()
Dim ht As Single
Dim strLen As Integer
Dim rng As Range
Dim strFind As String

'The index number of the tables that have the string 'Single Products' have to be listed in a MsgBox at the End


    strFind = "Single Products"
    strLen = Len(strFind)

    Set rng = ActiveDocument.Range
     

    With rng
        .Find.ClearFormatting
        With .Find
            .Text = strFind
            .Execute
        End With
        
Do
            If .Find.Found Then
                If rng.Information(wdWithInTable) Then
                MsgBox "String has been found"
                End If
            End If
            
           If Not .Find.Found Then
                MsgBox "String has not been found"
                End If
            rng.MoveStart strLen
            .Find.Execute
        Loop Until Not (.Find.Found)
 End With
    
    

End Sub

Open in new window

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
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 1400 total points
ID: 40029634
Hi Andreas,
The table object doesn't have an index property, so it would be best to search each table, keeping track of its index as we go along.
Sub Search_Tables()
    Dim tbl As Table
    Dim strFind As String
    Dim ixTables() As Integer
    Dim ix As Integer
    Dim t As Integer
    Dim strMessage As String
    
    'The index number of the tables that have the string 'Single Products' have to be listed in a MsgBox at the End
    strFind = "Single Products"
    
    For Each tbl In ActiveDocument.Tables
        t = t + 1
        If tbl.Range.Find.Execute(strFind) Then
            ReDim ixTables(ix)
            ixTables(ix) = t
            ix = ix + 1
        End If
    Next tbl
    
    If ix > 0 Then
        strMessage = "String has been found in table" & IIf(ix = 1, "", "s") & ": "
        For t = 0 To ix - 2
            strMessage = strMessage & "(" & ixTables(t) & "), "
        Next t
        strMessage = strMessage & "(" & ixTables(t) & ")."
    Else
         strMessage = "String has not been found"
    End If
        
    MsgBox strMessage

End Sub

Open in new window

0
 
LVL 53

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 600 total points
ID: 40029986
Hi,

Corrected code: Redim Preserve at line 15
Sub Search_Tables()
    Dim tbl As Table
    Dim strFind As String
    Dim ixTables() As Integer
    Dim ix As Integer
    Dim t As Integer
    Dim strMessage As String
    
    'The index number of the tables that have the string 'Single Products' have to be listed in a MsgBox at the End
    strFind = "Single Products"
    
    For Each tbl In ActiveDocument.Tables
        t = t + 1
        If tbl.Range.Find.Execute(strFind) Then
            ReDim Preserve ixTables(ix)
            ixTables(ix) = t
            ix = ix + 1
        End If
    Next tbl
    
    If ix > 0 Then
        strMessage = "String has been found in table" & IIf(ix = 1, "", "s") & ": "
        For t = 0 To ix - 2
            strMessage = strMessage & "(" & ixTables(t) & "), "
        Next t
        strMessage = strMessage & "(" & ixTables(t) & ")."
    Else
         strMessage = "String has not been found"
    End If
        
    MsgBox strMessage

End Sub

Open in new window

Regards
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40029995
Thanks Rafael. I can't think why that didn't come out in my testing.
0
 

Author Closing Comment

by:AndreasHermle
ID: 40034322
Wow, I am impressed. How come that you both know all this? Of course it is partly experience and lots of coding jobs but there is also this gene that all you programmers have and the rest of us not.

Thank you very much for your great help. Regards, Andreas
0

Featured Post

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.

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

609 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