Is there some way i can check if a macro is present.

We are on the way to deploying a global template, which contains some macro
code. Next step is then to integrate some code in our templates that calls
code in this global template.

But.... If someone for some reason hasn't received this global template,
Word will fail, complaining about the missing resource since it doesnt have
access to the function it calls.

Are there any way I (in the code) can test if the global template is
present, and then execute if present, and just cancel if it is not present.

My goal is to letting people be aware of the missing global template, but
not get any errors that opens the debug editor and so forth...

/Anders

Re: Check if macro is present? by Jonathan

Jonathan
Thu Jan 19 04:13:26 CST 2006

Hi pnp,

You can iterate through the Addins collection and see if the template you
need is listed in the addins, and if its Loaded property is True.

- If it is present and loaded, no action is needed
- It is present but not loaded, you can load it.
- If it is not present you can see if the file exists, and add it to the
Addins collection and load it.
- If the file doesn't exist, you issue a warning.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org



"pnp" <pnp@discussions.microsoft.com> wrote in message
news:055D5D49-1592-45D4-B1A8-96EDE8CF7ACA@microsoft.com...
> Is there some way i can check if a macro is present.
>
> We are on the way to deploying a global template, which contains some
> macro
> code. Next step is then to integrate some code in our templates that calls
> code in this global template.
>
> But.... If someone for some reason hasn't received this global template,
> Word will fail, complaining about the missing resource since it doesnt
> have
> access to the function it calls.
>
> Are there any way I (in the code) can test if the global template is
> present, and then execute if present, and just cancel if it is not
> present.
>
> My goal is to letting people be aware of the missing global template, but
> not get any errors that opens the debug editor and so forth...
>
> /Anders


Re: Check if macro is present? by pnp

pnp
Thu Jan 19 06:02:02 CST 2006

I found a solution somewhat similar. Inspired by KB 308340 i came up with
the following code:
The setup is:
MacroProject.Macro is a function found in a global template (maybe found)
The following code is placed in the template to a document, and runs when
the document is opened:

Private Sub Document_Open()
'Check if any references is broken
checkReference
End Sub

Sub checkReference()
Dim vbProj As VBProject 'This refers to the vbproject
Dim chkRef As Reference 'A reference

'Refer to the vbproject where the code is placed
Set chkRef = Me.VBProject

'Check through each reference to see if they are missing
For Each chkRef In vbProj.References
If chkRef.IsBroken Then
'Errorhandling - In my case a msgbox stating that the global
template is missing, and how to install it.
Exit Sub
End If
Next
'If no references where broken - call the method that calls the method
in the global template.
Call UpdateFields
Exit Sub

End Sub

Sub UpdateFields()
MacroProject.Macro ' Calls the code in the global template.
End Sub


The code takes advantage of word first compiling the method when calling it.
That is, if the sub UpdateFields is called, and the reference to MacroProject
is missing, a compile error will raise. But if the reference is missing and
the sub is never called no errors occur.

I hope the example is understandable - if anyone sees any problems with it
let me know. :-)

/Anders



"Jonathan West" wrote:

> Hi pnp,
>
> You can iterate through the Addins collection and see if the template you
> need is listed in the addins, and if its Loaded property is True.
>
> - If it is present and loaded, no action is needed
> - It is present but not loaded, you can load it.
> - If it is not present you can see if the file exists, and add it to the
> Addins collection and load it.
> - If the file doesn't exist, you issue a warning.
>
> --
> Regards
> Jonathan West - Word MVP
> www.intelligentdocuments.co.uk
> Please reply to the newsgroup
> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>
>
>
> "pnp" <pnp@discussions.microsoft.com> wrote in message
> news:055D5D49-1592-45D4-B1A8-96EDE8CF7ACA@microsoft.com...
> > Is there some way i can check if a macro is present.
> >
> > We are on the way to deploying a global template, which contains some
> > macro
> > code. Next step is then to integrate some code in our templates that calls
> > code in this global template.
> >
> > But.... If someone for some reason hasn't received this global template,
> > Word will fail, complaining about the missing resource since it doesnt
> > have
> > access to the function it calls.
> >
> > Are there any way I (in the code) can test if the global template is
> > present, and then execute if present, and just cancel if it is not
> > present.
> >
> > My goal is to letting people be aware of the missing global template, but
> > not get any errors that opens the debug editor and so forth...
> >
> > /Anders
>
>

Re: Check if macro is present? by Jonathan

Jonathan
Thu Jan 19 07:03:14 CST 2006

Hi Anders

That will work, but if you plan to distribute the templates to other users,
I see a potential problem. If your add-in is going to be automatically
loaded as a result of being placed in the Startup folder, then be aware that
everybody's startup folder is different, since the startup folder is a
subfolder with the user profile. This is a problem because references to
other templates are hard-coded to the full pathname of the template, so if
the referenced add-in is somewhere else, it won't be found.

Depending on what you have in the add-in there are two alternatives that
come to mind.

1. Don't set references, but instead call routines in the add-in by using
the Application.Run method. This limits you to a certain extent - you cannot
directly call functions, forms or classes in the add-in, but only public
Subs (with or without parameters) in regular modules.

2. Don't use a template as an add-in, but instead get the code into a
development tool such as VB6 where you can create an ActiveX DLL. Once the
DLL is registered, it can be used more or less like any class module.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org


"pnp" <pnp@discussions.microsoft.com> wrote in message
news:327A91CE-E4AF-4867-89AC-58AA3CD8FDA7@microsoft.com...
>I found a solution somewhat similar. Inspired by KB 308340 i came up with
> the following code:
> The setup is:
> MacroProject.Macro is a function found in a global template (maybe found)
> The following code is placed in the template to a document, and runs when
> the document is opened:
>
> Private Sub Document_Open()
> 'Check if any references is broken
> checkReference
> End Sub
>
> Sub checkReference()
> Dim vbProj As VBProject 'This refers to the vbproject
> Dim chkRef As Reference 'A reference
>
> 'Refer to the vbproject where the code is placed
> Set chkRef = Me.VBProject
>
> 'Check through each reference to see if they are missing
> For Each chkRef In vbProj.References
> If chkRef.IsBroken Then
> 'Errorhandling - In my case a msgbox stating that the global
> template is missing, and how to install it.
> Exit Sub
> End If
> Next
> 'If no references where broken - call the method that calls the method
> in the global template.
> Call UpdateFields
> Exit Sub
>
> End Sub
>
> Sub UpdateFields()
> MacroProject.Macro ' Calls the code in the global template.
> End Sub
>
>
> The code takes advantage of word first compiling the method when calling
> it.
> That is, if the sub UpdateFields is called, and the reference to
> MacroProject
> is missing, a compile error will raise. But if the reference is missing
> and
> the sub is never called no errors occur.
>
> I hope the example is understandable - if anyone sees any problems with it
> let me know. :-)
>
> /Anders
>
>
>
> "Jonathan West" wrote:
>
>> Hi pnp,
>>
>> You can iterate through the Addins collection and see if the template you
>> need is listed in the addins, and if its Loaded property is True.
>>
>> - If it is present and loaded, no action is needed
>> - It is present but not loaded, you can load it.
>> - If it is not present you can see if the file exists, and add it to the
>> Addins collection and load it.
>> - If the file doesn't exist, you issue a warning.
>>
>> --
>> Regards
>> Jonathan West - Word MVP
>> www.intelligentdocuments.co.uk
>> Please reply to the newsgroup
>> Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
>>
>>
>>
>> "pnp" <pnp@discussions.microsoft.com> wrote in message
>> news:055D5D49-1592-45D4-B1A8-96EDE8CF7ACA@microsoft.com...
>> > Is there some way i can check if a macro is present.
>> >
>> > We are on the way to deploying a global template, which contains some
>> > macro
>> > code. Next step is then to integrate some code in our templates that
>> > calls
>> > code in this global template.
>> >
>> > But.... If someone for some reason hasn't received this global
>> > template,
>> > Word will fail, complaining about the missing resource since it doesnt
>> > have
>> > access to the function it calls.
>> >
>> > Are there any way I (in the code) can test if the global template is
>> > present, and then execute if present, and just cancel if it is not
>> > present.
>> >
>> > My goal is to letting people be aware of the missing global template,
>> > but
>> > not get any errors that opens the debug editor and so forth...
>> >
>> > /Anders
>>
>>