Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
CompleteNewb CompleteNewb is offline
external usenet poster
 
Posts: 7
Default Replacing hyperlink with the html equivalent

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.


  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default Replacing hyperlink with the html equivalent

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.


On Sun, 20 Jan 2008 19:47:41 -0500, "CompleteNewb"
wrote:

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.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
  #3   Report Post  
Posted to microsoft.public.word.docmanagement
CompleteNewb CompleteNewb is offline
external usenet poster
 
Posts: 7
Default Replacing hyperlink with the html equivalent

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!!!)


"Jay Freedman" wrote in message
...
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.


On Sun, 20 Jan 2008 19:47:41 -0500, "CompleteNewb"

wrote:

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.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.



  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Bob Buckland ?:-\) Bob   Buckland ?:-\) is offline
external usenet poster
 
Posts: 2,073
Default Replacing hyperlink with the html equivalent

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.

============
"CompleteNewb" wrote in message . ..
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*



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing text, instead of replacing, text inserts alongside word Margaret Minker Microsoft Word Help 4 December 9th 05 03:52 PM
Make a Hyperlink to open another html file in Word - is thispossible? gap Microsoft Word Help 2 May 28th 05 05:57 AM
mailmerge hyperlink to html file Jeremy Newkirk Mailmerge 0 March 13th 05 09:08 PM
how to make a relative path in a hyperlink in a word-html file? Word Hyperlinks Microsoft Word Help 1 December 15th 04 02:31 PM


All times are GMT +1. The time now is 08:33 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"