Hard return to activate "dead" hyperlink in Word...

A

andysjunkmale

Hello. Here's my dilemma. Hope someone out there can help.

I'm taking data from one program and pasting into Word (with this other
program, there's really no export function, so cutting and pasting is
the only way I can do this). The problem is that when I paste the
information into Word, all of the hyperlinks are "dead" (that is, when
you try to Ctrl+click the link, it does nothing).

Of course, I can manually enter a hard return at the end of the
hyperlink to activate it, but I have a TON of links and don't really
want to spend time doing this. I've tried creating a macro to do this,
and it does everything it should need to do to re-activate the
hyperlink, but it's not.

I'm actually in the VBA window, stepping through the script. I see it
locate the letters "http" and move to the end of the link, then I see
what looks to me like a Return/Enter, but the link never "activates."
Here's the VBA code. Anyone know how I can generate a hard return that
will activate a hyperlink?

Thanks,

Andy

==================

Selection.Find.ClearFormatting
With Selection.Find
.Text = "http"
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
.Replacement.Text = ""
Selection.TypeParagraph
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Hello. Here's my dilemma. Hope someone out there can help.

I'm taking data from one program and pasting into Word (with this
other program, there's really no export function, so cutting and
pasting is the only way I can do this). The problem is that when I
paste the information into Word, all of the hyperlinks are "dead"
(that is, when you try to Ctrl+click the link, it does nothing).

Of course, I can manually enter a hard return at the end of the
hyperlink to activate it, but I have a TON of links and don't really
want to spend time doing this. I've tried creating a macro to do this,
and it does everything it should need to do to re-activate the
hyperlink, but it's not.

I'm actually in the VBA window, stepping through the script. I see it
locate the letters "http" and move to the end of the link, then I see
what looks to me like a Return/Enter, but the link never "activates."
Here's the VBA code. Anyone know how I can generate a hard return that
will activate a hyperlink?

Thanks,

Andy

==================

Selection.Find.ClearFormatting
With Selection.Find
.Text = "http"
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph
.Replacement.Text = ""
Selection.TypeParagraph
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

You are approaching this the wrong way..

Here is a VBA example to create a hyperlink:

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:="http:\\www.microsoft.com"

Together with your code (modified, because as is, it will not work), you
would get something like:

'_______________________________________
Dim rgeDoc As Range
Dim rgeFound As Range

Set rgeDoc = ActiveDocument.Range

With rgeDoc.Find
.Text = "http"
.Wrap = wdFindStop
Do While .Execute
.Parent.MoveEndUntil " "
ActiveDocument.Hyperlinks.Add Anchor:=.Parent, _
Address:="Parent.text"
.Parent.Collapse wdCollapseEnd
Loop
End With
'_______________________________________

Look up the Add method for the Hyperlinks object for more options..

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

JGM,

I was studying something along a similar line:

Sub FindAndActivateDeadHyperlinks2()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "http"
.Wrap = wdFindStop
Do While .Execute
oRng.MoveEndUntil Cset:= " ", Count:=wdForward
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:="oRng.text"
oRng.Collapse wdCollapseEnd
Loop
End With
End Sub

This works similiar to yours, but like yours it falls short if the hyperlink
is not followed by a space. If it occurs at the end of a line or if
punctuation immediately follows then only the http is hyperlinked.
 
J

Jay Freedman

Greg, the solution to that problem is to add more characters to the
Cset argument of the .MoveEndUntil method. For example, to pick up
hyperlinks that end at either a space or a paragraph mark, use

oRng.MoveEndUntil Cset:=" " & vbCr, Count:=wdForward

Adding other punctuation to the list could be dangerous, since
periods, commas, question marks, and others could be legitimate parts
of some hyperlinks rather than stop characters.

For Andy, a word of explanation about why this approach is necessary:
When you type an address into a document, the part of Word that
recognizes the text and changes it into the hyperlink is the
"AutoFormat As You Type" feature (Tools > AutoCorrect Options >
AutoFormat As You Type > Internet and network paths with hyperlinks).
This feature is triggered *only* by typing on the keyboard, not by
anything you can do in VBA -- the Selection.TypeParagraph command
won't do it.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Greg Maxey

Jay,

Thanks. In view of what you just explained, wouldn't something like:

Sub ScratchMacro()
With Options
.AutoFormatAsYouTypeReplaceHyperlinks = True
.AutoFormatReplaceHyperlinks = True
End With
ActiveDocument.Range.AutoFormat
End Sub

Do a nice job of it?
 
J

Jay Freedman

Jay,

Thanks. In view of what you just explained, wouldn't something like:

Sub ScratchMacro()
With Options
.AutoFormatAsYouTypeReplaceHyperlinks = True
.AutoFormatReplaceHyperlinks = True
End With
ActiveDocument.Range.AutoFormat
End Sub

Do a nice job of it?

Yes it would, except that firing the AutoFormat could have a lot more
consequences than just creating the hyperlinks. It depends on what
else is checked in the AutoFormat options and what text that matches
in the document.

To be safe, you'd have to (a) save all the user's current AutoFormat
options in local variables, (b) set all of the AutoFormat options to
False except the .AutoFormatReplaceHyperlinks one, (c) fire the
AutoFormat method, and (d) put back the user's options to their
original state. Not difficult, but tedious.

Also, to achieve this, you don't need to work with
..AutoFormatAsYouTypeReplaceHyperlinks at all. It has no bearing on
what the macro does.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

Jean-Guy Marcil

Jay Freedman was telling us:
Jay Freedman nous racontait que :
Greg, the solution to that problem is to add more characters to the
Cset argument of the .MoveEndUntil method. For example, to pick up
hyperlinks that end at either a space or a paragraph mark, use

oRng.MoveEndUntil Cset:=" " & vbCr, Count:=wdForward

Adding other punctuation to the list could be dangerous, since
periods, commas, question marks, and others could be legitimate parts
of some hyperlinks rather than stop characters.

Silly me! I just assumed that the hyperlinks would be delimited by spaces...

Of course, you would have to check for more than that!


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Jay,

All true. I suppose what I really meant to say was that in view of what you
had pointed out, the simplest solution for the OP might be simply to set his
AutoFormat options appropriately and then AutoFormat. Leave VBA alone for
another day or another challenge ;-)
 
A

andysjunkmale

A big THANKS to everyone, but especially those who mentioned using
AutoFormat. That
was right on the mark. All I had to do was "re-run" autoformat. When I
did this,
all of the "dead" hyperlinks were activated. It looks like I was trying
to make this too complicated by using VBA :( Sometimes it pays to
keep it simple!

Andy
 

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