Home > Enterprise >  ActiveDocument not Activating, Bookmark Search
ActiveDocument not Activating, Bookmark Search

Time:05-05

So I know this is long and not the prettiest, but what I am trying to accomplish is to cycle through a list of tables and look for a bookmark that I have placed in certain tables in the document. These tables have the ability to be anywhere in the doc, so I am looping through all and looking for each possible bookmark on each table.

Right now, the below is my current code. objDoc returns the correct Doc name and opens the correct Doc. The problem is after that, when the code cycles through the tables in that Doc, it does not see my Bookmarks. I have verified it is selecting the correct Doc and tables with this code. When I use the 'ActiveDoc' operator after 'objDoc.Activate', it selects the Doc I am running the code from, not objDoc where I meaning to perform this search. If I run this as a test macro in the Doc connected to objDoc outside of the below code, all variables assign correctly.

Please help, this is driving me crazy, thank you!

P.S. - also any help on slimming this down is welcome!

Dim objDoc As Document
Set objDoc = objWord.Documents.Open(strPath)

Dim fileName As String
fileName = Dir(strPath)

 objDoc.Activate

 Dim x As Long
 Dim data0, data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12, data13, data14, data15, data16 As Long

x = 0

Dim J As Integer
Dim iTableNum As Integer
Dim oTbl As Table

objDoc.Activate
iTableNum = objDoc.Tables.Count
For J = 1 To objDoc.Tables.Count
    Set oTbl = objDoc.Tables(J)
    
tryagain:
    oTbl.Select
    objDoc.Tables(J).Select ''''''ERROR LINE

    If Selection.Bookmarks.Exists("data" & x) And x < 17 Then
        iTableNum = objDoc.Tables.Count
        'Exit For
        If x = 0 Then
        data0 = J
        ElseIf x = 1 Then
        data1 = J
        ElseIf x = 2 Then
        data2 = J
        ElseIf x = 3 Then
        data3 = J
        ElseIf x = 4 Then
        data4 = J
        ElseIf x = 5 Then
        data5 = J
        ElseIf x = 6 Then
        data6 = J
        ElseIf x = 7 Then
        data7 = J
        ElseIf x = 8 Then
        data8 = J
        ElseIf x = 9 Then
        data9 = J
        ElseIf x = 10 Then
        data10 = J
        ElseIf x = 11 Then
        data11 = J
        ElseIf x = 12 Then
        data12 = J
        ElseIf x = 13 Then
        data13 = J
        ElseIf x = 14 Then
        data14 = J
        ElseIf x = 15 Then
            data15 = J
        Else
            data16 = J
            Exit For
        End If
    
                  
    ElseIf x < 17 Then
        x = x   1
        GoTo tryagain
    End If
    x = 0
    
Next J

x = 0

CodePudding user response:

Something like this might be a little easier to manage:

Sub Tester()
    Dim objDoc As Document, strPath As String
    Dim x As Long, J As Long
    Dim data(0 To 16) As Long
    
    strPath  = "some path here"
    Set objDoc = Documents.Open(strPath)
    
    For J = 1 To objDoc.Tables.Count              'loop over tables
        With objDoc.Tables(J)            
            For x = LBound(data) To UBound(data)  'loop bookmarks
                If .Range.Bookmarks.Exists("data" & x) Then data(x) = J
            Next x
        End With
    Next J
    
    'show the results
    For x = LBound(data) To UBound(data)
        Debug.Print x, data(x)
    Next x    
End Sub

CodePudding user response:

There is no need to loop through the tables to find the bookmark. There can only be one bookmark of a given name in a document, so either it exists or it doesn't. Hence, there is no need to loop through all the tables and again through all the bookmarks for each table:

With objDoc
    For x = LBound(Data) To UBound(Data)  'loop bookmarks
        If .Bookmarks.Exists("data" & x) Then
            If .Bookmarks("data" & x).Range.Information(wdWithInTable) = True Then
                Data(x) = .Range(0, .Bookmarks("data" & x).Range.End).Tables.Count
            End If
        End If
    Next x
End With

There is potential for further simplification (eliminating If tests) if you know that all the bookmarks exist and/or that any that do exist are in tables.

  • Related