Include Target attribute of hyperlink

C

CompleteNewb

A Word guru was nice enough to provide me with this snippet that makes Word
hyperlinks HTML ready (find text that's a hyperlink, and wraps said text in
the <a href> tags for export to a text file that's web-ready (and before you
suggest Word's save as html, I can't use that because of the pages and pages
of Microsoft-only html language Word uses; it's far from a clean web page).

One thing this doesn't do, though (which I forgot to mention I needed to
do), is include the Target attribute (ie. when a hyperlink is set to open in
a new window, I need the "Target="_blank" to be included in the <a href>
tag.

So, if I have a hyperlink in Word that is supposed to open the page in a new
browser window, I need to wrap that text with:

<a href="www.whatever.com" target='_blank">This is the hyperlink</a>

Some hyperlinks are supposed to open in a new window, and some are not.

Can someone help me incorporate this particular requirement into the
following code (which currently works great except for not using the target
attribute):

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


Thanks for any help on this, and for taking the time.
 
J

Jay Freedman

A Word guru was nice enough to provide me with this snippet that makes Word
hyperlinks HTML ready (find text that's a hyperlink, and wraps said text in
the <a href> tags for export to a text file that's web-ready (and before you
suggest Word's save as html, I can't use that because of the pages and pages
of Microsoft-only html language Word uses; it's far from a clean web page).

One thing this doesn't do, though (which I forgot to mention I needed to
do), is include the Target attribute (ie. when a hyperlink is set to open in
a new window, I need the "Target="_blank" to be included in the <a href>
tag.

So, if I have a hyperlink in Word that is supposed to open the page in a new
browser window, I need to wrap that text with:

<a href="www.whatever.com" target='_blank">This is the hyperlink</a>

Some hyperlinks are supposed to open in a new window, and some are not.

Can someone help me incorporate this particular requirement into the
following code (which currently works great except for not using the target
attribute):

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


Thanks for any help on this, and for taking the time.

Change the lines
strText = strText & qt & ">" & _
hLink.TextToDisplay & "</a>"

to

strText = strText & qt & "target=" & qt & "_blank" & qt & ">" & _
hLink.TextToDisplay & "</a>"
 
C

CompleteNewb

Jay:

Thanks for the response, but not all of the hyperlinks are supposed to open
in a new window, just if that's specified in the hyperlink when it was made
with Word's "Insert Hyperlink," where you can choose the target as well as
the URL. This document is hundreds of pages long, with thosands of
hyperlinks, and any number of them could have a target set as new window or
NOT. So I can't just put the string in all of them to open in a new window,
just the ones that are supposed to (again, just the ones where, when the
hyperlink was inserted using Word's "Insert Hyperlink" dialog box, the
Target Fram was chosen as New Window).

Thanks for the response, though.
 
C

CompleteNewb

Nevermind, I figured it out.

In case other people run into this, I just inserted an additional If Then:

If hLink.Target <> "" Then
strText = strText & qt & " " & "target=" & qt & hLink.Target
End If

So, to take into account anchors and also a target frame (if one was set to
"New Window"), the whole thing is like this:

Sub targettest()

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

If hLink.Target <> "" Then
strText = strText & qt & " " & "target=" & qt & hLink.Target
End If

strText = strText & qt & ">" & _
hLink.TextToDisplay & "</a>"

oRg.Text = strText
Next
End Sub

Now, this only assumes that any target set is a new window (which is the
only specified target my particular document would have). To account for
different targets being set in different hyperlinks (ie. one goes to new
window, one goes to existing open browser, one goes to particular frame,
etc.), I'm assuming you'd need some nested If Thens or a Select Case to set
the string to different values depending on the value of each target
property.

Thanks again, everybody.
 

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