将 Excel SaveAs 对话框置于前面 Visual Basic(不是 VBA)

发布于 2024-12-25 08:52:49 字数 534 浏览 0 评论 0原文

我编写了一个 Visual Basic 程序,它连接到 Access 数据库,生成一些 SQL 语句,然后尝试将结果写入 Excel 文件。

一切正常,除了调用“另存为”对话框时:

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

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

有什么办法让我将对话框强制放在前面吗?我在此处找到了相关主题< /a> 但我不处理单独的线程。那里的OP建议使用BringToFront方法,但我不确定如何在我的xlApp.Dialogs中使用它。

预先感谢您的帮助!

I've written a Visual Basic program that connects to an Access database, makes a few SQL statements, then tries to write it the results to an excel file.

Everything works properly, except when it calls the SaveAs dialog:

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

The dialog is behind the program, which is maximized. Thus, the program appears to hang as it's waiting for the dialog to close, but the dialog cannot be accessed (except by Alt+Tab, but that's an ugly workaround).

Any way for me to force the Dialog to the front? I found a related thread here but I'm not dealing with separate threads. The OP there suggests the BringToFront method, but I'm not sure on how to use that with my xlApp.Dialogs.

Thanks in advance for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2025-01-01 08:52:49

找到窗口句柄并调用 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)

不要忘记同时装饰 SetForegroundWindow:

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Find the window handle and call 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

So with the above function you should be able to do something like:

Dim saveAsHwnd as Long

call saveAsHwnd = FindWindowByPartialTitle("Save")
call SetForegroundWindo(saveAsHwnd)

Don't forget to decalre SetForegroundWindow as well:

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文