I've been stuck for far too long on this - please can someone help
me ?!

Ok, here's what I'm doing. I have a word template (.dot) with loads of
code in it. It's in startup.

I am opening a new document, inserting a code module, and I want to
call a function in the template (which is loaded)

Apparently this seems impossible?

For example, if I create a routine in my template:

Public sub Check()

..whatever...

End sub

I can run it from my other code module using
application.run("templatename.modulename.Check")

However if I change sub to function - one big error.

I also cannot pass anything in to this routine when it's a sub. For
example:

application.run("templatename.modulename.Check", arg1) will not get to
Public Sub Check (arg1 as var) in the template.

Surely this is possible? Exactly the same thing works fine in Excel
VBA... what am I missing???


Any help would be much appreciated

RE: Calling Functions in Word by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Wed Aug 13 14:37:01 PDT 2008

To: James,

I would like to know the answer to your question also.

I did figure out how to call a function from the Normal template.

I put the following function in Normal:

Function Xyz(ByVal x As Long) As Long
Xyz = x
End Function

Then, from another template, I checked the box "Normal" in the Tools ->
References.

And in this template I created the following sub routine.

Sub Abc()
Dim z As Long
z = Normal.Xyz(5)
MsgBox z
End Sub

And it worked. I was even able to step though the code, even through the
function Xyz line by line.

But this raises the question, what about calling functions in other
templates? Is there a way to add those templates to Tool -> References?

Steven Craig Miller

"james_davey1@yahoo.co.uk" wrote:

> I've been stuck for far too long on this - please can someone help
> me ?!
>
> Ok, here's what I'm doing. I have a word template (.dot) with loads of
> code in it. It's in startup.
>
> I am opening a new document, inserting a code module, and I want to
> call a function in the template (which is loaded)
>
> Apparently this seems impossible?
>
> For example, if I create a routine in my template:
>
> Public sub Check()
>
> ...whatever...
>
> End sub
>
> I can run it from my other code module using
> application.run("templatename.modulename.Check")
>
> However if I change sub to function - one big error.
>
> I also cannot pass anything in to this routine when it's a sub. For
> example:
>
> application.run("templatename.modulename.Check", arg1) will not get to
> Public Sub Check (arg1 as var) in the template.
>
> Surely this is possible? Exactly the same thing works fine in Excel
> VBA... what am I missing???
>
>
> Any help would be much appreciated
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

Re: Calling Functions in Word by Greg

Greg
Wed Aug 13 15:27:37 PDT 2008

Gents,

I really don't know enough about the mechanics of VBA to explain it, but I
think I have been able to achieve what James is trying to do.

I created a .dot template that loads at startup. It contains a module named
Test_Procedures and the following code:

Private Sub Test1()
MsgBox "Call Test1 tested sat sat."
End Sub
Sub Test2(ByRef pStr As String)
MsgBox pStr & " test tested sat."
End Sub
Function Test3(ByRef pStr As String) As String
Test3 = "This test on passing variable to a " & pStr & " tested sat."
End Function

I then open a new document and paste in the following code.

Sub Testing()
On Error GoTo Err_Handler
'This works
Application.Run "Test1"
'While this doesn't
Application.Run MacroName:="TestTemplate.Test_Procedures.Test1"
'This works
Application.Run "Test2", "Passing variable"
'While this doesn't
Application.Run MacroName:="TestTemplate.Test_Procedures.Test2",
varg1:="Passing variable"
'This works
MsgBox Application.Run("Test3", "Function")
'While this doesn't
MsgBox Application.Run(MacroName:="TestTemplate.Test_Procedures.Test3",
varg1:="Function")
Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Next
End Sub

You can see the results for yourself. I don't know why the procedures fail
to run it you call the macro by template, module, and name as the help file
indicates.


james_davey1@yahoo.co.uk wrote:
> I've been stuck for far too long on this - please can someone help
> me ?!
>
> Ok, here's what I'm doing. I have a word template (.dot) with loads of
> code in it. It's in startup.
>
> I am opening a new document, inserting a code module, and I want to
> call a function in the template (which is loaded)
>
> Apparently this seems impossible?
>
> For example, if I create a routine in my template:
>
> Public sub Check()
>
> ..whatever...
>
> End sub
>
> I can run it from my other code module using
> application.run("templatename.modulename.Check")
>
> However if I change sub to function - one big error.
>
> I also cannot pass anything in to this routine when it's a sub. For
> example:
>
> application.run("templatename.modulename.Check", arg1) will not get to
> Public Sub Check (arg1 as var) in the template.
>
> Surely this is possible? Exactly the same thing works fine in Excel
> VBA... what am I missing???
>
>
> Any help would be much appreciated

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org



Re: Calling Functions in Word by Greg

Greg
Wed Aug 13 16:00:11 PDT 2008

Wait a minute gents. I did stumble on something. In your VBA editor click
on Tools>References. Then check the TemplateProject to add a reference to
your tempalte adding.

Now you can call Test1, Test2 and Test3 three like this:

TemplateProject.Test_Procedures.Test2 ("Variable")
TemplateProject.Test_Procedures.Test2 ("Variable")
MsgBox TemplateProject.Test_Procedures.Test3("Function")

I still don't know why the Application.Run with full Project.Module.Name
doesn't work.


james_davey1@yahoo.co.uk wrote:
> I've been stuck for far too long on this - please can someone help
> me ?!
>
> Ok, here's what I'm doing. I have a word template (.dot) with loads of
> code in it. It's in startup.
>
> I am opening a new document, inserting a code module, and I want to
> call a function in the template (which is loaded)
>
> Apparently this seems impossible?
>
> For example, if I create a routine in my template:
>
> Public sub Check()
>
> ..whatever...
>
> End sub
>
> I can run it from my other code module using
> application.run("templatename.modulename.Check")
>
> However if I change sub to function - one big error.
>
> I also cannot pass anything in to this routine when it's a sub. For
> example:
>
> application.run("templatename.modulename.Check", arg1) will not get to
> Public Sub Check (arg1 as var) in the template.
>
> Surely this is possible? Exactly the same thing works fine in Excel
> VBA... what am I missing???
>
>
> Any help would be much appreciated

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org



Re: Calling Functions in Word by Greg

Greg
Wed Aug 13 16:10:50 PDT 2008

Ignore the last.

In your VBA editor click on Tools>References. Then check the
TemplateProject to add a reference to
your tempalte addin.

Now you can call Test1, Test2 and Test3 like this:

TemplateProject.Test_Procedures.Test1
TemplateProject.Test_Procedures.Test2 ("Variable")
MsgBox TemplateProject.Test_Procedures.Test3("Function")

I still don't know why the Application.Run with full Project.Module.Name
doesn't work.


james_davey1@yahoo.co.uk wrote:
> I've been stuck for far too long on this - please can someone help
> me ?!
>
> Ok, here's what I'm doing. I have a word template (.dot) with loads of
> code in it. It's in startup.
>
> I am opening a new document, inserting a code module, and I want to
> call a function in the template (which is loaded)
>
> Apparently this seems impossible?
>
> For example, if I create a routine in my template:
>
> Public sub Check()
>
> ..whatever...
>
> End sub
>
> I can run it from my other code module using
> application.run("templatename.modulename.Check")
>
> However if I change sub to function - one big error.
>
> I also cannot pass anything in to this routine when it's a sub. For
> example:
>
> application.run("templatename.modulename.Check", arg1) will not get to
> Public Sub Check (arg1 as var) in the template.
>
> Surely this is possible? Exactly the same thing works fine in Excel
> VBA... what am I missing???
>
>
> Any help would be much appreciated

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org



Re: Calling Functions in Word by jamesdavey

jamesdavey
Wed Aug 13 23:44:30 PDT 2008

Thanks for that, I was misled somewhat by the help file - and by the
fact that xl would call it differently - but then as I'm finding out
with this project using vba in word & powerpoint is somewhat different
to excel :) Cheers

Re: Calling Functions in Word by Jonathan

Jonathan
Thu Aug 14 04:32:04 PDT 2008


<james_davey1@yahoo.co.uk> wrote in message
news:bd60fbfe-0618-442b-aa69-3b05f038ad83@25g2000hsx.googlegroups.com...
> I've been stuck for far too long on this - please can someone help
> me ?!
>
> Ok, here's what I'm doing. I have a word template (.dot) with loads of
> code in it. It's in startup.
>
> I am opening a new document, inserting a code module,

I would go about the above bit in a different way. Create a template or
document with the appropriate code module already present. When you want to
create a new document, open this document and then do a Save As to a new
location. (Alternatively, copy the file to the new location and then open
it.)


> and I want to
> call a function in the template (which is loaded)
>
> Apparently this seems impossible?
>
> For example, if I create a routine in my template:
>
> Public sub Check()
>
> ..whatever...
>
> End sub
>
> I can run it from my other code module using
> application.run("templatename.modulename.Check")

In Word, Application.Run doesn't want the template name and project name
unless you need to distinguish between two different routines both with the
same module and routine names.
>
> However if I change sub to function - one big error.

You need to understand the calling convention and how parentheses work. This
should work

x = Application.Run("modulename.Check")

>
> I also cannot pass anything in to this routine when it's a sub. For
> example:
>
> application.run("templatename.modulename.Check", arg1) will not get to
> Public Sub Check (arg1 as var) in the template.

When using Application.Run to call a sub, don't use parentheses

Application.Run "modulename.Check", arg1

And also, if you want to pass a parameter and get a return value, use
something like this

x = Application.Run("modulename.Check", arg1)


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup



Re: Calling Functions in Word by Greg

Greg
Thu Aug 14 15:44:17 PDT 2008

Jonathan,

I also notice that I can call a procedure in s template AddIn using:

Sub Testing()
Application.Run "'TestTemplate.dot'!TestMod.Test1"
End Sub

Do you know what the "!" in the code signifies and where it is explained in
the help file?

Thanks.


Jonathan West wrote:
> <james_davey1@yahoo.co.uk> wrote in message
> news:bd60fbfe-0618-442b-aa69-3b05f038ad83@25g2000hsx.googlegroups.com...
>> I've been stuck for far too long on this - please can someone help
>> me ?!
>>
>> Ok, here's what I'm doing. I have a word template (.dot) with loads
>> of code in it. It's in startup.
>>
>> I am opening a new document, inserting a code module,
>
> I would go about the above bit in a different way. Create a template
> or document with the appropriate code module already present. When
> you want to create a new document, open this document and then do a
> Save As to a new location. (Alternatively, copy the file to the new
> location and then open it.)
>
>
>> and I want to
>> call a function in the template (which is loaded)
>>
>> Apparently this seems impossible?
>>
>> For example, if I create a routine in my template:
>>
>> Public sub Check()
>>
>> ..whatever...
>>
>> End sub
>>
>> I can run it from my other code module using
>> application.run("templatename.modulename.Check")
>
> In Word, Application.Run doesn't want the template name and project
> name unless you need to distinguish between two different routines
> both with the same module and routine names.
>>
>> However if I change sub to function - one big error.
>
> You need to understand the calling convention and how parentheses
> work. This should work
>
> x = Application.Run("modulename.Check")
>
>>
>> I also cannot pass anything in to this routine when it's a sub. For
>> example:
>>
>> application.run("templatename.modulename.Check", arg1) will not get
>> to Public Sub Check (arg1 as var) in the template.
>
> When using Application.Run to call a sub, don't use parentheses
>
> Application.Run "modulename.Check", arg1
>
> And also, if you want to pass a parameter and get a return value, use
> something like this
>
> x = Application.Run("modulename.Check", arg1)

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org