How do I programatically import a macro if it doesn't exist?

M

M

Hello helpful readers.

I am working with a distributed client/server application that needs
to run word macros on the users local machine. Of course the local
version of word may not know about the macro that it needs to run as
it may not exist on the local machine.

The application that I am working with can get an instance of the
Word.Application object with which I am able to do some things. What I
want to do is test to see if a given macro exists in the Application
object. If it does exist I'll run it ( Easy to do ) and if doesn't I
want to import it into the Application object and then run it.

Can this be done? If so what is the code, or where do I need to look
in the help files, so that I can write the code to do this.

Thanks in advance for all help.

AH
 
T

Tom Winter

M said:
Hello helpful readers.

I am working with a distributed client/server application that needs
to run word macros on the users local machine. Of course the local
version of word may not know about the macro that it needs to run as
it may not exist on the local machine.

The application that I am working with can get an instance of the
Word.Application object with which I am able to do some things. What I
want to do is test to see if a given macro exists in the Application
object. If it does exist I'll run it ( Easy to do ) and if doesn't I
want to import it into the Application object and then run it.

Can this be done? If so what is the code, or where do I need to look
in the help files, so that I can write the code to do this.

Thanks in advance for all help.

AH

You might want to include the macro in a .DOT add-in. From your application,
you can then programmatically check to see if the add-in is loaded, and if
not, load it. Once the add-in is loaded, you can then call the macro.
Something like this:

----------------------------------------------------------------------------

' Assume oWord is instance of Word.Application

If GetWordAddIn(oWord, "MyTemplateWithMacro.dot") Is Nothing Then

oWord.AddIns.Add "S:\PathOnMyServer\MyTemplateWithMacro.dot", True

End If

' We now assume the add-in is loaded

oWord.Run "NameOfMyMacro", Parameter1, Parameter2, etc

' Now we remove the add-in, though we don't have to if we don't really want
to

RemoveWordAddIn oWord, "MyTemplateWithMacro.dot"

Public Function GetWordAddIn(oWord As Word.Application, sName As String) As
Word.AddIn

' sName is just the file name of the add-in, without path.

On Error GoTo ErrorHandler

Dim oCurrAddIn As Word.AddIn

For Each oCurrAddIn In oWord.AddIns

If StrComp(oCurrAddIn.Name, sName, vbTextCompare) = 0 Then

Set GetWordAddIn = oCurrAddIn

Exit Function

End If

Next

Set GetWordAddIn = Nothing

Exit Function

ErrorHandler:

Set GetWordAddIn = Nothing

End Function

Public Sub RemoveWordAddIn(oWord As Word.Application, sName As String)

' sName is just the file name of the add-in, without path.

On Error GoTo ErrorHandler

Dim oWordAddIn As Word.AddIn

Set oWordAddIn = GetWordAddIn(oWord, sName)

If Not oWordAddIn Is Nothing Then

' The 'Delete' method doesn't actually delete the add-in file,
it just
' turns it off. It removes the add-in from the add-ins list.
It's like
' clicking the Remove button the Templates and Add-Ins dialog.

oWordAddIn.Delete

End If

Exit Sub

ErrorHandler:

Exit Sub

End Sub

----------------------------------------------------------------------------

If your "application" is VBA code within a Word document, you can add a
reference to the other file at run-time:

ActiveDocument.VBProject.References.AddFromFile
"S:\PathOnMyServer\MyTemplateWithMacro.dot"

' You can now call the macro directly

Result = NameOfMyMacro(Paramter1, Parameter2)

-------------------------------------------------------------------------

You can also import the actual code module into your Word document's VBA
project, though this is probably not the best way:

oWord.ActiveDocument.VBProject.VBComponents.Import "FileNameOfMyModule.txt"

Hope that helps!

-Tom
 
M

M

Hi Tom,

Thanks for your response. I used your ideas and have solved my
problem. I did have an issue with one of your suggestions which you
may be able to shed some light on.

If your "application" is VBA code within a Word document, you can add a
reference to the other file at run-time:

ActiveDocument.VBProject.References.AddFromFile
"S:\PathOnMyServer\MyTemplateWithMacro.dot"

' You can now call the macro directly

Result = NameOfMyMacro(Paramter1, Parameter2)
</snip>

I tried to do this but kept getting an error saying the resource
already existed. I am not sure if it was trying to load all of the
References from the new file or just the macros I coded (VBA code).
In anycase I added my .dot file as and addin which seemed to work
fine.

Once again, thanks for your help.
 
T

Tom Winter

M said:
Hi Tom,

Thanks for your response. I used your ideas and have solved my
problem. I did have an issue with one of your suggestions which you
may be able to shed some light on.


</snip>

I tried to do this but kept getting an error saying the resource
already existed. I am not sure if it was trying to load all of the
References from the new file or just the macros I coded (VBA code).
In anycase I added my .dot file as and addin which seemed to work
fine.

Once again, thanks for your help.

Honestly, I have never used this to reference another .DOT file (I've used
it to reference COM components). This may be a problem if you ALL READY have
the file referencing are try to reference it again. I could see Word/VBA
complaining about that.

(Going the add-in route is how I would do this ideally!)

-Tom
 

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