• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 867
  • Last Modified:

applescript to run word98 vba subroutine

I have tried a variety of things to run a word 98 visual bacic subroutine from applescript. I have a public sub with no parameters named "script". I have tried:
tell application "Microsoft Word"
  run "Script" -- no response
  do script "run script" -- ambiguous name detected: TmpDDE
I can find no references as to how to do this. How should I do it?

BTW, from FoxPro I can actually and easily run a subroutine and pass parameters. I'd like to do that from Apple Script also.
0
ramrom
Asked:
ramrom
  • 7
  • 6
1 Solution
 
ramromconsultant Author Commented:
Edited text of question.
0
 
trottieaCommented:
Say you create a macro with Visual Basic editor and call it DocNames. After launching Word, you do menu Tools>Macro>Visual Basic Editor, menu View>Project Explorer (Command-R), click the Normal project, then menu Insert>Module, and type this, for example:

Public Sub DocNames()
  For Each aDoc In Documents
    aName = aName & aDoc.Name & vbCr
  Next aDoc
  MsgBox aName
End Sub

That VB macro will show the names of every open document in a message box.
Close Visual Basic and return to your new document.

The AppleScript to execute the macro would then be:

on run
      tell application "Microsoft Word"
  activate
            do Visual Basic "DocNames"
      end tell
end run

0
 
trottieaCommented:
You must make sure to select the Normal project before inserting a new module, or else Word might not find the script you requested, and will generate an error (TmpDDE). You can find additional Help & reference files on that topic on the Office Installation CD-ROM. All the information is there.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ramromconsultant Author Commented:
Am firing up my mac to try this. It continues to amaze and disappoint me that MS makes it so hard to find the information we need. I sure with they had some sense of standards and quality assurance.
0
 
ramromconsultant Author Commented:
What about passing parameters?
0
 
ramromconsultant Author Commented:
"Office Installation CD-ROM"
I only have the Word 98 CD. What help file or reference do I look in? And what topic? So far I can find no reference to applescript of visual basic as a command.
0
 
ramromconsultant Author Commented:
I am sorry to report that your suggestion does not work.

In the project window I followed the tree
Normal
  Microsoft Word Objects
    This document.
That led to a module window in which I put a public sub. When I run my script I get
"compile error:
Sub or Function not defined"
in Normal - WordTmpDDEMod Sub TmpDDE
0
 
trottieaCommented:
I agree with your comments about Microsoft.

I don't know about passing parameters.

Also, I have the Canadian French version of Office. The documentation about VB was in the Microsoft Office 98:Compléments:Aide supplémentaire folder (this should translate to Microsoft Office 98:Extra:Additional Help or something similar, you can search for the file "MS Word VBA Help" and you should find the other Help files as well). I guess that file should also be on the Word-only CD.

And I also got the TmpDDE error, but only when I created the VB macro inside a particular document. You must create the module directly in Normal, so that it's in the Normal template and not local to the document. I guess you'll find more info in the Help file. And you should delete the whole TmpDDE sub if you don't want to get that error again.

I did it exactly the way I wrote it in my proposed answer and it worked.

Good luck! (And Merry Christmas.)
0
 
ramromconsultant Author Commented:
Please review the way I described what I did. Is this not the way to put it in the Normal template?

Of course putting it there is of no use to me, since I aim to distribute a word file containing macros, and want to run onme of those without disturbing the client's normal template.

I looked at the help files; there is no information that I can find about calling a macro from applescript. Do you have an explicit reference to a specific help file and topic? So far your references are vague.
0
 
trottieaCommented:
I found out how to use parameters. Say you have a VB macro called MessageX(aMessage) which displays a message box:

Sub MessageX(aName)
MsgBox aName
End Sub

The AppleScript to use the macro is

on run
 tell application "Microsoft Word"
  activate
  do Visual Basic "MessageX \"Hello, World!\""
 end tell
end run

So the question is not how to call a macro from AppleScript, but how to call a macro that's local to a document instead of global. (By the way, you'll find reference about AppleScript and Word if you launch the AppleScript Editor and do File Menu>Open Dictionary and select the Word application.)

I'm looking through the Word VBA Help files. I'll get back to you when I find something.
0
 
trottieaCommented:
Yeehah! I found it.

If you created a macro called NameMsg in a document called Document2, for example, use this AppleScript:

on run
 tell application "Microsoft Word"
  activate
  do Visual Basic "Documents(\"Document2\").NameMsg \"Hello, World!\""
 end tell
end run

The code for the macro NameMsg is

Public Sub NameMsg(aMessage)
 MsgBox aMessage
End Sub

You should create it in ThisDocument, using the path you specified in one of your previous comments. Just double-click on the ThisDocument icon and type in your macro.

Hope your question is answered.
0
 
ramromconsultant Author Commented:
Yeehah indeed! Now, if only Microsoft had risen high enough to provide the information. Even searching MSDN and calling tech support did not help. Would you be kind enough to tell me where you found it?
0
 
trottieaCommented:
All the information is in the "MS Word VBA Help" file on the CD-ROM. I found some information in the Reference section (how to call macros) and some information in the Concepts section (how to access document objects). But it is extremely difficult to find what you're looking for in those files (there isn't even a search engine).

Happy new year and the best wishes from Montréal.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now