Hi Guys,
I've been getting some help on this code from Stack Overflow but work doesn't let me acces that site. My code is as follows:
The bottom two subs are to activate the actual code. The issue I have is as follows.:
Runtime Error 429. This happens when there is no Excel windows open. I feel this is something to do with the Do While line.
Runtime error 91. This happens after all Excel windows have been closed. This appears to be closing Notepad but it isn't actually open (there is a notepad.exe process but no actual screen to see). Also, if I comment this line out the loop is endless. I think this is due to an excel.exe process still being open. Ulitmately, I want to kill all the excel processes but this code is what I want as the precursor so all workbooks can be reopened again later. I know I am close as I can get ti to all work within an Excel enviroment, I just can't work it out for Access.
Thanks guys.
I've been getting some help on this code from Stack Overflow but work doesn't let me acces that site. My code is as follows:
Code:
Public Sub CloseAllExcel(Close1 As Boolean)
'On Error GoTo handler
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim strPath As String
strQueryName = "Closed Excel"
strPathUser = Environ$("USERPROFILE") & "\Desktop\"
strPath = strPathUser & strQueryName & ".txt"
If Close1 Then
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile(strPath)
Do While xl Is Nothing
Set xl = GetObject(, "Excel.Application")
For Each wb In xl.Workbooks
oFile.WriteLine xl.ActiveWorkbook.FullName
wb.Save
wb.Close
Next
oFile.Close
Set fso = Nothing
Set oFile = Nothing
xl.Quit
Set xl = Nothing
Loop
Exit Sub
Else
Dim FileNum As Integer
Dim DataLine As String
FileNum = FreeFile()
Open strPath For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, DataLine
Workbooks.Open DataLine
Wend
Exit Sub
End If
'handler:
' If Err <> 429 Then 'ActiveX component can't create object
' MsgBox Err.Description, vbInformation
' End If
End Sub
Private Sub Toggle0_Click()
CloseAllExcel True
End Sub
Private Sub Toggle1_Click()
CloseAllExcel False
End Sub
The bottom two subs are to activate the actual code. The issue I have is as follows.:
Code:
Set xl = GetObject(, "Excel.Application")
Code:
ofile.close
Thanks guys.