Calling Functions In Global AddIns

G

Greg Maxey

I created a global Word Addin (template file "TestDoc.dotm" stored in the
Word startup directory) containing a standard project module "TestModule"
and the following code:

Option Explicit
Sub RunTest1()
MsgBox "Test 1 Sat"
End Sub

Function RunTestA(ByRef pStr As String) As String
RunTestA = "Test A Sat. Variable " & pStr & " passed sucessfully."
End Function

I then started Word and in a new document ran the following test of methods
for call a sub and function from a global template:

Sub Testing()
On Error GoTo Err_Handler

'These all work to call the routine.
Application.Run "RunTest1"
Application.Run "TestModule.RunTest1"
Application.Run "TestDoc.dotm!TestModule.RunTest1"
Application.Run MacroName:="RunTest1"
Application.Run MacroName:="TestModule.RunTest1"
Application.Run MacroName:="TestDoc.dotm!TestModule.RunTest1"

'These all work to call the function.
MsgBox Application.Run("RunTestA", "Test")
MsgBox Application.Run("TestModule.RunTestA", "Test")
MsgBox Application.Run(MacroName:="RunTestA", varg1:="Test")
MsgBox Application.Run(MacroName:="TestModule.RunTestA", varg1:="Test")

'While these don't. Why?
MsgBox Application.Run("TestDoc.dotm!TestModule.RunTestA", "Test")
MsgBox Application.Run(MacroName:="TestDoc.dotm!TestModule.RunTestA",
varg1:="Test")

Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Next
End Sub

Can anyone explain why the last two calls to the function won't work?
Can anyone explain the use of the exclamation point "!" in the Macro name
string? What is this symbol, where is it defined or explained. What other
uses does it have?

Thanks.


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Gordon Bentley-Mix

Greg,

As far as I can tell, apart from being the "type-declaration character" for
Single type variables, the exclamation point doesn't have any use in Word
VBA. All other references I've found talk about Access VBA or Visual Basic,
where it is used to change levels in the database hierarchy or activate a
sub-object respectively.

Of course this doesn't explain why it causes problems when trying to call a
function from within a message box... Maybe the answer is like the old
vaudeville routine:

Patient: "Doc, it hurts when I do this!"
Doctor: "Then don't do it!"
--
Cheers!
Gordon

Vote National!!

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Greg Maxey

Gordon:

< "Then don't do it!"

That's a bitter pill ;-)

I'm not sure the "!" is really causing the problem. It certainly doesn't
cause a problem calling (Sub) procedures explicitly by
Template.Module.Procedure:

Application.Run MacroName:="TestDoc.dotm!TestModule.RunTest1"
'***********this works
Application.Run MacroName:="TestDoc.dotm.TestModule.RunTest1"
'***********where this doesn't


I guess the underlying question is:

As you can't call a (Function) procedure explicitly by
Template.Module.Procedure usind the method that works for a (Sub) procedure,
how do you do it?

Greg,

As far as I can tell, apart from being the "type-declaration
character" for Single type variables, the exclamation point doesn't
have any use in Word VBA. All other references I've found talk about
Access VBA or Visual Basic, where it is used to change levels in the
database hierarchy or activate a sub-object respectively.

Of course this doesn't explain why it causes problems when trying to
call a function from within a message box... Maybe the answer is like
the old vaudeville routine:

Patient: "Doc, it hurts when I do this!"
Doctor: "Then don't do it!"

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Gordon Bentley-Mix

GAAAAKKK!!!

I see the problem now. What do you do if there's a risk of two or more
templates that contain the same function in the same module? How do you
specify which function to use if you can't use a bang to identify the
specific template?

Hmm... I'm not really sure. I guess you'd have to implement some sort of
kludgy workaround where you use a procedure that accepts arguments and pass
your value to it (using a dummy variable to ensure that your original value
doesn't get modified). Of course this ruins the advantages of using a
function in the first place...

Access and VB appear to use some jiggery-pokery with square brackets [] to
identify a specific table or object, but I don't know if this is available in
Word VBA. Guess you could always try.

A bitter pill indeed! But at least I learned how to call procedure in a
specific template using a bang...
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
M

Manfred F

Hi Gordon,

:

...
I see the problem now. What do you do if there's a risk of two or more
templates that contain the same function in the same module? ..
..
Access and VB appear to use some jiggery-pokery with square brackets [] to
identify a specific table or object, but I don't know if this is available in
Word VBA. Guess you could always try.

In W2003, I use the square brackets e.g. to distinguish between public
variables from different global add-ins having the same name
([Projectname].VariableName).
To uniquely identify a procedure or a function, the combination
<modulename.procedurename> has to be unique, the projectname is not
evaluated. As far as I know, this is a known bug, also described somewhere in
the knowledgebase.

Kind Regards,
Manfred
 
G

Greg Maxey

Sub Testing2()
On Error Resume Next
'Manfred, so the tempalte name is ignored here ...
Application.Run MacroName:="TestDoc.dotm!TestModule.RunTest1"
'but evaluated and throws an error here!!
MsgBox Application.Run("TestDoc.dotm!TestModule.RunTestA", "Test")
'Gordon, by adding a reference to the project you can call the function
like:
MsgBox ProjectA.TestModule.RunTestA("Test")
On Error GoTo 0
End Sub

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
Manfred F said:
Hi Gordon,

:

..
I see the problem now. What do you do if there's a risk of two or more
templates that contain the same function in the same module? ..
..
Access and VB appear to use some jiggery-pokery with square brackets []
to
identify a specific table or object, but I don't know if this is
available in
Word VBA. Guess you could always try.

In W2003, I use the square brackets e.g. to distinguish between public
variables from different global add-ins having the same name
([Projectname].VariableName).
To uniquely identify a procedure or a function, the combination
<modulename.procedurename> has to be unique, the projectname is not
evaluated. As far as I know, this is a known bug, also described somewhere
in
the knowledgebase.

Kind Regards,
Manfred
 

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