Getting a MsBox when Automating Word from the Outside

M

Matthew Lock

Hello,
I am automating MS Word from the "outside' using a script and all is
well. Now I want to get Word to display a MsgBox actually in Word.

How do I call MsgBox to actually come up in Word when I am automating
from the outside?

(From looking through VBA's object inspector I found that MsgBox is a
member of the Interaction class, which is a member of the VBA class.
Can I access the VBA class when all I have in my script is a handle on
the Application class? Or am I barking up the wrong tree here)

Regards,
Matthew Lock
 
D

Doug Robbins - Word MVP

If a Word document is visible at the time the MsgBox is called, then the
MsgBox would appear in front of the Word document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

Matthew Lock

Doug said:
If a Word document is visible at the time the MsgBox is called, then the
MsgBox would appear in front of the Word document.

How do I actually call MsgBox though? From my script I can't simply
write "MsgBox" as the script will think that's a function in my script,
not a function of Word.

I have tried exploring through the Application object, but didn't find
anything there.

Thanks,
Matthew
 
T

Tony Jollans

You don't have access to VBA methods via the Word Application. If your
Script (you don't say what application or scripting language) doesn't have a
native MsgBox you will have to use APIs for a Windows message box.
 
M

mr_unreliable

Er, wait.

vba has a msgbox function.

How about writing a word/vba/macro which calls the mb function.

Then from script, run the macro.

Note: you can find examples of running a vba macro from script
by searching the archives of this ng.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
 
T

Tony Jollans

Yes, that would work if you are always using the same document (or template)
or all users have a global template containing the code but that's far from
guaranteed in most cases.
 
M

mr_unreliable

Tony, I disagree with the notion that you must have your
"msgbox macro" in some universal template, in order to call
a msgbox macro from script.

If fact, you can insert your "msgbox macro" code into word
(or excel) from your script.

Here is some sample code:

--- <snip> ---
Dim oWD : Set oWD = WScript.CreateObject("Word.Application.8")

' oWD.Visible = False ' (optional)

With oWD.VBE.VBProjects("Normal")

' add a standard code module, returns a module object
(vbComponent)...
Set oVBModCode = .VBComponents.Add(vbext_ct_StdModule)

' name the standard code module,
' (so as to keep track of it, for later)...
oVBModCode.Name = "myCodeModule"

' enter your code into the code module...
..VBComponents("myCodeModule").CodeModule.AddFromString(GetResource("myMsgBoxCode"))
End With
--- </snip> ---

The above code was taken from a "wsf" file, which supports "resources".
But that's not really necessary. You could hold the code in an
ordinary string variable.

You can then call your inserted macro, using the "run" method.

--- <snip> ---
' call the macro the you just loaded into msWD...
sResult = oWD.Run("ShowMessageBox") ' ("TestFunction")
--- </snip> ---

Oh, and you either need to define the relevant constants yourself,
or else reference the msWD typelib:

<reference object="Word.document" version="8.0" />

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
 
T

Tony Jollans

Yes that is possible but relies (since Word XP / 2002) on the users having
security set to trust access to the VB project, which is not a default
setting nor one an average user would themselves set.

Many solutions are dependent on the environment to an extent but using APIs
and a Windows Message box should always work. I would expect, however, that
the scripting language would have its own message box facility and that
would be the easiest one to use.
 

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