Hyperlinks pasted by a macro do not work!

T

Thomas Lindberg

I have a macro that copies text from a *.rtf document and paste the text
(after some editing) into a *.doc document.
The *.rtf document is generated in an application that opens Word and
presents the document there as 'filename.rtf (Read-Only)'.
My macro copies selected parts and paste them, works as planned

____exept____

if the text is or includes an URL, typically like
http://www.microsoft.com/danmark/products/windows/.

Then it is just plain text, not blue, not underlined and does not work as a
hyperlink.

If I _manually_ insert a blank directly after the URL then thenURL gets
underlined blue and works as a hyperlink.
If I let the macro insert the blank nothing happens.

WinXPPro and Office 2003, both English.

Suggestion for workaround, please!


Thomas
 
D

Doug Robbins - Word MVP

Use the following code:

Dim linkrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="//", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Set linkrange = Selection.Bookmarks("\line").Range
linkrange.End = linkrange.End - 1
ActiveDocument.Hyperlinks.Add Anchor:=linkrange, Address:= _
linkrange.Text
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Tony Jollans

After you have pasted you can autoformat the pasted text to mimic what
happens when you manually enter the text, something like this

Start = Selection.Start
Selection.Paste
ActiveDocument.Range(Start, Selection.End).AutoFormat
 
T

Thomas Lindberg

The hook to your proposal is that the URL is embedded in a, sometimes
large,
chunk of text.
I have not had time to test Doug's proposal yet.

Thanks anyhow!

Thomas
 
D

Doug Robbins - Word MVP

Tony's method is simpler than mine and can be further simplified to using
just the following:

ActiveDocument.Range.AutoFormat

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jay Freedman

The AutoFormat statement will work, but only if the corresponding
option is checked in the Tools > AutoCorrect Options > AutoFormat
dialog. Further, all the other options that the user has checked at
the time will also be applied, which may not be what you want.

This version saves all the current settings in that Options page, sets
the hyperlink option and turns off all the others, does the
autoformatting, and finally puts back all the original settings.

Sub MyFormatHyperlinks()
Dim bHyper As Boolean, bHeads As Boolean
Dim bLists As Boolean, bBullets As Boolean
Dim bParas As Boolean, bQuotes As Boolean
Dim bSymbol As Boolean, bOrdinal As Boolean
Dim bFract As Boolean, bPlain As Boolean
Dim bStyles As Boolean, bMail As Boolean

With Options
' preserve user's settings
bHyper = .AutoFormatReplaceHyperlinks
bHeads = .AutoFormatApplyHeadings
bLists = .AutoFormatApplyLists
bBullets = .AutoFormatApplyBulletedLists
bParas = .AutoFormatApplyOtherParas
bQuotes = .AutoFormatReplaceQuotes
bSymbol = .AutoFormatReplaceSymbols
bOrdinal = .AutoFormatReplaceOrdinals
bFract = .AutoFormatReplaceFractions
bPlain = .AutoFormatReplacePlainTextEmphasis
bStyles = .AutoFormatPreserveStyles
bMail = .AutoFormatPlainTextWordMail

' set up only ReplaceHyperlinks
.AutoFormatReplaceHyperlinks = True
.AutoFormatApplyHeadings = False
.AutoFormatApplyLists = False
.AutoFormatApplyBulletedLists = False
.AutoFormatApplyOtherParas = False
.AutoFormatReplaceQuotes = False
.AutoFormatReplaceSymbols = False
.AutoFormatReplaceOrdinals = False
.AutoFormatReplaceFractions = False
.AutoFormatReplacePlainTextEmphasis = False
.AutoFormatPreserveStyles = False
.AutoFormatPlainTextWordMail = False

' do the work
ActiveDocument.Range.AutoFormat

' restore user's settings
.AutoFormatReplaceHyperlinks = bHyper
.AutoFormatApplyHeadings = bHeads
.AutoFormatApplyLists = bLists
.AutoFormatApplyBulletedLists = bBullets
.AutoFormatApplyOtherParas = bParas
.AutoFormatReplaceQuotes = bQuotes
.AutoFormatReplaceSymbols = bSymbol
.AutoFormatReplaceOrdinals = bOrdinal
.AutoFormatReplaceFractions = bFract
.AutoFormatReplacePlainTextEmphasis = bPlain
.AutoFormatPreserveStyles = bStyles
.AutoFormatPlainTextWordMail = bMail
End With
End Sub

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

Thomas Lindberg

macro works of course but it has some unwanted side effects:
- It removes one of double end-of-para marks.
- At least once ( in my 155 pages test doc.), if the para starts with _ONE_
blank,
the preceeding end-of-para mark was removed and hence the two paras were
merged.

I think I have to change strategy to something like this:

While NOT AtEndOfDocument
Find StartOfURL(URL_Start) 'Standard Word Selection.Find
FindEndOfURL(URL_End) 'Search for first character NOT
PERMITTED in an URL
Selection.SetRange URL_Start URL_End
URL=Selection.Text
ActiveDocument.Hyperlinks.Add Anchor:= Selection.Range Address URL
Wend

No problem to search for URL_Start, all (my) URLs start with 'http://'
But I have no knowledge the end of then URLs: which characters are _NOT_
permitted in an URL???
I know that blanks and comma are not permitted but others??


Thanks for any help!!


Thomas
 
D

Doug Robbins - Word MVP

Have you tried the code that I gave you in my first response?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Thomas Lindberg

I have not actually tested it but as I _read_ it the code selects from
'//' to 'end of line' and in my case
the URLs are embedded in running text like

"Blaha blaha ... includes an URL, typically like
http://www.microsoft.com/windows more blaha blaha". EOL.

So the only way, I figure, is to find the first character _NOT_ permitted in
an URL and then define the URL as all between
'http://' and the character just before that not permitted one.

Obviously the blank char is not permitted as Word detects that a _manually_
inserted blank as
'first not permitted URL character'. But there are others, I presume.
Will search for some definition of a legeally constructed URL.

Thomas
 
T

Tony Jollans

You will find the general case quite complex - for example, a trailing
period is not part of a url.

Does Jay's code not work? It should ignore all but valid hyperlinks.
 
D

Doug Robbins - Word MVP

The following will handle your situation:

Dim linkrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="http://", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set linkrange = Selection.Range
linkrange.End = Selection.Bookmarks("\Line").Range.End
linkrange.End = linkrange.Start + InStr(linkrange, " ") - 1
ActiveDocument.Hyperlinks.Add Anchor:=linkrange, Address:= _
linkrange.Text
Selection.Collapse wdCollapseEnd
Loop
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

Thomas Lindberg was telling us:
Thomas Lindberg nous racontait que :
I have not actually tested it but as I _read_ it the code selects
from '//' to 'end of line' and in my case
the URLs are embedded in running text like

"Blaha blaha ... includes an URL, typically like
http://www.microsoft.com/windows more blaha blaha". EOL.

So the only way, I figure, is to find the first character _NOT_
permitted in an URL and then define the URL as all between
'http://' and the character just before that not permitted one.

Obviously the blank char is not permitted as Word detects that a
_manually_ inserted blank as
'first not permitted URL character'. But there are others, I presume.
Will search for some definition of a legeally constructed URL.

Assuming that the source of the pasted text contains valid hyperlinks, we
can say that the said hyperlinks are all formatted with underlining.
So, instead of looking for valid/invalid URL characters, look for
underlining. Here is a Doug's macro modified to that end:

Dim linkrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="http://", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set linkrange = Selection.Range
Do
linkrange.End = linkrange.End + 1
Loop Until linkrange.Next.Characters(1).Underline = wdUnderlineNone
ActiveDocument.Hyperlinks.Add Anchor:=linkrange, Address:= _
linkrange.Text
Selection.Collapse wdCollapseEnd
Loop
End With


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

Thomas Lindberg

y for late response, but sure, your code works, but for a few
(incorrectly) constructed URLs:
those URLs were manually created out of file names (including path), the
file name included blanks and other 'odd' characters.
That problem is solved, sorry for the trouble caused.

But an unresolved question remanins:
Why does the Word macro not detect that a pasted correct URL that is part
of/embedded in a text string
is a URL and makes it blue and underlined until the URL is _manually_
terminated with (sometimes an another ) blank?
I'm just curious!

Thomas
 
T

Thomas Lindberg

Jean-Guy Marcil said:
Thomas Lindberg was telling us:
Thomas Lindberg nous racontait que :


Assuming that the source of the pasted text contains valid hyperlinks, we
can say that the said hyperlinks are all formatted with underlining.
So, instead of looking for valid/invalid URL characters, look for
underlining. Here is a Doug's macro modified to that end:

Dim linkrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="http://", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set linkrange = Selection.Range
Do
linkrange.End = linkrange.End + 1
Loop Until linkrange.Next.Characters(1).Underline = wdUnderlineNone
ActiveDocument.Hyperlinks.Add Anchor:=linkrange, Address:= _
linkrange.Text
Selection.Collapse wdCollapseEnd
Loop
End With


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

Jean-Guy,

The URLs that caused me problems are embedded and part of plain text, no
formatting and no underlining.
I can hence not detect the 'end of underline'.

Thomas
 

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