none
VBA - Word 2007 - Add Hyperlinks from an Array

    Question

  • I created a sub to analyse an word document and add a list of hyperlink to the end of the document.

    I have 2 problems :

    - i used a new paragraph for each lines as the range the hyperlink. But it doesn't work, i only have the last line created. I search for other méthod for the range and don't find any that work.

    - it's impossible to use the array value for the property TextToDisplay : it failed with an error

     

    This is the code I used :

     

    Sub Search()
    
    Dim newPara As Paragraph
    
    Dim result() As String
    Dim LastOutline As String
    Dim i As Integer
    i = 1
    
    For Each pAra In ActiveDocument.Paragraphs
    
      If pAra.OutlineLevel <> WdOutlineLevel.wdOutlineLevelBodyText Then
        LastOutline = pAra.Range.ListFormat.ListString & pAra.Range.Text
      End If
         
      If pAra.Range.ListFormat.ListType = WdListType.wdListSimpleNumbering Then
        ReDim Preserve result(i)
        result(i) = LastOutline
        i = i + 1
      End If
    Next pAra
    
    For i = 1 To UBound(result)
      Set newPara = Me.Paragraphs.Add
      ActiveDocument.Hyperlinks.Add Anchor:=newPara.Range, Address:=ActiveDocument.FullName, _
        SubAddress:=result(i), ScreenTip:="", TextToDisplay:=result(i)
    Next i
    
    
    
    
    End Sub
    
    
    

    Can i have some idea to solve my problems

    Thanks

     

     

    Wednesday, January 05, 2011 5:54 PM

All replies

  • Hello Franck,

    Thanks for posting. From what you side, I think I am able to see this issue on my side. If we use the Add method without specified range as the parameter, it will add a new paragraph mark at the end of the active document. However, actually, the return value is not the paragraph where this mark is in. You could test it by using newPara.Range.Select method to see which range is selected in current document. So here I have made a change on this VBA code, and it works fine on my side.

    Sub Search1()

    Dim newPara As Paragraph
    Dim result() As String
    Dim LastOutline As String
    Dim i As Integer
    i = 1

    For Each pAra In ActiveDocument.Paragraphs

      If pAra.OutlineLevel <> WdOutlineLevel.wdOutlineLevelBodyText Then
        LastOutline = pAra.Range.ListFormat.ListString & pAra.Range.Text
      End If
        
      If pAra.Range.ListFormat.ListType = WdListType.wdListSimpleNumbering Then
        ReDim Preserve result(i)
        result(i) = LastOutline
        i = i + 1
      End If
    Next pAra

    For i = 1 To UBound(result)
      Set newPara = ActiveDocument.Paragraphs.Add
      'newPara.Range.Select
      count1 = ActiveDocument.Paragraphs.Count
      Set newPara = ActiveDocument.Paragraphs.Item(count1)
      ActiveDocument.Hyperlinks.Add Anchor:=newPara.Range, Address:=ActiveDocument.FullName, _
        SubAddress:=result(i), ScreenTip:="", TextToDisplay:=result(i)
    Next i

    End Sub

    If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 06, 2011 6:40 AM
  • Hello Bessie,

    Thanks for your answer.

    You solve one of my problems.

    But I still have error when I use the array value for the property TextToDisplay : TextToDisplay:=result(i)

    I have the error : 5824 - Memory or Space disk insufficient

    Thursday, January 06, 2011 7:22 AM
  • Hello again Franck,

    Glad to know that my post works for you. This issue has been confirmed as a bug in Word 2007 and logged into database. However the database is an internal one. When I put this VBA code in Word 2010, it works fine without this error. Sorry for the inconvenience it brings to you.

    Have a nice weekend.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 07, 2011 10:20 AM