PDA

Click to See Complete Forum and Search --> : Extract URL in Word


benk
Jul 16th, 2005, 03:08 AM
Hi, I'm not really a programmer, but I've found VBA to be useful in many areas of my work. But right now I'm a bit stuck. I need a way to search through documents and extract hypertext URLs as HTML code so I can cut and paste into a web CMS. I believe it can be done. Here's an example of what I want to achieve:

I want to turn:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque a.

Into:
Lorem ipsum dolor sit amet, <a href="#">consectetuer adipiscing</a> elit. Quisque a.

Does anyone know how to perform this operation? Really appreciate any help.

JustinLabenne
Jul 16th, 2005, 11:47 AM
Maybe like this?

Option Explicit

Sub Extraction()
Dim hlnk As Hyperlink
Dim C As String
Dim d As String

For Each hlnk In ActiveDocument.Hyperlinks
C = "<a href=""#"">"
d = "</a>"

hlnk.TextToDisplay = C & hlnk.TextToDisplay & d

Next hlnk
End Sub

benk
Jul 16th, 2005, 01:17 PM
Thanks Justin, but just one problem. I need the URL that was extracted to replace the "#". Can that be done? Should I replace the "#" with "hlnk.hyperlink"?

Also, is it possible to make the text not a link anymore?

Sorry, I'm really not familiar with the code. Thanks again for the help.

benk
Jul 17th, 2005, 01:33 AM
Oh, I got it. it's "hlnk.Address". =) Thanks!

JustinLabenne
Jul 17th, 2005, 01:37 AM
Try this, had an issue at first with not getting the hyperlinks removed, stubborn little buggers,

Option Explicit

Sub Extraction()
Dim hlnk As Hyperlink
Dim C As String
Dim d As String
Do Until ThisDocument.Hyperlinks.Count = 0
For Each hlnk In ActiveDocument.Hyperlinks
C = "<a href=" & hlnk.Address & ">"
d = "</a>"
With hlnk
.TextToDisplay = C & hlnk.TextToDisplay & d
.Delete
End With
Next hlnk
Loop
End Sub