PDA

View Full Version : Macrobutton with arguments in Word



micky811
10-18-2010, 02:27 AM
Hi all,

Is there a way to assign some arguments to a macro using a MACROBUTTON in word?

I want to use following code:



Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal lngHwnd As Long, _
ByVal strOperation As String, _
ByVal strFile As String, _
ByVal strParameters As String, _
ByVal strDirectory As String, _
ByVal lngShow As Long) As Long


Public Sub OpenFile(ByVal strPathAndFile As String)

ShellExecute 0, "Open", strPathAndFile, "", "", 1

End Sub


I hope there is a way to give an argument (location of jpg-file) to function 'OpenFile' that can be opened using a Macrobutton in Word.

Thanks,
Lies

fumei
10-18-2010, 08:58 AM
No.

However, you could have the MACROBUTTON get a parameter, and then pass it on to another procedure.

Sub GetSomethingAndPassItOn() ' the macrobutton procedure
Dim strPathAndFile As String
strPathAndFile = InputBox("Enter path and filename.")
Call OpenFile(strPathAndFile)
End Sub

micky811
10-19-2010, 01:31 AM
Thanks for the reply.
It is indeed a solution but I'm looking for an automated solution to create a hyperlink to figures (not with 'Hyperlink' because it opens the jpg-figures in Internet Explorer).
If I click on the Macrobutton I don't want to insert the path and filename.
When I click on the Macrobutton, the figure has to open in the default viewer (that's why I use ShellExecute).
And I don't want to create for all figures a separate macro.

fumei
10-19-2010, 09:13 AM
I see.

You want to create a hyperlink...but not with a hyperlink.


"If I click on the Macrobutton I don't want to insert the path and filename. "

You want to to be able to click the macrobutton and use a path and filename...without entering a path or filename. Interesting.

How were you thinking of getting the path/filename to the Macrobutton, assuming you could pass a parameter, which you can not?

Let me see if I understand correctly, because you are not being clear.

You have a Macrobutton. A macrobutton is a pointer to ONE procedure. That macrobutton - somehow - has some sort of pointer to a figure (an InlineShape? a Shape?), and uses that pointer (a path/filename) as a parameter in its instruction (the ShellExecute).

Yes?


Sorry....no.


"And I don't want to create for all figures a separate macro." Ok, fine. So you tell me...how are you going to GET the path/filename of each figure?

Also, please expand on these figures. Embedded?

gmaxey
10-19-2010, 01:59 PM
Micky,

As Gerry says, no. Macrobuttons fields can't pass argurments to a procedure. However, if you are trying to use several macrobuttons to call the same procedure and you want that procedure to know which macrobutton called it then you could bound your macrobutton fields with a bookmark i.e, Loc1, Loc2, etc. and then use something like this:

Public Sub OpenFile()
Dim strPathAndFile As String
Select Case True
Case Selection.InRange(ActiveDocument.Bookmarks("Loc1").Range)
strPathAndFile = "D:\My Pictures\Picture 1"
Case Selection.InRange(ActiveDocument.Bookmarks("Loc2").Range)
strPathAndFile = "D:\My Pictures\Picture 2"
End Select
ShellExecute 0, "Open", strPathAndFile, "", "", 1
End Sub

micky811
10-20-2010, 03:38 AM
I'm very happy to announce that this is working :-).
Thank you very much !!

It is more work than using just a hyperlink to open jpg-picture but if this is the only way to open a jpg-picture in the standard viewer than it is worth the effort.

Is there a way to refer only to a subdirectory instead of the full directory. Because the document (together with the figures in a subdirectory) is going to be used by different people on different computers.

Thanks a lot
Lies

gmaxey
10-20-2010, 12:47 PM
Micky,

Just create more string variables and then put them together in the Case statement

Dim pPath as String
Dim pFileName as String
Dim pStringToPass as Sting

pPath = "Whatever"
pFileName = "Whatever else"
pStringToPass = pPath + pFileName



I'm very happy to announce that this is working :-).
Thank you very much !!

It is more work than using just a hyperlink to open jpg-picture but if this is the only way to open a jpg-picture in the standard viewer than it is worth the effort.

Is there a way to refer only to a subdirectory instead of the full directory. Because the document (together with the figures in a subdirectory) is going to be used by different people on different computers.

Thanks a lot
Lies

micky811
10-21-2010, 03:41 AM
Ok thank you.
I managed changing the script so that you don't have to update the procedure every time you add a new picture.
Just give the bookmark the same name as the picture and the macrobutton opens the jpg-picture in the standard viewer.

I'm amazed what you can do with VBA.

Now a more fundamental question.

At the moment I have to import the module (which contains the procedure 'OpenFile') everytime I open the word-document.
As this document has to be used by others, I really don't want to explain each time what they have to do before the links to the pictures work.

Is there a way the module is imported automatically when the document is opened?
Or fomulated differently: is there a way to link a module to a word-document which is imported automatically when that particular document is opened?

Lies

fumei
10-21-2010, 08:49 AM
Yes. EXPORT the module to a .bas file, then have it IMPORTED in the Document_Open event.

OR, have your procedure in a global template (probably the better way). If th eglobal is loaded automatically (by being in Startup) then the procedure can be accessed immediately. Or, you can load the global dynamically.

gmaxey
10-21-2010, 01:58 PM
Sub Docuement_Open()
Dim vbProj As VBProject
Set vbProj = ThisDocument.VBProject
vbProj.VBComponents.Import "C:\Code.bas" 'use your save .bas file
End Sub







Ok thank you.
I managed changing the script so that you don't have to update the procedure every time you add a new picture.
Just give the bookmark the same name as the picture and the macrobutton opens the jpg-picture in the standard viewer.

I'm amazed what you can do with VBA.

Now a more fundamental question.

At the moment I have to import the module (which contains the procedure 'OpenFile') everytime I open the word-document.
As this document has to be used by others, I really don't want to explain each time what they have to do before the links to the pictures work.

Is there a way the module is imported automatically when the document is opened?
Or fomulated differently: is there a way to link a module to a word-document which is imported automatically when that particular document is opened?

Lies