View Full Version : How to determine current table location (row/col)
Digilee
06-07-2011, 02:36 PM
Hi,
I need to loop through a document and find all occurrences of '0x??:??', but ignore it if it is found in cell(1,1) of a table.  I can do everything but determine what row and column I am in when I am inside a table.
Is there a method for this?
Regards,
Lee
Frosty
06-07-2011, 02:41 PM
You're looking for .RowIndex and .ColumnIndex off of the Cell object.
Digilee
06-07-2011, 02:50 PM
Thanks for the quick reply.
I am missing something, though.  If I use the Cell object, I must specify a row and column (.cell(1,1)), at which point .ColumnIndex returns 1.  The Cells object does not have a .ColumnIndex member.
This is my start at the code. 
Sub UpdateRegisterLinks()
    Dim r As Range
    
    Application.ScreenUpdating = False
    
    Set r = ActiveDocument.Range
    With r.Find
        .ClearFormatting
        .Text = "0x??:??"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Do While .Execute(Forward:=True) = True
        
            ' If in column 1 of a table, ignore it
            If (r.Information(wdWithInTable)) Then
                MsgBox r.Tables(1).Cell(1,1).ColumnIndex
            End If
        Loop
    End With
    Application.ScreenUpdating = True
End Sub
Thanks,
Lee
gmaxey
06-07-2011, 03:18 PM
Try:
 
With r.Find
  .ClearFormatting
  .Text = "0x??:??"
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = True
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  Do While .Execute(Forward:=True) = True
    'If in column 1 of a table, ignore it
     If (r.Information(wdWithInTable)) Then
       If r.Cells(1).ColumnIndex = 1 And r.Cells(1).RowIndex = 1 Then
         MsgBox "Ignore me I am in column 1 and row 1"
       Else
         MsgBox "Do me but be gentle."
       End If
     End If
  Loop
End With
Application.ScreenUpdating = True
End Sub
Digilee
06-07-2011, 03:35 PM
That worked, but also the following worked.  I like yours better
                MsgBox "Cell " & r.Information(wdStartOfRangeColumnNumber) _
                        & "," & r.Information(wdStartOfRangeRowNumber)
(How do I mark this question solved?)
Digilee
06-07-2011, 05:00 PM
Actually, it's not quite solved.  Everything seems to work fine until I add a hyperlink to the text when I find a matching string.  As soon as it fonds the first one and (successfully) adds a hyperlink, the find seems to start over at the same text string, causing a loop forever.
Here is the code so far.  Your help for this VBA Newbie but programming forever guy is appreciated.
'-----------------------------------------------------------------------------------------------
' Add the correct bookmark link to the passed register address text
'-----------------------------------------------------------------------------------------------
Function AddLink(r As Range) As Boolean
    Dim bmString As String
    Dim colonPos As Long
    Dim x As Range
    
    bmString = Trim(Left(r.Text, 7))     ' Len( "0xB0:00") == 7
    
    ' Create a bookmark string from the text at the passed range
    colonPos = InStr(1, bmString, ":")
    If colonPos <> 0 Then
        ' Replace colon with underscore
        bmString = "REG_" + Left(bmString, colonPos - 1) + "_" + Mid(bmString, colonPos + 1)
        
        ' Bookmark must exist, otherwise it's an error
        If ActiveDocument.Bookmarks.Exists(bmString) = False Then
            AddLink = False
            Exit Function
        End If
        
        ' Bookmark exists, create a link to it at the range text
        ActiveDocument.Hyperlinks.Add _
            Anchor:=r, _
            Address:=ActiveDocument.FullName, _
            SubAddress:=bmString, _
            ScreenTip:=""
        AddLink = True
    Else
        AddLink = False
    End If
End Function
'-----------------------------------------------------------------------------------------------
' Update all links to register tables
'-----------------------------------------------------------------------------------------------
Sub UpdateRegisterLinks()
    Dim r As Range
    Dim linkErr As Boolean
    
    Application.ScreenUpdating = False
    
    Set r = ActiveDocument.Range
    With r.Find
        .ClearFormatting
        .Text = "0x??:??"
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        Do While .Execute(Forward:=True) = True
            linkErr = False
            
            ' If in column 1 of a table, ignore it
            If (r.Information(wdWithInTable)) Then
                If r.Cells(1).ColumnIndex <> 1 Then
                    linkErr = Not AddLink(r)
                End If
            Else
                linkErr = Not AddLink(r)
            End If
            ' Mark text if bad link
            If linkErr = True Then
                MsgBox "Link Error"
            End If
        Loop
    End With
    
    
    Application.ScreenUpdating = True
End Sub
I am going to attempt to upload a sample file, if I can remember how.
Digilee
06-07-2011, 05:06 PM
Here is the test file.  It has bookmarks in the form 'REG_0xB0_00' and text to be linked in the format '0xB0:00'
6080
gmaxey
06-07-2011, 05:21 PM
I'm in the middle of a movie.  Try collasping r in your loop after it is process
 
r.Collapse wdCollapseEnd
Frosty
06-07-2011, 05:22 PM
I think you want to add an else condition to collapse your search range, however, I'm getting so many Link Error message boxes that it's hard to tell what you want to have happen here, as I assume this is sort of stubby code.
             ' Mark text if bad link
            If linkErr = True Then
                MsgBox "Link Error"
            Else
              r.Collapse wdCollapseEnd
            End If
Digilee
06-07-2011, 05:52 PM
:doh:
Thanks, I knew that... enjoy your movie!
Digilee
06-07-2011, 05:54 PM
Thanks Frosty, yes it is stub code - that message is annoying, though there are too many links that are not in the bookmark list.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.