Reputation: 727
From the code of Excel A , I want to open Excel B.
Using this code, the new Excel is open in a new Excel Application, so you have 2 independent Excel windows, BUT! The Workbook_Open code of the sFileB is not executed:
Dim myApp As New Excel.Application
myApp.Workbooks.Open sFileB
myApp.Visible = True
If I open the new Excel inside the same application then I have only 1 Excel windows with the 2 files open, but opening the file in this way the Workbook_Open event is executed correctly:
Workbooks.Open sFileB
I want to open the new excel sFileB in a new application and, of course, I want to execute the Workbook_Open code of sFileB. Do you know the reason of why in the first case the Workbook_Open is not executed? Any solution to force the execution of the event? Maybe I'm doing something wrong... Thanks for your help!
Upvotes: 0
Views: 472
Reputation: 727
Thanks to Paul's answer, I could do some more testing and discovered that the point is to write FIRST the line myApp.Visible = True before the line myApp.Workbooks.Open sFileB.
So this is the definitive working code:
Set myApp = CreateObject("Excel.Application")
myApp.Visible = True
myApp.Workbooks.Open sFileB
Upvotes: 0
Reputation: 10715
Use this
Option Explicit
Public Sub OpenXLFileInNewInstance()
Dim xlApp As Excel.Application, wb As Workbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set wb = xlApp.Workbooks.Open("D:\Tmp\xTemplate.xlsm")
'wb.Close False
'xlApp.Quit
End Sub
Upvotes: 1