[英]Bringing an Excel window to foreground from Access
我正在嘗試從 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 窗口出現在前台(在所有打開的窗口之上)?
謝謝!
我首先檢查已經打開的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可見之前,您必須調用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
這里聚會有點晚了,
(使用Office 2013)
如果Excel尚未打開,我發現:
.invisible = true
如果Excel未打開,則將Excel窗口置於前面。 如果Excel已經打開,我發現我需要首先將invisible設置為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 oWrkBk.Name & "-Excel"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.