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 / September 2006

Tip: Looking for answers? Try searching our database.

Function running when Clicking on hyperlink in a Word document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oetss - 13 Sep 2006 11:25 GMT
Hello everyone,

I'm new to this group and to the whole google discussions groups.
I have a question on hyperlinks in a word document

Is there a way of running a function or a class when I click on an
hyperlink in a word document?

I have a word document with hyperlinks to other word documents.
I want to open the hyperlink as read only.

thanks in advance

Michiel
(sorry for my englisch, i'm a dutchie)
Shauna Kelly - 13 Sep 2006 13:52 GMT
Hi Oetss

As far as I know, you can't run code from a Hyperlink field, and you can't
use the Hyperlink field to open a document read-only.

But you can use a Macrobutton field. Do ctrl-F9, and then within the braces
that Word gives you, type Macrobutton YourMacroName Click here to open the
XYZ document.

Create your code to open the document read-only. YourMacroName must be a Sub
with no parameters. There's more info in Word's help under Macrobutton.

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> Hello everyone,
>
[quoted text clipped - 11 lines]
> Michiel
> (sorry for my englisch, i'm a dutchie)
Cindy M. - 13 Sep 2006 13:54 GMT
Hi Oetss,

> Is there a way of running a function or a class when I click on an
> hyperlink in a word document?
>  
> I have a word document with hyperlinks to other word documents.
> I want to open the hyperlink as read only.

There's nothing to run any code when a hyperlink is clicked...

You could embed the hyperlink in a MacroButton field, then use the
code the field executes to do whatever. But given your requirement,
I wouldn't use a real hyperlink as the Macrobutton prompt, just text
formatted to look like a hyperlink. You'd need to use the
Documents.Open method in the macro code to control HOW a document is
opened (a hyperlink can ONLY open a document, it can't affect how
the document is opened).

Here's an code sample for the basic approach. This picks up the file
path from the macrobutton field, so you could use the same macro for
multiple files.

Sub OpenFileReadOnly()
   Dim filePath As String
   Dim startPos As Long
   
   'Cut off the spaces at beginning and end of field code
   filePath = Trim(Selection.Fields(1).Code)
   'Cut off the field name and macro name
   startPos = InStrRev(filePath, " ", -1)
   filePath = Mid(filePath, startPos)
   filePath = Trim(filePath)
   Documents.Open FileName:=filePath, ReadOnly:=True
End Sub

Insert/Field to create the MacroButton field. Once it's inserted you
use Alt+F9 to turn the field codes on and off. You can format the
file path in the field code so that it displays like a hyperlink.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :-)
Oetss - 13 Sep 2006 14:55 GMT
Dear Shauna and Cindy,

This is realy a good option for me.
Thanks to both of you!

Bye
Michiel
Oetss - 13 Sep 2006 15:36 GMT
Still one question.

Is it also possible to show a hand when moving over the macrobutton
field?

Thanks in advance,
Michiel.
Cindy M. - 14 Sep 2006 15:49 GMT
Hi Oetss,

> Is it also possible to show a hand when moving over the macrobutton
> field?

No, unfortunately it is not. Sorry.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :-)
 
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.