简体   繁体   English

将 Excel 窗口从 Access 置于前台

[英]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 Statement AppActivate声明

AppActivate oWrkBk.Name & "-Excel"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Internet Explorer窗口置于前台 - Bringing Internet Explorer window to foreground 将Excel窗口返回到前景 - Get Excel Window back to Foreground 有没有办法从 AppleScript 创建或打开 Excel 工作簿而不会成为前台窗口? - Is there a way to create or open an Excel workbook from AppleScript without it becoming the foreground window? Excel:从单元格中提取一个完整的数字-将所有数字组合在一起:) - Excel: Extracting a full number from a cell - bringing it all together :) Excel | 在不同工作表中搜索表并从该行中的列中获取值 - Excel | Searching table in different sheet and bringing value from column in that row 从 Excel 打开 Access 报告转到“另存为”窗口 - Opening Access Report from Excel goes to the 'save as' window 从Excel打开时将访问窗口置于顶部 - Bring Access Window On Top When Opened From Excel 使用Selenium VBA将动态网页表中的单元格值带到excel单元格 - Using selenium VBA bringing the cell values from a dynamic web page table to excel cells 确定弹出窗口是否在Excel的前景中 - Determine if Popup is in foreground of Excel 自动化Internet Explorer后如何将Excel窗口置于前台? - How can I bring the Excel window to the foreground after automating Internet Explorer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM