vba 从 Access 将 Excel 窗口置于前台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25353453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:16:19  来源:igfitidea点击:

Bringing an Excel window to foreground from Access

excelvbams-access

提问by Knodel

I am trying to open an Excel file from Access and it does work, however the Excel window pops up in the background (behind the Access window), which is not very user friendly. Here is the code I use:

我正在尝试从 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

How can I make the Excel window appear in the foreground (on top of all opened windows) instead?

如何让 Excel 窗口出现在前台(在所有打开的窗口之上)?

Thank you!

谢谢!

采纳答案by David Zemens

I would first check for already open instance of Excel. If you mustallow for multiple instances of the Application, then it will be trickier. If you are OK with only using one instance of Excel, then I think this should work using the AppActivatestatement.

我会首先检查已经打开的 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

回答by QuiOui

You have to call AllowSetForegroundWindowbefore making Excel visible. I don't develop in VBA, but I think it would look like this:

您必须AllowSetForegroundWindow在使 Excel 可见之前调用。我不在 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

回答by noelmcg

A little late to the party here,

这里的聚会有点晚了,

(using Office 2013)

(使用 Office 2013)

If Excel isn't already open, I find that:

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

.invisible = true

Brings the Excel window to the front if Excel isn't open. If Excel is already open however, I find I need to set invisible to false first then reset to true to get the window to the front

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

.invisible = false
.invisible = true

Maybe this should work?

也许这应该有效?

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

EDIT:

编辑:

Actually, what seems to work far better is AppActivate

实际上,似乎效果更好的是 AppActivate

AppActivate(nameOfExcelFile)

AppActivate Statement

应用激活声明