[英]Bringing an Excel window to foreground from Access
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.我正在尝试从 Access 打开一个 Excel 文件并且它确实可以工作,但是 Excel 窗口会在后台弹出(在 Access 窗口后面),这对用户来说不是很友好。 Here is the code I use:
这是我使用的代码:
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!谢谢!
I would first check for already open instance of Excel. 我首先检查已经打开的Excel实例。 If you must allow 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 AppActivate statement.
如果您只使用一个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
You have to call AllowSetForegroundWindow
before making Excel visible. 在使Excel可见之前,您必须调用
AllowSetForegroundWindow
。 I don't develop in VBA, but I think it would look like this: 我没有在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
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. 如果Excel未打开,则将Excel窗口置于前面。 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已经打开,我发现我需要首先将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 oWrkBk.Name & "-Excel"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.