문제

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?

도움이 되었습니까?

해결책

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?

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top