|
|
|
date: Fri, 1 Feb 2008 13:33:15 -0500,
group: microsoft.public.word.vba.beginners
back
Include Target attribute of hyperlink
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.
date: Fri, 1 Feb 2008 13:33:15 -0500
author: CompleteNewb
Re: Include Target attribute of hyperlink
On Fri, 1 Feb 2008 13:33:15 -0500, "CompleteNewb"
wrote:
>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>"
--
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.
date: Sat, 02 Feb 2008 08:19:16 -0500
author: Jay Freedman
Re: Include Target attribute of hyperlink
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.
"Jay Freedman" wrote in message
news:qpq6q35789o4k9r28075o3qsg3d6m1p154@4ax.com...
> On Fri, 1 Feb 2008 13:33:15 -0500, "CompleteNewb"
>
> wrote:
>
>>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>"
>
> --
> 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.
date: Sat, 2 Feb 2008 10:42:01 -0500
author: CompleteNewb
Re: Include Target attribute of hyperlink
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.
"CompleteNewb" wrote in message
news:XcydnYptLblM-T7anZ2dnUVZ_jmdnZ2d@comcast.com...
>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.
>
>
date: Sat, 2 Feb 2008 11:15:48 -0500
author: CompleteNewb
|
|