PDA

View Full Version : Activate Web Addresses as Hyperlinks in Word



musgrave2
02-28-2005, 07:41 PM
Hi,

I want a button or some macro to allow the user to activate the hypertext that's been inserted by autotext. I already created the template to add the grammar hyperlinks, and now I just need an instruction that will turn on the links.

This is a template for teachers to use in students' papers in a program called Office Hours. This program allows participants to share applications in real time.

Thanks!

:hi: Jim Musgrave

Jacob Hilderbrand
02-28-2005, 08:03 PM
Can you post the template for me to work with?

Anne Troy
02-28-2005, 08:03 PM
Hi, Jim! Glad to see you made it.

We should build Jim a toolbar, using VBA, with just one button on it. When clicked, it should find all instances of text beginning with http:// and make it a hyperlink. The only problem I have in doing this macro myself is that the hyperlink could be followed by a space or a period, I'm sure, neither of which should be part of the hyperlink address. Also, we don't need to hide the address or anything; just make it an active hyperlink using the same address.

Oh...if somebody can give me the code for the hyperlinks, I can build the toolbar. :)

Jim: You might want to consider adding your autotext entries to the toolbar, which would ideally float. If you'd like to do that, let us know.

musgrave2
02-28-2005, 08:18 PM
Is it possible to (easily) copy the hypertext links to the program you're suggesting? :devil:

Anne Troy
02-28-2005, 08:24 PM
We wouldn't even need to, Jim. I would assign buttons for each one. Behind each button, we'd put a macro. Behind each macro, we'd put one of your autotext entries. The bother would be that if you wanted to add a new hyperlink, you'd have to code it to add it to the toolbar.

However, I would be that our Jake (DRJ) could probably make that hyperlink macro button for you, put it on a toolbar, and then add all the autotext entries as a dropdown to it. My only gripe on that would be that I wouldn't wanna see all Word's default autotext in it, would you? In which case, we'd want to do the macro thing.

Now, if you're reasonably intelligent, and I think you are, you could easily add an autotext, and edit the toolbar and other VBA code to add a new entry to the toolbar. But that's up to you.

There's a really good example here: http://www.vbaexpress.com/kb/getarticle.php?kb_id=14 The only difference would be that those "subs" you see that insert letter closings would be autotext entries instead.

I did something VERY similar here: http://www.vbaexpress.com/forum/showthread.php?t=877

Only his autotext entries included graphics. :)
You might want to download his file, though, since it'd pretty much show what I mean.

musgrave2
02-28-2005, 08:58 PM
Yes, I certainly could make-out the logic and add my own hypertext. That's how I learned .html!

Thanks, this sounds awesome.

Jacob Hilderbrand
02-28-2005, 09:01 PM
Please attach a zip file with an example of your data so that I can see what we are working with.

Anne Troy
02-28-2005, 09:03 PM
We didn't mention that Jim first wanted to be able to use Autotext entries, but he didn't want the formatting copied over because they're being inserted into "anyone's" files, and the formatting is unknown. So, we already gave him a macro (off VBAX and I can't share the macro) that automatically makes autotext entries "unformatted text". That means that it takes on the formatting at the insertion point. Now, when we do that, Jim loses his active hyperlinks. With this macro, we're putting them back.

Winzip and then upload (attach) the file you sent me, Jim. Or I can do it if you can't figure it out. To attach a file, you gotta hit the "Go Advanced" button below the reply box.

musgrave2
03-09-2005, 03:08 PM
Any progress on this yet?

Thanks,

Jim

TonyJollans
03-09-2005, 04:00 PM
I'm not good with autotext so I don't totally understand the requirement but this code will find all text beginning "http://" and ending either space or paragraph mark. It then drops the last character and if the (new) last character is a dot it drops that as well and then converts the remains to a hyperlink. The list of trailing delimiters can easily be added to, to include, say, manual line breaks or tabs or whatever you want/use.

It's a bit rough and ready, but is this what you're after?

Selection.HomeKey wdStory

Selection.Find.ClearFormatting
With Selection.Find
.Text = "http://*[ ^13]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

While Selection.Find.Execute
Selection.MoveEnd wdCharacter, -1
If Right(Selection, 1) = "." Then Selection.MoveEnd wdCharacter, -1
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=Selection.Range.Text
Selection.Collapse wdCollapseEnd
Wend

musgrave2
03-09-2005, 05:19 PM
How do I insert this in the document template?

Anne Troy
03-09-2005, 05:29 PM
Copy the code above.
Open the template.
Hit Alt+F11 to access the visual basic editor (VBE).
On the top-left, choose your template's name and then hit Insert-Module.
On the right, type:


Option Explicit
Sub GetHTTP()

Paste the code into the code window that appears at right of your screen.
Then type:

End Sub


Close the VBE.
To test: Tools-Macro-Macros and double-click GetHTTP.

If it works, assign a toolbar button to the macro. To do that, begin with step 2 at:
http://theofficeexperts.com/officevba.htm#WordVBA

:)

TonyJollans
03-09-2005, 05:35 PM
This might be too much detail - I don't know what you know.

In Word, Alt+F11 to open the VBE
In the VBE, Ctrl+r to open the Project Explorer (if not open)
In the Project Explorer locate your Template and right click on it
Select Insert > Module
In the Code Window which opens, Enter Sub MakeHyperlinksFromText and Press Enter
It will add an End Sub line for you
Between the Sub and End SUb, cut and paste the code
Alt+F4 (or whatever) to close the VBE

To run:

In Word, Alt+F8
Find MakeHyperlinksFromText in the list and double click it.

If it does what you want we can move on to setting up buttons.

Anne types faster than me :D - she also distracts me with PMs while she's doing it.

Anne Troy
03-09-2005, 05:40 PM
Anne types faster than me :D - she also distracts me with PMs while she's doing it.

ROFL!! :rofl

Get back to coding. I'll take care of your lightwork. :D

j19_2002
09-16-2008, 03:17 PM
LOL!!