Replacing hyperlink with the html equivalent

C

CompleteNewb

Hi All.

I'm at the end of a long series of experimentation and getting excellent
from you fine folks, and in my task of html-ifying all the text in a Word
document (turning bold, italics, line breaks, etc. into the HTML code so
that the Word document is web-ready html text), the last step is making the
hyperlinks web-ready.

So, wherever there is a hyperlink in the Word text, I need to replace it
with the HTML for a hyperlink.

Example, Word shows:

To see for yourself, click here.

In the above, the words "click here" are blue, and floating the mouse over
them shows that the hyperlink is http://www.example.com

My macro needs to turn that into:

To see for yourself, <a href="http://www.example.com">click here</a>.

The hyperlinks could be to anywhere, and the text that is hyperlinked could
be anything, and the document is hundreds of pages long.

Is there a way to automate this? I have had a very hard time finding out
how to do this.

Any help, advice, code snippets, etc. would be GREATLY appreciated. Thanks
for taking the time.
 
J

Jay Freedman

Hi Newb,

One of the bits of knowledge you should try to pick up is which newsgroup is the
most appropriate for particular questions. Beginning VBA is usually best covered
in microsoft.public.word.vba.beginners. More advice on this is at
http://word.mvps.org/FindHelp/WhichNewgrp.htm.

The code you're looking for is something like this:

Sub demo()
Dim nLink As Long
Dim hLink As Hyperlink
Dim strText As String
Dim oRg As Range
Const qt = """"

For nLink = ActiveDocument.Hyperlinks.Count To 1 Step -1
Set hLink = ActiveDocument.Hyperlinks(nLink)
Set oRg = hLink.Range

strText = "<a href=" & qt & hLink.Address
If hLink.SubAddress <> "" Then
strText = strText & "#" & hLink.SubAddress
End If
strText = strText & qt & ">" & _
hLink.TextToDisplay & "</a>"

oRg.Text = strText
Next
End Sub

Some notes on the code:

- The For loop works back from the end of the document to the beginning, instead
of working from the beginning to the end or using a For Each loop. This is to
avoid a logic trap that happens because the loop deletes each hyperlink from the
Hyperlinks collection as it replaces the range with plain text.

- Some hyperlinks have just an Address (such as "http://www.example.com"), while
others may have both an Address and a SubAddress that points to a bookmark in a
document or to a named anchor in a web page (the part after the # symbol in
"http://www.example.com#anchor"). If the SubAddress is blank, you don't want the
# to be included.
 
C

CompleteNewb

Jay:

A sincere thanks for your code and for the posting advice. I posted here
because I have seen the most helpful Word-specific code stuff here (much
more so than even in the microsoft.public.[whatever MS app I'm looking for
help on].coding (or .programming).

But your point is well taken, and I have not visited your suggested group
before, so I'll certainly add it to my subscriptions.

Thanks again, that was a whiz-bang bit of VBA! (and I especially appreciate
the notes, such as the logic trap, etc. That was extremely helpful in terms
of helping me not only use the code but apply the concept elsewhere; that
logic trap part is something that would have had me confounded for hours or
days!!!)
 
B

Bob Buckland ?:-\)

What version of Word are you using?

Are you using Word's 'Save as Web Page,filtered' file type to create the HTML or are you using Word to work on HTML source code then
saving as 'Plain text' and giving the file name a .htm ending.

If you're using Word's 'Save as Web Page, filtered' then the hyperlinks will automatically be converted to <a href=... form.

============
Hi All.

I'm at the end of a long series of experimentation and getting excellent
from you fine folks, and in my task of html-ifying all the text in a Word
document (turning bold, italics, line breaks, etc. into the HTML code so
that the Word document is web-ready html text), the last step is making the
hyperlinks web-ready.

So, wherever there is a hyperlink in the Word text, I need to replace it
with the HTML for a hyperlink.

Example, Word shows:

To see for yourself, click here.

In the above, the words "click here" are blue, and floating the mouse over
them shows that the hyperlink is http://www.example.com

My macro needs to turn that into:

To see for yourself, <a href="http://www.example.com">click here</a>.

The hyperlinks could be to anywhere, and the text that is hyperlinked could
be anything, and the document is hundreds of pages long.

Is there a way to automate this? I have had a very hard time finding out
how to do this.

Any help, advice, code snippets, etc. would be GREATLY appreciated. Thanks
for taking the time. >>
--

Bob Buckland ?:)
MS Office System Products MVP

*Courtesy is not expensive and can pay big dividends*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top