开发者

Calling an External VBA from VBScript

I am using a program called mathtype to pull some equation objects out of a word document. I've written code in VBA that works perfectly using their API, but I have to translate it to a VBScript file. I have looked all over google, but have n开发者_JAVA技巧ot found any solution on how (If it is even possible) to call a VBA library from VBScript.

VBScript can't see the MathTypeSDK Objects/Functions.

If not possible, how would I encase the macro I need to run in a globally available word file and call it from the VBScript?

Edit: Got it! Unfortunately the approaches below, while helpful, did not work for my situation. I found something closer: Embedding the macro in a global file and calling it through the Word Objects Run command. objWord.Run "Normal.NewMacros.RunMain"


Here is an approach which might work for you. I tested this simple example.

Class "clsTest" in file "Tester.docm":

Public Sub Hello()
    MsgBox "Hello"
End Sub

Class "Instancing" is marked "PublicNotCreatable".

Module in "Tester.docm":

Public Function GetClass() As clsTest
    Set GetClass = New clsTest
End Function

In your vbscript:

Dim fPath, fName

fPath = "C:\Documents and Settings\twilliams\Desktop\"
fName = "Tester.docm"

Dim wdApp, o

Set wdApp = CreateObject("word.application")
wdApp.visible=true
wdapp.documents.open fPath & fName

Set o = wdApp.Run("GetClass")
o.Hello

Set o=nothing

Again - I only tested this simple example: you'll have to adapt it to your situation and try it out.


Word-VBA was not made to create reusable libraries, I suppose (for usage in external programs).

One way to reuse existing Word-VBA code is, however, run Word via WScript.Shell.Run using the /m<macroname> command line switch (see http://support.microsoft.com/kb/210565/en-us for details). This, has the restriction that evertime you need to call a specific macro, a Word process is started again, running that macro, and ends afterwards. Means, if you need just one call to your Word.VBA for a specfific task, this may be ok, but if you need a lot of interprocess communication between your VBScript and your VBA macro, you should look for a different solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜