Open New Instance

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
The first code closes any new workbook and the second code opens a new instance. If an attempt to open an existing file or start a new workbook, Instead of closing the new workbook, I would like it to open in a new instance. How can I combine them?



VBA Code:
If Workbooks.Count > 1 Then
    For Each wbk In Application.Workbooks
        If (Not wbk Is Application.ThisWorkbook) Then
            MsgBox "Opening additional workbook in the same instance is restricted while using Traderking - Trade Journal.", vbCritical, "Traderking - Trade Journal"
            wbk.Close SaveChanges:=False
        End If
    Next
End If

VBA Code:
Sub New_Instance_of_Excel()
    Dim objXL
    Set objXL = CreateObject("Excel.Application")
    objXL.Visible = True
    objXL.Workbooks.Add
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Private Sub Workbook_Open()
If Application.Workbooks.Count > 1 Then
Call Shell(Application.Path & Application.PathSeparator & "excel.exe " & Chr(34) & ThisWorkbook.FullName & Chr(34))
ThisWorkbook.Close SaveChanges:=False
End If
end Sub
 
Upvote 0
Thank you for the quick response.

It appears this
Private Sub Workbook_Open()
If Application.Workbooks.Count > 1 Then
Call Shell(Application.Path & Application.PathSeparator & "excel.exe " & Chr(34) & ThisWorkbook.FullName & Chr(34))
ThisWorkbook.Close SaveChanges:=False
End If
end Sub

The code you provided is an open event procedure, but I would like this for the workbook deactivate event. I did place it in the deactivate event, but it says it can not find the workbook if I'm just starting a blank workbook (a non-existent file).

VBA Code:
If Workbooks.Count > 1 Then
    For Each wbk In Application.Workbooks
        If (Not wbk Is Application.ThisWorkbook) Then
            Call Shell(Application.Path & Application.PathSeparator & "excel.exe " & Chr(34) & ActiveWorkbook.FullName & Chr(34))
        End If
    Next
End If
 
Upvote 0
Once more.
In case if user wants to open second workbook or create new workbook, this possibility have to be block and new excel instance have to be open?
 
Upvote 0
WIth my current workbook open,

1. if the user navigates to a exiting file, that existing file should open in its own instance.
2. if the user opens a new worksheet/workbook, this new worksheet/workbook should open in its own instance.
 
Upvote 0
Processing like this I hope the Excel computer has at least 16 Go RAM, even more !​
 
Upvote 0
VBA Code:
Private Sub Workbook_Deactivate()
Dim tmpName As String
For Each wbk In Application.Workbooks
        If (Not wbk Is Application.ThisWorkbook) Then
            MsgBox "Opening additional workbook in the same instance is restricted while using Traderking - Trade Journal.", vbCritical, "Traderking - Trade Journal"
            tmpName = wbk.FullName
            wbk.Close SaveChanges:=False
            If InStr(1, tmpName, ".xl") Then     ' quick test that new workbook is already saved and open as second or new workbook
                Call Shell(Application.Path & Application.PathSeparator & "excel.exe " & Chr(34) & tmpName & Chr(34))   ' reopen existed in new instance
            Else
                Call Shell("excel.exe")    'create new instance
            End If
        End If
Next
End Sub
 
Upvote 0
Hi Kokosek,

As the code runs through, it closes the new workbook, but it reopens the workbook in the same instance again. Also, it does this continuously. I have to ctrl-break the code to stop it from running.
 
Upvote 0
I do not know what version of excel you are using. On 365 works fine (small tweak):
Code:
Private Sub Workbook_Deactivate()
Dim tmpName As String
For Each wbk In Application.Workbooks
        If (Not wbk Is Application.ThisWorkbook) Then
            MsgBox "You can't open other workbook in the same instance. New instance will be created.", vbCritical, "Second workbook open"
            tmpName = wbk.FullName
            wbk.Close SaveChanges:=False
            If InStr(1, tmpName, ".xl") Then     ' quick test that new workbook is already saved and open as second or new workbook
                Call Shell(Application.Path & Application.PathSeparator & "excel.exe " & Chr(34) & tmpName & Chr(34))   ' reopen existed in new instance
            Else
                Call Shell(Application.Path & Application.PathSeparator & "excel.exe ")    'create new instance
            End If
        End If
Next
End Sub

If I press Ctrl+N (or choose New from File menu) I've got message, new workbook is closed and new instance of excel is created with new workbook.
The same if I open existed workbook.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top