vba 在 Excel 中使用 WScript.CreateObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43806988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:33:20  来源:igfitidea点击:

Using WScript.CreateObject in Excel

excelvbashell

提问by Hareborn

I have been researching using

我一直在研究使用

WScript.CreateObject("WScript.Shell")

to act as a means to create a multi-line, hover-over, 5-second pop-up for a command button on a Userform. From everything I can figure this should be working fine, but it's giving a "Variable not defined"error. On debugging it highlights "WScript"in the Setline as the issue.

作为一种为用户窗体上的命令按钮创建多行、悬停、5 秒弹出窗口的方法。从我能想到的一切来看,这应该可以正常工作,但它给出了“未定义变量”错误。在调试时,它"WScript"Set问题行中突出显示。

Private Sub CB1604A_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
                              ByVal X As Single, ByVal Y As Single)

Dim WshShell
Dim BTN
Set WshShell = WScript.CreateObject("WScript.Shell")

BTN = WshShell.PopUp("What do you want to do?", 5)

Select Case BTN
    Case 6
        WScript.Echo "Do it now."
    Case 7
        WScript.Echo "Do it later."
    Case -1
        WScript.Echo "Cancel all actions?"
End Select

End Sub

回答by Alex K.

You would need to add a reference to WScriptto use its CreateObject- but you don't need to; instead use VBA's CreateObjectto create an instance of .Shell:

您需要添加WScript对使用它的引用CreateObject- 但您不需要;而是使用 VBACreateObject创建一个实例.Shell

Set WshShell = CreateObject("WScript.Shell")
BTN = WshShell.PopUp("What do you want to do?", 5)

And subsequently use MsgBoxinstead of WScript.Echo:

随后使用MsgBox代替WScript.Echo

...
MsgBox "Do it now."