none
inserting hyperlinks one after the other in a word document using vb.net RRS feed

  • Question

  • hi,

    i am developing an application which automatically creates word docs. The problem is, i have to add a list of hyperlinks at the start of the word doc one after the other.something like this

       hyperlink1

       hyperlink2...................

    all i am able to get   is hyperlink1 hyperlink2..........

    the part of the code is as follows. As a test i have taken some 30 bookmarks

     

      Do Until (t = 30)

     

                If oDoc.Range.Bookmarks.Exists(oDoc.Bookmarks.Item(t).Name.ToString) Then

                    Dim opara3 As Word.Paragraph

                    opara3 = oDoc.Range.Paragraphs.Add(oWord.Application.Selection.Range)

     

     

                    opara3.Range.Hyperlinks.Add(Anchor:=oWord.Application.Selection.Range, SubAddress:=oDoc.Range.Bookmarks.Item(t))

     

                    opara3.Range.InsertParagraphAfter()

                    opara3.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

     

     

                    t = t + 1

     

     

                End If

     

     

                oWord.Application.Selection.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

                oWord.Application.Selection.Range.Move(Word.WdKey.wdKeyReturn)

     

     

            Loop

    can anyone help me with this..!???

     

     

    Regards,

    Aditya

    Wednesday, March 30, 2011 5:23 AM

Answers

  • Hi Aditya,

    Thanks for posting in the MSDN Forum.

    This is my snippet.

    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wdApp As New Word.Application
        Dim wdDoc As Word.Document
        Dim wdParagraph As Word.Paragraph
        Dim wdNewParagraph As Word.Paragraph
    
        Try
          wdDoc = wdApp.Documents.Add()
          wdApp.Visible = True
    
          wdParagraph = wdDoc.Sections(1).Range.Paragraphs.Add()
          wdParagraph.Range.Hyperlinks.Add(Anchor:=wdApp.Application.Selection.Range, Address:="http://localhost", ScreenTip:="This is a Tesst", TextToDisplay:="Test")
          wdNewParagraph = wdParagraph.Range.Paragraphs.Add()
          wdNewParagraph.Range.Hyperlinks.Add(Anchor:=wdNewParagraph.Range, Address:="http://localhost", TextToDisplay:="Another Text")
        Catch ex As Exception
          MsgBox(ex.Message, , "Exception")
        End Try
      End Sub
    End Class
    

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [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, March 31, 2011 6:12 AM
    Moderator

All replies

  • hi,

    i am developing an application which automatically creates word docs. The problem is, i have to add a list of hyperlinks at the start of the word doc one after the other.something like this

       hyperlink1

       hyperlink2...................

    all i am able to get   is hyperlink1 hyperlink2..........

    the part of the code is as follows. As a test i have taken some 30 bookmarks

     

     

     

      Do Until (t = 30)

     

                If oDoc.Range.Bookmarks.Exists(oDoc.Bookmarks.Item(t).Name.ToString) Then

                    Dim opara3 As Word.Paragraph

                    opara3 = oDoc.Range.Paragraphs.Add(oWord.Application.Selection.Range)

     

     

                    opara3.Range.Hyperlinks.Add(Anchor:=oWord.Application.Selection.Range, SubAddress:=oDoc.Range.Bookmarks.Item(t))

     

                    opara3.Range.InsertParagraphAfter()

                    opara3.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

     

     

                    t = t + 1

     

     

                End If

     

     

                oWord.Application.Selection.Range.Collapse(Word.WdCollapseDirection.wdCollapseEnd)

                oWord.Application.Selection.Range.Move(Word.WdKey.wdKeyReturn)

     

     

            Loop

    can anyone help me with this..!???

     

     

     

     

    Regards,

    Aditya

    Wednesday, March 30, 2011 5:45 AM
  • Hi Aditya,

    Thanks for posting in the MSDN Forum.

    This is my snippet.

    Imports Word = Microsoft.Office.Interop.Word
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wdApp As New Word.Application
        Dim wdDoc As Word.Document
        Dim wdParagraph As Word.Paragraph
        Dim wdNewParagraph As Word.Paragraph
    
        Try
          wdDoc = wdApp.Documents.Add()
          wdApp.Visible = True
    
          wdParagraph = wdDoc.Sections(1).Range.Paragraphs.Add()
          wdParagraph.Range.Hyperlinks.Add(Anchor:=wdApp.Application.Selection.Range, Address:="http://localhost", ScreenTip:="This is a Tesst", TextToDisplay:="Test")
          wdNewParagraph = wdParagraph.Range.Paragraphs.Add()
          wdNewParagraph.Range.Hyperlinks.Add(Anchor:=wdNewParagraph.Range, Address:="http://localhost", TextToDisplay:="Another Text")
        Catch ex As Exception
          MsgBox(ex.Message, , "Exception")
        End Try
      End Sub
    End Class
    

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [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, March 31, 2011 6:12 AM
    Moderator