Pregunta

Is it possible to call Word's MsgBox function from Excel VBA? I am having trouble doing this as MsgBox is not a method of Word.Application.

I have tried this:

Dim objWord As Word.Application
Set objWord = New Word.Application
Dim objDoc As Word.Document
Set objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Activate
Call objWord.Run("MsgBox", "Hello World") ' Or:
Call objDoc.Run("MsgBox", "Hello World")

But I get the error "Object doesn't support this property or method", no matter which 'Call' line I include.

I am trying to do this so that I can display a message box in front of the open Word document saying that the document rendering has completed. If I just call Excel's MsgBox then I have to click on the flashing button in the Windows Taskbar before I can see the message box.

The word document is generated entirely by my Excel macro.

Is this possible?

¿Fue útil?

Solución

I have some functions that export modules and re-import them. It is configured now for PPT but was hacked together from Excel functions that I found. There is also a way to use VBA to write VBA. Both are fairly advanced though. Probably both sets of functions are available here:

http://www.cpearson.com/excel/vbe.aspx

However, that's probably overkill. This is probably your best bet, to use a popup instead of a VBA msgBox.

Sub Test()
Dim wdApp As Object
Dim shl As Object

Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
wdApp.Activate
Set shl = CreateObject("wscript.shell")

shl.Popup ("Hello, world!")

Set shl = Nothing
Set wdApp = Nothing

End Sub

See also here for more detail about how to control it's timeout/etc.

Whats the best way to display a message box with a timeout value from VBA?

Otros consejos

It is! The problem with the way that you're doing it is that the run function of the word application looks for macros with that name and runs them. The way that I was able to accomplish this is by making a subroutine in the word doc that creates the message box.

Public Sub Testing()
 Dim objWord As Word.Application
 Set objWord = New Word.Application
 Dim objDoc As Word.Document
 Set objDoc = objWord.Documents.Open("C:\whatnot\Stuff.docm")
 objWord.Visible = True
 objWord.Activate
 objWord.Run ("Testing")
End Sub

Then in the Word document:

Public Sub Testing()
 MsgBox ("Hello World")
End Sub
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top