Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Launch Code from Double Click in Word 1

Status
Not open for further replies.

GummowN

Programmer
Jul 26, 2002
157
GB
I am writing a document that links back to a lot of data in a database - instead of having it all displayed (cumbersomely) in the document I want the user to launch the data from the word document.

The code to get the data and display it (in excel) is done - all I want is someway of launching the code.

Preferably something like a hyperlink - or even better highlight the text - and then on the right click menu - Get data

I am good at VBA - but I have never had to do anything in Word until today!!

If at first you don't succeed, try for the answer.
 
Perhaps a ActiveX commandbutton in your Word document? Or do you definitely want a right menu click? If you used a commandbutton in the Word document, you could have it Captioned "Get Data", click on it, and badda-bing it can fire your code. Something like:
Code:
Sub GetData_Click()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.application")
xlApp.Workbooks.Open "c:\test.xls"
xlApp.Application.Run "'test.xls'!ThisWorkbook.TryMe"
xlApp.Quit
Set xlApp = Nothing
End Sub

TryMe is the procedure (in Excel) to run. It can be in any module.

Gerry
See my Paintings and Sculpture
 
Hi fumei,

As an alternative to a command button, a MACROBUTTON field in the document could be used. To creat one for this macro, Press Ctrl-F9 to creat a pair of field braces:
{ }
where you want the prompt to appear and type:
MACROBUTTON GetData_Click double-click here
between them, so that you get:
{MACROBUTTON GetData_Click double-click here}
Then press F9 again to update the field. Now, double-clicking it will fire the macro.

Cheers
 
Thanks Macropod exactly what I wanted - you get a star for that!!!

And you will get another if you know how to do the same but instead of running GetData_Click you can run GetData_Click(strSomething)


If at first you don't succeed, try for the answer.
 
Hi GummowN ,

I don't you can do that with a MACROBUTTON field, but you could solicit the 'strSomething' parameter via an InputBox in the macro.

Cheers
 
Not to worry.

I will just have GetData1, GetData2, etc each one doing the main function - which incidentally connects to an Access DB gets a recordset then dumps it nicely formatted into excel.

Thanks anyway

If at first you don't succeed, try for the answer.
 
Or, you could simply call another Sub from a commandbutton.

Code:
Private Sub CommandButton1_Click()
Call OtherParameters
End Sub

Sub OtherParameters(strIn As String)  ' or whatever argument
'  ...do whatever...
End Sub

Generally, I prefer commandbuttons because they easily accessible events, and you can easily edit captions. For example, the MouseMove event on a commandbutton can display a message box explaining what the button will do. The user puts the mouse over the button and, hey presto they have a sort of help display for that button.

Also, they can be programmatically resized, AND they take a single click rather than a double click. {MACROBUTTON} works fine, but since I can use ActiveX controls...why not? They have better, more accessible events.

Gerry
See my Paintings and Sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top