none
office interop word link RRS feed

  • Question

  • I am trying to create links automatically in word from within my vb.net application , by far i am able to create the link for the first word i find but for the other words link is created but not on them , sometimes on the word before or after them

    below is my code 

     Dim _wordApp As ApplicationClass
            Dim _doc As Document
            Dim pos, len As Integer
            Dim reg As Regex
            
            Dim rng As Object
            Dim search As String = "AIO"
            Dim fileName As String = "c:\word.doc"
            Dim nullobj As Object = System.Reflection.Missing.Value
            Dim isReadOnly As Object = False
            _wordApp = New ApplicationClass()
    
            'open the word document
            _doc = _wordApp.Documents.Open(fileName, isReadOnly, nullobj, nullobj, nullobj, _
                 nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, _
                 nullobj, nullobj, nullobj, nullobj, nullobj)
    
            _wordApp.Visible = True
            _wordApp.ScreenUpdating = True
    
          
            reg = New Regex(search)
    
           
    
            Dim m As MatchCollection = reg.Matches(_doc.Range.Text)
    
    
            For Each found As Match In m
    
                For Each ora As Capture In found.Captures
    
                    MessageBox.Show(ora.ToString())
    
    
    
    
    
                    pos = found.Index
    
                    
                    len = search.Length
                    ' select the text
    
                    rng = _doc.Range(pos, len + pos)
    
    
                    _doc.Hyperlinks.Add(rng, "http://www.microsoft.com")
    
                    ''        
                    ''   _doc.SaveAs(fileName & ".html", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML)
    
    
    
                Next
            Next
    

    when runing the code i am displaying a message box that writes the word im searching for but when trying to write the link this is the problem , hope someone can help !!!:(

    Monday, March 3, 2014 2:19 PM

Answers

  • Hi,

    According to your description, you want to find specific string “AIO” in a Word document and add hyperlinks into all "AIO" words.

    I find you are using the Match Class of System.Text.RegularExpressions Namespace to find Strings. However, I recommend you to use Find Object in Word Object Model to do that since you are operating with Word document.

       

    I edit your code with Find.Execute Method to achieve the goal for your reference.

        Sub InsertLinks()
            Dim _wordApp As ApplicationClass
            Dim _doc As Word.Document
    
            Dim rng As Object
            Dim search As String = "AIO"
            Dim fileName As String = "c:\word.doc"
            Dim nullobj As Object = System.Reflection.Missing.Value
            Dim isReadOnly As Object = False
            _wordApp = New ApplicationClass()
    
            'open the word document
            _doc = _wordApp.Documents.Open(fileName, isReadOnly, nullobj, nullobj, nullobj, _
                 nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, _
                 nullobj, nullobj, nullobj, nullobj, nullobj)
    
            _wordApp.Visible = True
            _wordApp.ScreenUpdating = True
    
            rng = _doc.Range
            With rng.Find
                .MatchWildcards = True
                Do While .Execute(FindText:=search, Forward:=True) = True
                    _doc.Hyperlinks.Add(rng, "http://www.microsoft.com")
    
                    With rng
                        .End = rng.Hyperlinks(1).Range.End
                        .Collapse(0)
                    End With
                Loop
            End With
        End Sub



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 4, 2014 8:26 AM
    Moderator