• Adding Hyperlinks to a Table Crashes Word (Word 2002 SP1)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Adding Hyperlinks to a Table Crashes Word (Word 2002 SP1)

    Author
    Topic
    #392365

    I have a template that copies data from a local HTML file into a new file, creates headings and then creates a table at the top of the new document with hyperlinks to the various Headings within the document.

    The code I am using, at the bottom of the post, works perfectly using W2K, but when run in W2K2 it will fill the first column just fine, but as soon as it starts to fill the second column with the hyperlinks Word Freezes and then eventually crashes.

    I have spent all day searching the MSKB, Google and the Usenet for anything that even remotely similar. Does anyone have any idea of this is a bug in W2K2? Or is my code wrong?

    Thanks for any insight.

    ' Get Rows and Columns for Link table at the top
    intCount = docMe.Bookmarks.Count
    intMaxCol = 4
    
    ' Figure out the number of rows needed
    If intCount Mod intMaxCol = 0 Then
        intMaxRow = intCount / intMaxCol
    Else
        intMaxRow = Int(intCount / intMaxCol) + 1
    End If
    
    ' Create Table
    Set rng = docMe.Range(docMe.Content.Start, docMe.Content.Start)
    Set tbl = docMe.Tables.Add(rng, intMaxRow, intMaxCol, , wdAutoFitContent)
    
    ' Remove Table Borders
    tbl.Borders.InsideLineStyle = wdLineStyleNone
    tbl.Borders.OutsideLineStyle = wdLineStyleNone
    
    ' Set starting values of Row and Column
    intRow = 1
    intColumn = 1
    
    
    For Each par In docMe.Paragraphs
        ' Is the paragraph an H3
        If par.Style = "Heading 3" Then
            ' Set Name for Hyperlink
            strHLink = Left$(par.Range.Text, Len(par.Range.Text) - 1)
            ' Loop through State Names to get the right one
            For intLoop = 0 To UBound(aryStates())
                If aryStates(intLoop, 0) = strHLink Then
                    ' Add a Hyperlink in the table to the State Name
    '                docMe.Hyperlinks.Add tbl.Cell(intRow, intColumn).Range, , _
                        aryStates(intLoop, 1), , strHLink
    'The line above was the original line that freezes Word
    'I changed it to the line below and it still freezes here
                    docMe.Hyperlinks.Add Anchor:=tbl.Cell(intRow, intColumn).Range, _
                        SubAddress:=aryStates(intLoop, 1), TextToDisplay:=strHLink
                    Exit For
                End If
            Next
            ' Increment Rows and Columns for Down and then over
            If intRow = intMaxRow Then
                intRow = 1
                intColumn = intColumn + 1
            Else
                intRow = intRow + 1
            End If
        End If
    Next

    Any ideas? Any suggestions?

    This is being run on WinXPP SP1

    Thanks

    Viewing 0 reply threads
    Author
    Replies
    • #704134

      hi Bryan,

      i can confirm your results (crashing that is), but i think i’ve found the solution.
      you need to set a reference to the cell range and then collapse that range before you add the hyperlink :

      dim HLCellRange as range
      ‘…..
      ‘…..
      set hlcellrange=tbl.Cell(intRow, intColumn).Range
      hlcellrange.collapse
      docMe.Hyperlinks.Add Anchor:=hlcellrange, _
      SubAddress:=aryStates(intLoop, 1), TextToDisplay:=strHLink

      i believe word might be having problems with the cell marker inside the range…

      • #704138

        pieter,

        That worked beautifully. thankyou

        Although, I was sure I tried something similar and it still crashed. shrug

    Viewing 0 reply threads
    Reply To: Adding Hyperlinks to a Table Crashes Word (Word 2002 SP1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: