PDA

View Full Version : open a new instance of Excel



Djblois
11-23-2009, 07:19 AM
I want to be able to open a new instance of Excel.

How do I do this with VBA and how do I do it with a keyboard shortcut?

Bob Phillips
11-23-2009, 07:51 AM
Set xlApp = CreateObject("Excel.Application")


Put it in a macro and assign a shortcut with



Application.OnKey "^+!","myMacro"

Djblois
11-23-2009, 12:07 PM
I tried this code to get a particular workbook to open in Excel in a new instance but it didn't work. What am I doing wrong?

blnIsOpen = True
On Error Resume Next
Set wbRef = Workbooks(sWorkbook)

If Err Then
Set xlApp = CreateObject("Excel.Application")
Set wbRef = xlApp.Workbooks.Open(sWorkbookToOpen, , blnReadOnly, , , , True, , , , , , False)
blnIsOpen = False
End If
If blnOpenRef = True Then
wbRef.Activate
Else
wsWorking.Activate
End If
On Error GoTo 0

The workbook just never opened - I did not get an error or anything it just didn't open.

GTO
11-23-2009, 04:28 PM
Greetings Daniel,

A bit of guessing, as I don't know where blnOpenRef comes from etc, but are you sure its not opening? It created a new instance for me, but as visibility has not been set to true, the app would remain hidden.

You might wish to try it like below. Rather than ignoring errors for several statements, we could limit it to just the Set wbRef... line. This way, we're not flying by any errors in opening/activating the wb or activating a sheet.


Sub exa2()
Dim blnIsOpen As Boolean
Dim blnReadOnly As Boolean
Dim blnOpenRef As Boolean
Dim wbRef As Workbook
Dim xlApp As Excel.Application
Dim wsWorking As Worksheet
Dim strPath As String
Dim sWorkbookToOpen As String
Dim sWorkbook As String

strPath = ThisWorkbook.Path & "\"
sWorkbook = "Fake.xls"
sWorkbookToOpen = strPath & sWorkbook

blnIsOpen = True
On Error Resume Next
Set wbRef = Workbooks(sWorkbook)
On Error GoTo 0

If wbRef Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
Set wbRef = xlApp.Workbooks.Open(sWorkbookToOpen, , blnReadOnly)
xlApp.Visible = True
blnIsOpen = False
End If
If blnOpenRef = True Then
wbRef.Activate
Else
' wsWorking.Activate
End If
End Sub


Hope that helps,

Mark

Djblois
11-24-2009, 08:23 AM
Ok, you are correct it is opening but none of the formulas that I have coded to work in the first instance of Excel will reference the workbook in the new instance of excel. How do I get it to reference a different instance of excel. Do I have to use the xlApp in every situation that I reference the workbook?

Bob Phillips
11-24-2009, 10:14 AM
What exactly do you want to do with this new instance Daniel?

Djblois
11-24-2009, 11:50 AM
Here is an example of one thing I want to do with it. rngAddto will be in the first instance of Excel and wbRef will be the workbook opening in the new instance of Excel.

Sub comAddWhse(ByVal rngAddto As Range, ByVal intColumnDiff As Integer)

'Adds Warehouse from Warehouse Number from BusinessReportingReference.xls
With rngAddto
.FormulaR1C1 = _
"=if(ISNA(VLOOKUP(RC[" & intColumnDiff & "],'[" & wbRef.Name & "]Whses'!C1:C2,2,FALSE)),"""",VLOOKUP(RC[" & intColumnDiff & "],'[" & wbRef.Name & "]Whses'!C1:C2,2,FALSE))"
.Value = rngAddto.Value 'Changes Formula into Values
End With
End Sub

mdmackillop
11-24-2009, 04:58 PM
To satisfy my curiosity, why do you particularly need the second instance?

Djblois
12-04-2009, 10:28 AM
Sorry mdmackillop I thought I responded to you already - now that I came back here I see that I did not.

The reason I need to open a new instance of Excel is because we use our DBMA to create Excel Reports and it takes a long time. While that report is running, if someone opens another Excel file it will crash the running report. So theoretically opening a new instance of Excel may prevent this issue. (it is hard to test to see if it fixes the issue because it does not always crash the new report)

timsta13
08-21-2012, 02:11 PM
Greetings Daniel,

A bit of guessing, as I don't know where blnOpenRef comes from etc, but are you sure its not opening? It created a new instance for me, but as visibility has not been set to true, the app would remain hidden.

You might wish to try it like below. Rather than ignoring errors for several statements, we could limit it to just the Set wbRef... line. This way, we're not flying by any errors in opening/activating the wb or activating a sheet.


Sub exa2()
Dim blnIsOpen As Boolean
Dim blnReadOnly As Boolean
Dim blnOpenRef As Boolean
Dim wbRef As Workbook
Dim xlApp As Excel.Application
Dim wsWorking As Worksheet
Dim strPath As String
Dim sWorkbookToOpen As String
Dim sWorkbook As String

strPath = ThisWorkbook.Path & "\"
sWorkbook = "Fake.xls"
sWorkbookToOpen = strPath & sWorkbook

blnIsOpen = True
On Error Resume Next
Set wbRef = Workbooks(sWorkbook)
On Error GoTo 0

If wbRef Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
Set wbRef = xlApp.Workbooks.Open(sWorkbookToOpen, , blnReadOnly)
xlApp.Visible = True
blnIsOpen = False
End If
If blnOpenRef = True Then
wbRef.Activate
Else
' wsWorking.Activate
End If
End Sub


Hope that helps,

Mark

This worked perfectly for me, just modified it slightly to feed the workbook path into the sub. Using it in a "control centre" type application giving easy access to the desired workbooks, and enabling more than one task to be accessed at once.

Cheers