I have a program that runs vba from Excel. It opens up an excel spreadsheet and then opens up Extra. It then puts data in Extra on some screens and writes any error messages from Extra to the Excel Spreadsheet. Everything runs great and I get my message at the end. Click okay and then I go to physically close the workbook and Excel crashes. I found the culprit but I don't know what to do. It seems any of the WaitFor Commands does this. If I use anything but Waitfor's in VBA, does fine. Close excel, no problem. Does anyone know what I can do?
Code:
Public Sub Testing()
Dim strmessage
Set System = New ExtraSystem
' Declare variables to contain the OLE objects
Dim objExcel As Object
Dim objWorkBook As Object
On Error Resume Next
' Attempt to get a reference to an open instance of Excel
Set objExcel = GetObject(, "Excel.Application")
If objExcel Is Nothing Then
'If GetObject failed, open a new instance of Excel
Set objExcel = CreateObject("Excel.Application")
If objExcel Is Nothing Then
MsgBox ("Could not open Excel.")
Exit Sub
End If
End If
' Make Excel visible on the screen
objExcel.Visible = True
' Create a new Workbook
Set objWorkBook = objExcel.Workbooks.Open("C:\Allotment Process\Allotment Entries.xls")
If objWorkBook Is Nothing Then
MsgBox ("Could not open a new Excel workbook.")
objExcel.Quit
Exit Sub
End If
'Select the sheet
objWorkBook.Worksheets("TR20").Select
'Get Extra Object
If (System Is Nothing) Then
MsgBox "Could not create the EXTRA System object. Stopping macro playback."
Stop
End If
'Establish a Session of Extra
Set Sessions = System.Sessions
If (Sessions Is Nothing) Then
MsgBox "Could not create the Sessions collection object. Stopping macro playback."
Stop
End If
'Open Extra
Set Sess0 = System.Sessions.Open("FLAIR.EDP")
If Not Sess0.Visible Then
Sess0.Visible = True
End If
Dim move
Dim move2
Dim strL2L5 As String
Dim fieldtest
Dim result
Dim result2
Dim x2
Dim strrange
'Login process
Set MyScn = Sess0.Screen
'!!!!!This is the culprit below!!!!!
Sess0.Screen.WaitForCursorMove (3)
Sess0.Close
objExcel.ActiveWorkbook.Close
Set MyScn = Nothing
Set Sess0 = Nothing
strmessage = MsgBox("TR21 Input Complete. Please click okay and check your work.", vbOKOnly)
End Sub