Function running when Clicking on hyperlink in a Word document

O

Oetss

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)
 
S

Shauna Kelly

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
 
C

Cindy M.

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 :)
 
O

Oetss

Dear Shauna and Cindy,

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

Bye
Michiel
 
O

Oetss

Still one question.

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

Thanks in advance,
Michiel.
 

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