我编写了一个Visual Basic程序,它连接到Access数据库,生成一些SQL语句,然后尝试将结果写入excel文件。
一切正常,除非它调用SaveAs对话框:
xlApp.Dialogs(Excel.XlBuiltInDialog.xlDialogSaveAs).Show()
对话框在程序后面,最大化。因此,程序似乎在等待对话框关闭时挂起,但无法访问对话框(除了Alt + Tab,但这是一个丑陋的解决方法)。
任何方式让我强迫对话到前面?我找到了一个相关的线程here,但我没有处理单独的线程。 OP那里建议使用BringToFront方法,但我不确定如何在我的xlApp.Dialogs中使用它。
提前感谢您的帮助!
答案 0 :(得分:0)
找到窗口句柄并调用SetForegroundWindow
Public Function FindWindowByPartialTitle(ByVal _
TestFlex_string As String) As Long
m_TestFlexString = TestFlex_string
m_TestFlexHwnd = 0
' Enumerate windows.
EnumWindows AddressOf EnumCallback, 0
' Return the hWnd found (if any).
FindWindowByPartialTitle = m_TestFlexHwnd
End Function
' Check a returned task to see if it's the one we want.
Public Function EnumCallback(ByVal app_hWnd As Long, ByVal _
param As Long) As Long
Dim buf As String
Dim title As String
Dim Length As Long
' Get the window's title.
Length = GetWindowTextLength(app_hWnd)
buf = Space$(Length)
Length = GetWindowText(app_hWnd, buf, Length)
title = Left$(buf, Length)
' See if the title contains the TestFlex string.
If InStr(title, m_TestFlexString) <> 0 Then
' This is the one we want.
m_TestFlexHwnd = app_hWnd
' Stop searching.
EnumCallback = 0
Else
' Continue searching.
EnumCallback = 1
End If
End Function
因此,使用上述功能,您应该可以执行以下操作:
Dim saveAsHwnd as Long
call saveAsHwnd = FindWindowByPartialTitle("Save")
call SetForegroundWindo(saveAsHwnd)
不要忘记decalre SetForegroundWindow:
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long