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