//flex table opened by JP

Click to See Complete Forum and Search --> : Hyperlink delete macro


orkboss
11-14-2004, 12:06 PM
Can anyone write a macro for Word that selects all contents and then deletes all hyperlinks on a document. I tried to record one but without success.

ScaryBinary
11-14-2004, 01:09 PM
This is probably overkill, but I had fun writing it.
' Function : DeleteLinks
' Purpose : Deletes the hyperlinks in a Word document.
' Arguments :
' fLinkOnly - If this is True, then the text currently displayed for
' the hyperlink will remain, but the link will be removed.
' If this is False, then the link and the text will be deleted.
' strNewText - If fLinkOnly is False (such that the original text associated
' with the link is removed), then the hyperlink will be replaced
' with this string.
Public Function DeleteLinks(fLinkOnly As Boolean, Optional strNewText As String)
' Find all the hyperlinks and delete them.

Dim lnkHL As Hyperlink
Dim rngText As Selection
Dim lngRngStart As Long

' Iterate through the hyperlinks and take the
' appropriate action.
While ThisDocument.Hyperlinks.Count > 0
Set lnkHL = ThisDocument.Hyperlinks(1)
If fLinkOnly Then
' Keep the text, but delete the link.
lnkHL.Delete
Else
' Remove everything...replace with the specified text,
' if supplied.
lnkHL.Range.Select
If Not IsMissing(strNewText) Then
Selection.Delete
Selection.Text = strNewText
Else
lnkHL.Delete
Selection.Delete
End If
End If
Wend

End Function

Paste this into a VBA module in Word, then run it with the desired parameters.

ScaryBinary
11-14-2004, 01:11 PM
Woops, forgot an example. You could enter this in the debug window in the VB Editor...

?DeleteLinks(False, "<DELETED>")

That would delete the hyperlink, text and all, and insert "<DELETED>" in it's spot.

?DeleteLinks(True)

would just remove the "link-ness" of the hyperlink and keep whatever text was displayed for the hyperlink.

orkboss
11-14-2004, 03:42 PM
Thanks for your help. Unfortunately I have only the very basic understanding of macros and I cannot get your code to work. I find the visual basic editor complicated and confusing. I have only been able to edit my own macros by creating them from word and then going to the edit function.

All my efforts are of the variety

Sub delink()



End Sub

I need to have an understanding of the functions of the visiual basic editor, I don't undertand the way the projects are laid out.

ScaryBinary
11-14-2004, 04:18 PM
In Word, choose Tools > Macros ... > Visual Basic Editor.

In the Editor window, select Insert > Module. This will create a new Visual Basic module. Copy the text from my previous post into this new module.

In the Immediate window, type something like:

?DeleteLinks(True)

to run the function.

I don't know if my copy of Word is hosed or what, but I couldn't get the function to show up in the Macros list, even if I recorded a new macro and then replaced it's code with mine.

orkboss
11-15-2004, 01:28 PM
I tried your instructions. It was difficult following them as I was unsure about the immediate window until I found it is a window you can open. I typed in the run command and got an error message.

ScaryBinary
11-15-2004, 05:20 PM
Make sure you didn't accidently paste the function in the editor twice (even if it's in different modules). The error message indicates that you have more than one function with the same name.

orkboss
11-15-2004, 07:01 PM
I had pasted it in a different module. The macro works very well although I do not understand the coding. I will have to pick up a book to teach myself the coding. I managed to get a macro working in excel to select all and delete hyperlinks but when trying to record the command to delete hyperlinks in word I found I could not right click on the link while recording a macro.