以编程方式关闭MsgBox

时间:2017-01-24 04:40:59

标签: excel vba excel-vba

我在Excel文件中有一个主宏,'文件A'打开另一个Excel文件,'文件B'。在打开时,加载项将数据导入到文件B'中。我想关闭提交B'加载项完成导入后,我正在寻找最佳方法。

我已经编写了代码来打开文件B' (它会自动触发加载项)并关闭文件,但是当加载项完成后,它会打开一个MsgBox来通知用户。我试图完全自动化内部流程,因此以编程方式解雇MsgBox将是理想的。

是否可以通过VBA解雇MsgBox?我知道我可以在VBA中创建定时MsgBoxes,但我没有创建这个MsgBox(加载项是);我只是想解雇它。我打开创建Word文件并根据需要调用宏,但不想使用SendKeys。

2 个答案:

答案 0 :(得分:0)

由于“加载项”和Excel / VBA在相同的上下文中运行,我们无法在相同的 VBA应用程序中启动它并监视其消息框,因为每个VBA应用程序都是< em>单线程进程。幸运的是,有一种解决方案可以利用不同VBA应用程序在不同上下文中运行的事实,因此它们可以并行运行。

我建议的解决方案是创建一个专门用于监视和关闭该消息框的MS-Word文档。我们需要在Word(或任何其他办公应用程序)中使用它,以便在不同的上下文中使监视代码和插件的代码并行运行。

1-创建一个名为mboxKiller.docm的Word宏启用文档,并将其放在某个文件夹中;在我的例子中是C:\SO。将此代码放在ThisDocument中并保存:

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Sub WaitAndKillWindow()
    On Error Resume Next
    Dim h As Long: h = FindWindow(vbNullString, "Microsoft Excel")
    If h <> 0 Then SendMessage h, 16, 0, 0 ' <-- WM_Close
    Application.OnTime Now + TimeSerial(0, 0, 1), "WaitAndKillWindow"
End Sub

Private Sub Document_Open()
    WaitAndKillWindow
End Sub

2-在Excel工作簿的VBA中,使用以下代码创建一个名为mboxKiller的类模块:

Private killerDoc As Object

Private Sub Class_Initialize()
    On Error Resume Next
    Set killerDoc = CreateObject("Word.Application").Documents.Open(Filename:="C:\SO\mboxKiller.docm", ReadOnly:=True)
    If Err.Number <> 0 Then
        If Not killerDoc Is Nothing Then killerDoc.Close False
        Set killerDoc = Nothing
        MsgBox "could not lauch The mboxKiller killer. The message-box shall be closed manuallt by the user."
    End If
End Sub

Private Sub Class_Terminate()
    On Error Resume Next
    If Not killerDoc Is Nothing Then killerDoc.Application.Quit False
End Sub

3-测试和使用。在普通的类模块中,放置以下代码并测试过程

Sub Test() ' <-- run this for testing after finishing the setup
    Dim killer: Set killer = New mboxKiller
    simulateAddin
    simulateAddin
    simulateAddin
End Sub

' Procedure supposed to do some calculation then display a message box
Private Sub simulateAddin()
    Dim i As Long
    For i = 0 To 1000: DoEvents: Next ' simulates some calculations
    MsgBox "This is a message box to simulate the message box of the addin." & VbCrLf & _
    "It will be automatically closed by the Word app mboxKiller"
End Sub

答案 1 :(得分:0)

VBA 还可以暂时关闭警报。

Application.DisplayAlerts = False
'while you run your code here, no alerts will be displayed
Application.DisplayAlerts = True