DK. I came to the same conclusion.
As far as monitoring events in both applications? It's a certainty as far as I know.
Simply changing this declaration:
Private pAuxiliaryInstance As Application
To this:
Private WithEvents pAuxiliaryInstance As Application
However, there is no need for it in this workbook. You can also assign a latebound reference to an an early bound variable declared withevents. I have found this useful at times. Thanks for the tips. The usercontrol property was also a new one for me...
DK_MyLonelyWorkbook2.zip
Jaafar,
Unless I am missing something here, I think the above line would only set the AuxiliaryInstance Property of the ReadOnly version Not that of the actual xlWb!
This is what we want. A reference to the current application hosting the readonly version sent to the newly created application which will now be hosting the non readonly version. pParentApp hold a reference to the actual parent application. The non readonly version will then have a reference to the first instance to refer to in the case of a user trying to open or create any workbooks in the new instance.
Only problem I have noticed is that more than 1 XL instance is created for new workbooks.
There should only be two instances. The instance hosting orphan.xls and the instance hosting all other workbooks...
<table border="1" bgcolor="White"><caption ALIGN=left>
<font size="2" face=Courier New>Example VBA Code:</FONT></caption><tr><td><font size="2" face=Courier New> <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> SetForegroundWindow <font color="#0000A0">Lib</font> "user32" _
(ByVal hWnd <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> FindWindow <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "FindWindowA" _
(ByVal lpClassName <font color="#0000A0">As</font> String, <font color="#0000A0">ByVal</font> lpWindowName <font color="#0000A0">As</font> String) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
<font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> pParentApp <font color="#0000A0">As</font> Application
<font color="#0000A0">Private</font> pAuxiliaryInstance <font color="#0000A0">As</font> Application
<font color="#0000A0">Private</font> pPersonalXls <font color="#0000A0">As</font> <font color="#0000A0">String</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_Open()
<font color="#0000A0">If</font> ThisWorkbook.ReadOnly <font color="#0000A0">Then</font> ThisWorkbook.Close False
SetUpOrpan
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> SetUpOrpan()
<font color="#0000A0">Dim</font> wb <font color="#0000A0">As</font> Workbook
<font color="#008000"> 'get an instance to "PERSONAL.XLS" if it is loaded</font>
<font color="#0000A0">On</font> <font color="#0000A0">Error</font> <font color="#0000A0">Resume</font> <font color="#0000A0">Next</font>
<font color="#0000A0">Set</font> wb = Workbooks("PERSONAL.XLS")
<font color="#0000A0">On</font> <font color="#0000A0">Error</font> <font color="#0000A0">GoTo</font> 0
<font color="#0000A0">If</font> Workbooks.Count > 1 <font color="#0000A0">And</font> wb <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
<font color="#008000"> 'PERSONAL.XLS not loaded</font>
<font color="#008000"> 'other workbooks are already open in this instance</font>
<font color="#008000"> 'open ThisWorkbook in a new instance of Excel.Application</font>
CloseMeAndStartMeInNewInstance
<font color="#0000A0">ElseIf</font> Workbooks.Count > 2 <font color="#0000A0">Then</font>
<font color="#008000"> 'PERSONAL.XLS may or may not be loaded. Does not matter at this point</font>
<font color="#008000"> 'other workbooks are already open in this instance</font>
<font color="#008000"> 'open ThisWorkbook in a new instance of Excel.Application</font>
CloseMeAndStartMeInNewInstance
<font color="#0000A0">Else</font>
<font color="#008000"> 'this instance is ok to open ThisWorkbook</font>
<font color="#008000"> 'close PERSONAL.XLS in this instance</font>
<font color="#0000A0">If</font> <font color="#0000A0">Not</font> wb <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
pPersonalXls = wb.FullName
wb.Close
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#008000"> 'continue opening in this instance</font>
<font color="#008000"> 'start watching the application level events to</font>
<font color="#008000"> 'respond to any attempts to create a new workbook</font>
<font color="#008000"> 'or to open an existing workbook</font>
<font color="#0000A0">Set</font> pParentApp = Application
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> SetAuxiliaryInstance(App <font color="#0000A0">As</font> Excel.Application)
<font color="#0000A0">If</font> pAuxiliaryInstance <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
<font color="#0000A0">Set</font> pAuxiliaryInstance = App
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_BeforeClose(Cancel <font color="#0000A0">As</font> Boolean)
<font color="#0000A0">If</font> <font color="#0000A0">Not</font> pAuxiliaryInstance <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
<font color="#008000"> 'if the user closes the Auxiliary Instance but we still have a reference</font>
<font color="#008000"> 'to the application, we will have an orphaned proccess running invisibly</font>
<font color="#008000"> 'this will close it</font>
<font color="#0000A0">If</font> <font color="#0000A0">Not</font> pAuxiliaryInstance.Visible <font color="#0000A0">Then</font>
pAuxiliaryInstance.Quit
<font color="#0000A0">Set</font> pAuxiliaryInstance = <font color="#0000A0">Nothing</font>
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> pParentApp_NewWorkbook(ByVal wb <font color="#0000A0">As</font> Workbook)
<font color="#008000"> 'close the newly created workbook and open it in the Auxiliary Instance</font>
wb.Close False
CreateNewOrGetExistingInstance
pAuxiliaryInstance.Workbooks.Add
SetForegroundWindow FindWindow("XLMAIN", pAuxiliaryInstance.Caption)
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> pParentApp_WorkbookOpen(ByVal wb <font color="#0000A0">As</font> Workbook)
<font color="#008000"> 'close the newly opened workbook and re-open it in the Auxiliary Instance</font>
<font color="#0000A0">Dim</font> WorkbookFullName <font color="#0000A0">As</font> <font color="#0000A0">String</font>
<font color="#0000A0">If</font> wb.FullName = ThisWorkbook.FullName <font color="#0000A0">Then</font> <font color="#0000A0">Exit</font> <font color="#0000A0">Sub</font>
WorkbookFullName = wb.FullName
wb.Close False
CreateNewOrGetExistingInstance
pAuxiliaryInstance.Workbooks.Open WorkbookFullName
SetForegroundWindow FindWindow("XLMAIN", pAuxiliaryInstance.Caption)
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> CreateNewOrGetExistingInstance()
<font color="#0000A0">If</font> pAuxiliaryInstance <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
<font color="#0000A0">Set</font> pAuxiliaryInstance = <font color="#0000A0">New</font> Application
<font color="#0000A0">If</font> pAuxiliaryInstance.Workbooks.Count = 0 <font color="#0000A0">And</font> pPersonalXls <> "" <font color="#0000A0">Then</font>
pAuxiliaryInstance.Workbooks.Open pPersonalXls
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
<font color="#008000"> 'always set the visiblility to True because the user may have closed</font>
<font color="#008000"> 'the application but it is still running invisibly if we have a reference to it</font>
pAuxiliaryInstance.Visible = True
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
<font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> CloseMeAndStartMeInNewInstance()
<font color="#0000A0">Dim</font> xlApp <font color="#0000A0">As</font> Excel.Application
<font color="#008000"> 'Will "transfer" this workbook to another instance of Excel</font>
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
<font color="#0000A0">If</font> ThisWorkbook.ReadOnly = False <font color="#0000A0">Then</font>
ThisWorkbook.ChangeFileAccess xlReadOnly
<font color="#0000A0">End</font> <font color="#0000A0">If</font>
Application.DisplayAlerts = True
<font color="#0000A0">Set</font> xlApp = <font color="#0000A0">New</font> Excel.Application
xlApp.Workbooks.Open ThisWorkbook.FullName
xlApp.UserControl = True
xlApp.Visible = True
xlApp.Run ThisWorkbook.Name & "!Thisworkbook.SetAuxiliaryInstance", Application
ThisWorkbook.Close savechanges:=False
<font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table>