4

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 not 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"

2
  • You could hook up your macro to the "open" event in a Word document, then just open the word file from VBScript. It all depends on how much interaction you need between the VBA and VBscript code. Is the Mathtype code in a dll or is it a VBA add-in? Commented May 31, 2011 at 19:55
  • Basically I just need one class (I could just redefine it in my VBScript) and one function (this is the troubling part) from a "global template" VBA project (The reference in VBA is to a .dot). Commented May 31, 2011 at 20:05

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.