views:

559

answers:

1

I'm writing a VB Macro to do some processing of documents for my work. The lines of text are searched and the bracketed text is put in a list(box).

The problem comes when I want to remove all hyperlinks in the document and then generate new ones (not necessarily in the location of the original hyperlinks)

So the problem is How do I remove the existing hyperlinks?

My current issue is that every time a link gets added, the hyperlinks count goes up one, but when you delete it, the count does NOT reduce. (as a result I now have a document with 32 links - all empty except for 3 I put in myself - they do not show up in the document)

At the end of the code are my attempts at removing the hyperlinks.

Private Sub FindLinksV3_Click()

ListOfLinks.Clear

ListOfLinks.AddItem Now
ListOfLinks.AddItem ("Test String 1")

ListOfLinks.AddItem ActiveDocument.FullName


SentenceCount = ActiveDocument.Sentences.Count
ListOfLinks.AddItem ("Sentence Count:" & SentenceCount)
counter = 0

For Each myobject In ActiveDocument.Sentences    ' Iterate through each element.
    ListOfLinks.AddItem myobject
    counter = counter + 1


    BracketStart = (InStr(1, myobject, "("))

    If BracketStart > 0 Then
        BracketStop = (InStr(1, myobject, ")"))

            If BracketStop > 0 Then
                ListOfLinks.AddItem Mid$(myobject, BracketStart + 1, BracketStop - BracketStart - 1)

                ActiveDocument.Sentences(counter).Select

                ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
                "http://testnolink/" & counter, ScreenTip:=""  'TextToDisplay:=""


            End If
    End If


Next


'ActiveDocument.Sentences(1).Select
'
'Selection.Range.Hyperlinks(1).Delete

ActiveDocument.Hyperlinks.Item(1).Delete

Debug.Print ActiveDocument.Hyperlinks.Count



End Sub
+2  A: 

The line removing the hyperlink is commented out. The following line will remove the first hyperlink within the selected range:

Selection.Range.Hyperlinks(1).Delete

This will also decrement Selection.Range.Hyperlinks.Count by 1.

To see how the count of links is changing you can run the following method on a document:

Sub AddAndRemoveHyperlink()

    Dim oRange As Range
    Set oRange = ActiveDocument.Range
    oRange.Collapse wdCollapseStart
    oRange.MoveEnd wdCharacter

    Debug.Print ActiveDocument.Range.Hyperlinks.Count

    ActiveDocument.Hyperlinks.Add oRange, "http://www.example.com"
    Debug.Print ActiveDocument.Range.Hyperlinks.Count

    ActiveDocument.Hyperlinks.Item(1).Delete
    Debug.Print ActiveDocument.Range.Hyperlinks.Count

End Sub
0xA3
First, Thanks divo for correcting the mess I made of posting the code. It is very much appreciated by myself.OK, am I making too big a deal of the ActiveDocument.Hyperlinks.Count ? (realy I want to be able to have a master document I can paste various sources into, this may get updated frequently )If I uncomment the Selection.Range.Hyperlinks(1).Delete line it still fails with an error Runtime error "4198", this I attribute to hyperlinks(1) having been deleted earlier this morning ...but my ActiveDocument.Hyperlinks.Count is now 49, (see above comment about is this a big issue? )