|
-
Apr 22nd, 2003, 12:51 PM
#1
Thread Starter
New Member
Problem with saving code in thisworkbook
I have an application that runs an Excel workbook in background. When I start another excel file, the system open it in the same excel.exe. When I close the file, its also closing the one in the background. I found some vba code to avoid that (to open multiple excel.exe).... then I have put the code in the thisworkbook class and saved it.... but when I open my workbook again, the code has disapeared (I also added a reference to VBE Extensibility Library)...here's the code :
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnWindow = "": Application.IgnoreRemoteRequests = False
If Workbooks.Count = 1 Then Application.Quit Else ThisWorkbook.Close
End Sub
Private Sub Workbook_Open()
If AutoInstance Then NewInstance: Exit Sub
Application.OnWindow = ThisWorkbook.Name & "!ThisWbkOnly"
Application.DisplayAlerts = False
Application.IgnoreRemoteRequests = True
Application.DisplayAlerts = True
End Sub
Private Function AutoInstance() As Boolean
If Application.Workbooks.Count = 1 Then Exit Function
If Application.Workbooks.Count = 2 And Workbooks(1).Path = "" Then Exit Function
AutoInstance = True
End Function
Private Sub NewInstance()
Dim xlApp1 As Excel.Application, xlApp2 As Excel.Application
Dim Wbk1 As Workbook, Wbk1_Name As String
Dim vbProj1 As VBProject, vbComp1 As VBComponent
Dim newCode As String, nbLine As Long
newCode = newCode & "Dim xlInstance As Excel.Application" & vbCrLf
newCode = newCode & "Dim CreatorName As String" & vbCrLf
newCode = newCode & "Dim CreatorFullName As String" & vbCrLf & vbCrLf
newCode = newCode & "Sub SourceExcelInstance(xlObj As Object, wbkName As String, wbkFullName As String)" & vbCrLf
newCode = newCode & "Set xlInstance = xlObj" & vbCrLf
newCode = newCode & "CreatorName = wbkName" & vbCrLf
newCode = newCode & "CreatorFullName = wbkFullName" & vbCrLf
newCode = newCode & "End Sub" & vbCrLf & vbCrLf
newCode = newCode & "Sub ReOpenFile()" & vbCrLf
newCode = newCode & "xlInstance.Workbooks(CreatorName).Close SaveChanges:=False" & vbCrLf
newCode = newCode & "Set xlInstance = Nothing" & vbCrLf
newCode = newCode & "Workbooks.Open CreatorFullName" & vbCrLf
newCode = newCode & "ThisWorkbook.Close False" & vbCrLf
newCode = newCode & "End Sub" & vbCrLf
Set xlApp1 = Application
Set xlApp2 = CreateObject("Excel.Application")
With xlApp2
.Visible = True
Set Wbk1 = .Workbooks.Add
Wbk1_Name = Wbk1.Name
Set vbProj1 = Wbk1.VBProject
Set vbComp1 = vbProj1.VBComponents.Add(vbext_ct_StdModule)
nbLine = vbComp1.CodeModule.CountOfLines + 1
vbComp1.CodeModule.InsertLines nbLine, newCode
Set vbComp1 = Nothing: Set vbProj1 = Nothing:: Set Wbk1 = Nothing
.Run "'" & Wbk1_Name & "'!SourceExcelInstance", xlApp1, ThisWorkbook.Name, ThisWorkbook.FullName
Set xlApp1 = Nothing
.OnTime Now + TimeValue("00:00:01"), Wbk1_Name & "!ReOpenFile"
End With
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|