MS Office Forum / Word / Programming / January 2006
Hyperlinks pasted by a macro do not work!
|
|
Thread rating:  |
Thomas Lindberg - 14 Jan 2006 15:22 GMT 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
Doug Robbins - Word MVP - 14 Jan 2006 16:19 GMT 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
 Signature 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
>I have a macro that copies text from a *.rtf document and paste the text >(after some editing) into a *.doc document. [quoted text clipped - 19 lines] > > Thomas Tony Jollans - 14 Jan 2006 17:25 GMT 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
-- Enjoy, Tony
> I have a macro that copies text from a *.rtf document and paste the text > (after some editing) into a *.doc document. [quoted text clipped - 19 lines] > > Thomas Thomas Lindberg - 14 Jan 2006 17:32 GMT 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
> After you have pasted you can autoformat the pasted text to mimic what > happens when you manually enter the text, something like this [quoted text clipped - 31 lines] >> >> Thomas Doug Robbins - Word MVP - 14 Jan 2006 17:59 GMT Tony's method is simpler than mine and can be further simplified to using just the following:
ActiveDocument.Range.AutoFormat
 Signature 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
> The hook to your proposal is that the URL is embedded in a, sometimes > large, [quoted text clipped - 43 lines] >>> >>> Thomas Jay Freedman - 14 Jan 2006 21:26 GMT 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 FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>Tony's method is simpler than mine and can be further simplified to using >just the following: [quoted text clipped - 48 lines] >>>> >>>> Thomas Thomas Lindberg - 15 Jan 2006 16:42 GMT 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
> The AutoFormat statement will work, but only if the corresponding > option is checked in the Tools > AutoCorrect Options > AutoFormat [quoted text clipped - 120 lines] >>>>> >>>>> Thomas Doug Robbins - Word MVP - 15 Jan 2006 20:22 GMT Have you tried the code that I gave you in my first response?
 Signature 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
> macro works of course but it has some unwanted side effects: > - It removes one of double end-of-para marks. [quoted text clipped - 148 lines] >>>>>> >>>>>> Thomas Thomas Lindberg - 16 Jan 2006 06:17 GMT 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
>>>>>>> Then it is just plain text, not blue, not > Have you tried the code that I gave you in my first response? [quoted text clipped - 154 lines] >>>>>>> >>>>>>> Thomas Tony Jollans - 16 Jan 2006 07:41 GMT 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.
-- Enjoy, Tony
> I have not actually tested it but as I _read_ it the code selects from > '//' to 'end of line' and in my case [quoted text clipped - 188 lines] > >>>>>>> > >>>>>>> Thomas Doug Robbins - Word MVP - 16 Jan 2006 12:22 GMT 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
 Signature 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
>I have not actually tested it but as I _read_ it the code selects from >'//' to 'end of line' and in my case [quoted text clipped - 172 lines] >>>>>>>> >>>>>>>> Thomas Thomas Lindberg - 22 Jan 2006 16:40 GMT 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
> The following will handle your situation: > [quoted text clipped - 192 lines] >>>>>>>>> >>>>>>>>> Thomas Jean-Guy Marcil - 16 Jan 2006 21:49 GMT 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 [quoted text clipped - 11 lines] > '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
 Signature Salut! _______________________________________ Jean-Guy Marcil - Word MVP jmarcilREMOVE@CAPSsympatico.caTHISTOO Word MVP site: http://www.word.mvps.org
Thomas Lindberg - 22 Jan 2006 16:43 GMT > Thomas Lindberg was telling us: > Thomas Lindberg nous racontait que : [quoted text clipped - 35 lines] > Loop > End With 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
|
|
|