Word Macro and hyperlinks

M

Matz

Steps to recreate the problem/bug:
1. Open a document such as the one:
http://www.3gpp.org/ftp/tsg_ran/WG3_Iu/Draft_Specs/TSG-RAN12/25402-360.doc -
all the specification documents from www.3gpp.org that I've tried seem to
give the same problem.

2. Start recording a macro, with Tools|Macro|Record New Macro|Ok.

3. Insert a hyperlink to a heading, with Insert|Hyperlink|Bookmark...
Click, for example, "2 References" and Ok|Ok.

4. Click the stop button to stop macro recording.

5. Check that the hyperlink you just created works by clicking it - you
should jump to chapter 2 References.

6. Close the document, but don't save it.

7. Reopen the document.

8. Run the macro you created earlier, with Tools|Macro then select it and
then Run.

9. A hyperlink should be inserted. Click on the hyperlink to see that it
works. For me at least, it doesn't, the cursor jumps to the start of the
file instead.

10. Now create a new hyperlink to "2 References", positioning it somewhere
else in the document.

11. Check that that hyperlink works. It should.

12. Now go to the hyperlink created by the macro and click on it. Now it
should work, thanks to the manually created hyperlink having been made.

The problem - clearly the macro command created,
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2005-04-21 by qpkorr
'
ChangeFileOpenDirectory _
"C:\Documents and Settings\qpkorr\Local Settings\Temporary Internet
Files\OLK7A7\"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="_2_References", ScreenTip:="", TextToDisplay:="2
References"
End Sub

has not fully recorded everything that the Insert|Hyperlink command actually
did. This means I can't make a macro to create hyperlinks, which I wish to
do.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TWF0eg==?=,
Steps to recreate the problem/bug:
1. Open a document such as the one:
http://www.3gpp.org/ftp/tsg_ran/WG3_Iu/Draft_Specs/TSG-RAN12/25402-360.doc -
all the specification documents from www.3gpp.org that I've tried seem to
give the same problem.

2. Start recording a macro, with Tools|Macro|Record New Macro|Ok.

3. Insert a hyperlink to a heading, with Insert|Hyperlink|Bookmark...
Click, for example, "2 References" and Ok|Ok.

4. Click the stop button to stop macro recording.

5. Check that the hyperlink you just created works by clicking it - you
should jump to chapter 2 References.

6. Close the document, but don't save it.

7. Reopen the document.

8. Run the macro you created earlier, with Tools|Macro then select it and
then Run.

9. A hyperlink should be inserted. Click on the hyperlink to see that it
works. For me at least, it doesn't, the cursor jumps to the start of the
file instead.

10. Now create a new hyperlink to "2 References", positioning it somewhere
else in the document.

11. Check that that hyperlink works. It should.

12. Now go to the hyperlink created by the macro and click on it. Now it
should work, thanks to the manually created hyperlink having been made.

The problem - clearly the macro command created,
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2005-04-21 by qpkorr
'
ChangeFileOpenDirectory _
"C:\Documents and Settings\qpkorr\Local Settings\Temporary Internet
Files\OLK7A7\"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="_2_References", ScreenTip:="", TextToDisplay:="2
References"
End Sub

has not fully recorded everything that the Insert|Hyperlink command actually
did. This means I can't make a macro to create hyperlinks, which I wish to
do.
In your document, press Alt+F9 to display the field codes. Compare the two
Hyperlink fields: the one generated by the recorded macro, the other inserted
through the UI. Copy and paste the content between the {} braces into your
reply so that we can see the difference.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
M

Matz

Steps to recreate the problem/bug:
1. Open a document such as the one:
http://www.3gpp.org/ftp/tsg_ran/WG3_Iu/Draft_Specs/TSG-RAN12/25402-360.doc -
all the specification documents from www.3gpp.org that I've tried seem to
give the same problem.

2. Start recording a macro, with Tools|Macro|Record New Macro|Ok.

3. Insert a hyperlink to a heading, with Insert|Hyperlink|Bookmark...
Click, for example, "2 References" and Ok|Ok.

4. Click the stop button to stop macro recording.

5. Check that the hyperlink you just created works by clicking it - you
should jump to chapter 2 References.

6. Close the document, but don't save it.

7. Reopen the document.

8. Run the macro you created earlier, with Tools|Macro then select it and
then Run.

9. A hyperlink should be inserted. Click on the hyperlink to see that it
works. For me at least, it doesn't, the cursor jumps to the start of the
file instead.

10. Now create a new hyperlink to "2 References", positioning it somewhere
else in the document.

11. Check that that hyperlink works. It should.

12. Now go to the hyperlink created by the macro and click on it. Now it
should work, thanks to the manually created hyperlink having been made.

The problem - clearly the macro command created,
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2005-04-21 by qpkorr
'
ChangeFileOpenDirectory _
"C:\Documents and Settings\qpkorr\Local Settings\Temporary Internet
Files\OLK7A7\"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="_2_References", ScreenTip:="", TextToDisplay:="2
References"
End Sub

has not fully recorded everything that the Insert|Hyperlink command actually
did. This means I can't make a macro to create hyperlinks, which I wish to
do.
In your document, press Alt+F9 to display the field codes. Compare the two
Hyperlink fields: the one generated by the recorded macro, the other inserted
through the UI. Copy and paste the content between the {} braces into your
reply so that we can see the difference.

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


:
Okay, I followed the instructions I gave in the mail, and when I re-opened
the document (step 7) and ran the macro (step 8), I got the following code
between the { and }:
HYPERLINK \l "_2_References"

Then I created a link manually (step 10), which got exactly the same field
code text (at least as far as I can see, but I copied it separately in case
there are hidden characters):
HYPERLINK \l "_2_References"

The point is - after I've create a hyperlink manually (step 10), the
hyperlink I had created with the macro (step 8) then works. So I don't think
it's the content of the field code that's the problem. As best I can tell,
when you insert a hyperlink manually, via the Insert menu, it does something
else, something more than is done by the command saved in the macro:
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="_2_References", ScreenTip:="", TextToDisplay:="2

Somehow the menu command also, I don't know, prepares "_2_References" as a
valid anchor or something? If so, what command can I run to achieve the same
thing?

Can't you reproduce it? I have done so in Word 2000 at work, on Word 2000
on my home PC, and just now, on a Swedish version of Word 2003 on a friend's
laptop. It should not be hard to reproduce...
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TWF0eg==?=,

"_2_References" is probably a bookmark. But if I follow your description the
bookmark already exists in the document when you create the hyperlink? Or are
you linking to something else, such as a heading?

FWIW, when I record a macro to hyperlink to an existing bookmark, the recorded
macro works when I run it again. Absolutely no problem.

But the bookmark name you show seems suspect, to me. A manually created bookmark
cannot begin with an underscore (_). So this has to be one created by Word or
using VBA code. What happens if you try to link to a valid UI bookmark name (has
to start with a letter of the alphabet)?
Okay, I followed the instructions I gave in the mail, and when I re-opened
the document (step 7) and ran the macro (step 8), I got the following code
between the { and }:
HYPERLINK \l "_2_References"

Then I created a link manually (step 10), which got exactly the same field
code text (at least as far as I can see, but I copied it separately in case
there are hidden characters):
HYPERLINK \l "_2_References"

The point is - after I've create a hyperlink manually (step 10), the
hyperlink I had created with the macro (step 8) then works. So I don't think
it's the content of the field code that's the problem. As best I can tell,
when you insert a hyperlink manually, via the Insert menu, it does something
else, something more than is done by the command saved in the macro:
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="_2_References", ScreenTip:="", TextToDisplay:="2

Somehow the menu command also, I don't know, prepares "_2_References" as a
valid anchor or something? If so, what command can I run to achieve the same
thing?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
M

Matz

More replies:
"_2_References" is probably a bookmark.
Correct - as stated in step 3 of my original instructions (copied at the end
of the mail)
But if I follow your description the
bookmark already exists in the document when you create the hyperlink? Or are
you linking to something else, such as a heading?
I'm not sure how to answer this. I just do what I wrote below, clicking on
the menus/choices:
Insert|Hyperlink|Bookmark...
and then select "2 References".
Yes, this is an auto generated bookmark to a heading I think. I don't care
what it is - as far as I'm concerned, I'm adding a hyperlink to an object -
and it works - the first time... but not if I close the document and reopen
it and try to create the same hyperlink with the macro I saved when I created
it the first time.
FWIW, when I record a macro to hyperlink to an existing bookmark, the recorded
macro works when I run it again. Absolutely no problem.
Yes and no for me. The macro runs, and inserts the hyperlink, but if I
close the document (no save) and reopen it and then run the macro, it still
works, but the hyperlink doesn't work. Clicking on it jumps to the top of
the page.
But the bookmark name you show seems suspect, to me. A manually created bookmark
cannot begin with an underscore (_). So this has to be one created by Word or
using VBA code.
Correct. The deal is that I have a hundred files like this (zipped) one:
http://www.3gpp.org/ftp/Specs/latest/R1999/25_series/25435-3b0.zip
which already have lots of numbered headings, eg
2
2.1
2.1.1
....
2.4.2.1.5.3

Also inside the document are references to those headings, which are just
text references - ie see 2.4.2.1.5.3. I wanted to write a macro to convert
all these text references into hyperlinks, so you could just click on the
reference and be taken to that location. So yes, all the heading bookmarks
already exist. But I can't find a macro command that creates a working
hyperlink.

What happens if you try to link to a valid UI bookmark name (has
to start with a letter of the alphabet)?
That works correctly. The problem only occurs when I'm adding hyperlinks to
"Heading Bookmarks" (by which I mean - Insert Menu, click Hyperlink, and
click Bookmark... - you get a tree of Headings and a tree of Bookmarks - my
step 3 talked about linking to "2 References", one of the Headings.

And this problem seems to exist in every version of Word I can find - if you
follow my instructions to the letter - use the word file in the zip file link
above - does the link created by the macro work for you in (step 9)?

Thanks,
John
Cindy Meister

Steps to recreate the bug:
1. Open a document such as the one attached - all the specification
documents from www.3gpp.org that I've tried seem to give the same problem.

2. Start recording a macro, with Tools|Macro|Record New Macro|Ok.

3. Insert a hyperlink to a heading, with Insert|Hyperlink|Bookmark...
Click, for example, "2 References" and Ok|Ok.

4. Click the stop button to stop macro recording.

5. Check that the hyperlink you just created works by clicking it - you
should jump to chapter 2 References.

6. Close the document, but don't save it.

7. Reopen the document.

8. Run the macro you created earlier, with Tools|Macro then select it and
then Run.

9. A hyperlink should be inserted. Click on the hyperlink to see that it
works. For me at least, it doesn't, the cursor jumps to the start of the
file instead.

10. Now create a new hyperlink to "2 References", positioning it somewhere
else in the document.

11. Check that that hyperlink works. It should.

12. Now go to the hyperlink created by the macro and click on it. Now it
should work, thanks to the manually created hyperlink having been made.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TWF0eg==?=,
Yes and no for me. The macro runs, and inserts the hyperlink, but if I
close the document (no save) and reopen it and then run the macro, it still
works, but the hyperlink doesn't work. Clicking on it jumps to the top of
the page.
Have you checked at this point (re-opening) whether that bookmark still exists
in the document? That would be the most likely explanation for the behavior
you're seeing.
Also inside the document are references to those headings, which are just
text references - ie see 2.4.2.1.5.3. I wanted to write a macro to convert
all these text references into hyperlinks, so you could just click on the
reference and be taken to that location. So yes, all the heading bookmarks
already exist. But I can't find a macro command that creates a working
hyperlink.
Well, if this isn't an "ancient" version of Word, you should simply be able to
add the \h switch to the Ref field that's maintaining the cross-reference. I'd
think that would be the better course?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 

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