Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andysjunkmale@gmail.com - 27 Nov 2006 22:28 GMT
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
Jean-Guy Marcil - 28 Nov 2006 00:07 GMT
andysjunkmale@gmail.com  was telling us:
andysjunkmale@gmail.com nous racontait que :

> Hello. Here's my dilemma. Hope someone out there can help.
>
[quoted text clipped - 40 lines]
>    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..

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Greg Maxey - 28 Nov 2006 00:38 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> andysjunkmale@gmail.com  was telling us:
> andysjunkmale@gmail.com nous racontait que :
[quoted text clipped - 73 lines]
>
> Look up the Add method for the Hyperlinks object for more options..
Jay Freedman - 28 Nov 2006 02:33 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>JGM,
>
[quoted text clipped - 96 lines]
>>
>> Look up the Add method for the Hyperlinks object for more options..
Greg Maxey - 28 Nov 2006 02:54 GMT
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?

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg, the solution to that problem is to add more characters to the
> Cset argument of the .MoveEndUntil method. For example, to pick up
[quoted text clipped - 122 lines]
>>>
>>> Look up the Add method for the Hyperlinks object for more options..
Jay Freedman - 28 Nov 2006 04:08 GMT
>Jay,
>
[quoted text clipped - 9 lines]
>
>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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Greg Maxey - 28 Nov 2006 05:36 GMT
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 ;-)

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Jay,
>>
[quoted text clipped - 25 lines]
> .AutoFormatAsYouTypeReplaceHyperlinks at all. It has no bearing on
> what the macro does.
andysjunkmale@gmail.com - 28 Nov 2006 20:23 GMT
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

> Jay,
>
[quoted text clipped - 38 lines]
> > .AutoFormatAsYouTypeReplaceHyperlinks at all. It has no bearing on
> > what the macro does.
Jean-Guy Marcil - 28 Nov 2006 05:02 GMT
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
[quoted text clipped - 5 lines]
> 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!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.