PDA

View Full Version : Solved: connecting hyperlink to a commandbutton



GrantLund
07-12-2006, 11:31 PM
:hi: Hi everyone again!

Firstly, thanx for the help on my previous challenge. This VBA is very new to me but is very exciting stuff.

I now have a new concept I want to try but not quite sure how to do it. When someone clicks a commandbutton I want it to open up the webpage linked to it. I came across the followhyperlink command (which also shows how to open up in a new window) but dunno how to make it connect to the commandbutton I want:think: . I am sure this is a simple thing though. I hope I have explained the concept clearly enough.

Rgds,
Grant
grant DOTlundAtsasolDOTcom (grant.lund@sasol.com)

mdmackillop
07-12-2006, 11:49 PM
Hi Grant,
In Design Mode (as when you added the button), double click to open the VBA editor and you should see the Click routine. Add your code there.

Private Sub CommandButton1_Click()
ActiveDocument.FollowHyperlink "C:\AAA\Document1.doc"
End Sub


Or write it direct into ThisDocument module.

GrantLund
07-12-2006, 11:55 PM
Wow you are fast. Ok that is pretty simple - I think I was confusing myself with the activedocument expression thinking that it would look at the document and not the commandbutton, but as you have pointed out, if put in the commandbutton subroutine then it links to commandbutton.

I also did the following which appears to work (named commandbutton CMD2) :
Private Sub CMD2_Click()
Dim hypTemp As Hyperlink

Set hypTemp = ActiveDocument.Hyperlinks.Add( _
Address:="http://xxx.xx.xx", _
Anchor:=Selection.Range)
hypTemp.Follow NewWindow:=True, AddHistory:=False

End Sub

Thank you!
G:beerchug:

mdmackillop
07-13-2006, 12:07 AM
Hi Grant,
Your code is creating a hyperlink and trying to add it to the document. This is unneccessary and may cause problems if it adds a new link each time. To be honest, I don't know where they are being added. Just use


Private Sub CMD2_Click()
ActiveDocument.FollowHyperlink "http://www.vbaexpress.com/forum/showthread.php?p=69022#post69022"
End Sub

mdmackillop
07-13-2006, 12:20 AM
Hi Grant,
Don't post your email address in plain form otherwise it gets caught by the Spam monsters. (although I suspect that they can sort out AT and DOT as well)
Regards
MD