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:
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?
Thank you!
I would first check for already open instance of 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.
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