【问题标题】:Bringing an Excel window to foreground from Access从 Access 将 Excel 窗口置于前台
【发布时间】:2014-08-17 20:45:18
【问题描述】:

我正在尝试从 Access 打开一个 Excel 文件,它确实可以工作,但是 Excel 窗口会在后台弹出(在 Access 窗口后面),这对用户不是很友好。这是我使用的代码:

Private Function OpenExcelAttachment()
Dim MyXL As Object
Set MyXL = CreateObject("Excel.Application")
With MyXL

   Dim FullPath As String, Name As String
   Name = "\ExcelFile.xlsx"
   FullPath = CurrentProject.Path & Name
   .Workbooks.Open FullPath
   .Visible = True

 End With

我怎样才能让 Excel 窗口出现在前台(在所有打开的窗口之上)?

谢谢!

【问题讨论】:

  • 应该是这样,因此您的代码没有问题。程序只有很少的时间将自己设置为前台窗口,是不是加载文件的时间很长?尝试先设置可见然后加载文件。 The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
  • The process is the foreground process. The process was started by the foreground process. The process received the last input event. There is no foreground process. The foreground process is being debugged. The foreground is not locked (see LockSetForegroundWindow). The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo). Windows 2000/XP: No menus are active.
  • 感谢您的回复!文件很小,所以根本不需要很长时间。当我设置可见时玩弄也没有帮助。有趣的是,我发现如果我在调用 Excel 之前打开 VBA,它确实会显示在前台!但是,如果我退出 Access,再次打开它并调用 Excel(潜在用户会这样做),它会在后台显示...
  • 从 MS 编程语言工具文件夹或 Windows SDK 工具文件夹中尝试 Spy++。通过记录所有消息查看发生了什么。

标签: excel vba ms-access


【解决方案1】:

我会首先检查已经打开的 Excel 实例。如果您必须允许应用程序的多个实例,那么这将更加棘手。如果您可以只使用一个 Excel 实例,那么我认为这应该使用 AppActivate 语句来工作。

Private Function OpenExcelAttachment()
Dim MyXL As Object

On Error Resume Next
Set MyXL = GetObject(,"Excel.Application")
If Err.Number <> 0 Then Set MyXL = CreateObject("Excel.Application")
On Error GoTo 0

With MyXL
   Dim FullPath As String, Name As String
   Name = "\ExcelFile.xlsx"
   FullPath = CurrentProject.Path & Name
   .Workbooks.Open FullPath
   .Visible = True
End With

AppActivate "Microsoft Excel"

End Function

【讨论】:

  • 感谢您的回复!它确实有效,但是,正如您所指出的,它需要一个 Excel 实例——这是无法保证的......哦,好吧,无论如何它都比以前更好了!
【解决方案2】:

AppActivate oWrkBk.Name & "- Excel"

【讨论】:

    【解决方案3】:

    在使 Excel 可见之前,您必须调用 AllowSetForegroundWindow。我没有在 VBA 中开发,但我认为它看起来像这样:

    Private Declare Function AllowSetForegroundWindow Lib "user32.dll" (ByVal dwProcessId As Long) As Long
    Private Function OpenExcelAttachment()
    Dim MyXL As Object
    Set MyXL = CreateObject("Excel.Application")
    AllowSetForegroundWindow -1
    With MyXL
    
       Dim FullPath As String, Name As String
       Name = "\ExcelFile.xlsx"
       FullPath = CurrentProject.Path & Name
       .Workbooks.Open FullPath
       .Visible = True
    

    【讨论】:

      【解决方案4】:

      在这里聚会有点晚了,

      (使用 Office 2013)

      如果 Excel 尚未打开,我发现:

      .invisible = true
      

      如果 Excel 未打开,则将 Excel 窗口置于最前面。但是,如果 Excel 已经打开,我发现我需要先将不可见设置为 false,然后重置为 true 以使窗口位于前面

      .invisible = false
      .invisible = true
      

      也许这应该有效?

      Private Function OpenExcelAttachment()
      Dim MyXL As Object
      Set MyXL = CreateObject("Excel.Application")
      With MyXL
      
         Dim FullPath As String, Name As String
         Name = "\ExcelFile.xlsx"
         FullPath = CurrentProject.Path & Name
         .Workbooks.Open FullPath
      
         .Visible = False
         .Visible = True
      
       End With
      

      编辑:

      实际上,看起来效果更好的是 AppActivate

      AppActivate(nameOfExcelFile)
      

      AppActivate Statement

      【讨论】:

        猜你喜欢
        • 2012-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-16
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多