简体   繁体   English

从Excel调用Word VBA MsgBox函数

[英]Call Word VBA MsgBox function from Excel

Is it possible to call Word's MsgBox function from Excel VBA? 是否可以从Excel VBA调用Word的MsgBox函数? I am having trouble doing this as MsgBox is not a method of Word.Application. 我这样做有困难,因为MsgBox不是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. 我试图这样做,以便我可以在打开的Word文档前面显示一个消息框,说明文档呈现已完成。 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. 如果我只是调用Excel的MsgBox,那么我必须先单击Windows任务栏中的闪烁按钮才能看到消息框。

The word document is generated entirely by my Excel macro. word文档完全由我的Excel宏生成。

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. 它现在配置为PPT,但是我发现的Excel功能一起被黑客入侵。 There is also a way to use VBA to write VBA. 还有一种方法可以使用VBA来编写VBA。 Both are fairly advanced though. 两者都相当先进。 Probably both sets of functions are available here: 可能这里有两组功能:

http://www.cpearson.com/excel/vbe.aspx 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. 这可能是你最好的选择,使用弹出窗口代替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? 什么是显示带有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. 我能够实现这一目标的方法是在创建消息框的单词doc中创建一个子例程。

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: 然后在Word文档中:

Public Sub Testing()
 MsgBox ("Hello World")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM