2012-01-06 53 views
0

我已经编写了一个Visual Basic程序,它连接到Access数据库,生成一些SQL语句,然后尝试将结果写入Excel文件。将Excel SaveAs对话框置于Visual Basic(不是VBA)的前面

一切工作正常,除了当它调用另存为对话框:

xlApp.Dialogs(Excel.XlBuiltInDialog.xlDialogSaveAs).Show() 

的对话框程序,这是最大的后面。因此,该程序似乎挂起,因为它正在等待关闭对话框,但对话框无法访问(除Alt + Tab,但这是一个丑陋的解决方法)。

任何方式让我强制对话框到前面?我发现一个相关的线程here,但我不处理单独的线程。那里的OP建议BringToFront方法,但我不知道如何使用我的xlApp.Dialogs。

在此先感谢您的帮助!

回答

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