Let me start with a brief description of what my project accomplishes currently. I have 3 subroutines in 'ThisOutlookSession'. One checks the last ~30 unread mail items and sends the subject to a sub that checks it's contents for a key word, the other does the same thing but handling the Items_ItemAdd event (new incoming mail) and the last sub I alluded to already checks the subject line and if a keyword is found calls a module I named 'ExcelConnection' which is where the issue stems from.
In the 'ExcelConnection' module I have this code that opens a workbook:
Dim oXL As Object
Dim oWS As Object
Dim lngRow As Long
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Open FileName:="T:\Capstone Proj\TimeStampsOnly.xlsx", AddTOMRU:=False, UpdateLinks:=False
'// Change sheet name to suit
Set oWS = oXL.Sheets("TimeStamps")
The issue is: This process takes about a minute or so to complete and then it goes through an ExitSave point where the workbook is saved, closed, and the application 'quits', BUT if another piece of mail comes in before it finished running and "ExitSaving" it gives an error saying I cant open the workbook since it's already open. This also stops the initial instance and the result is the workbook stays open in the background where I can't close it manually and I cant edit it either since it keeps saying it's being modified by 'Another User' (Outlook).
Is there any way to tell the macro to wait until the everything is done running before it runs again? This only happens when two pieces of mail with keywords come in within a minute or so of each other.
If you have any questions or need more code samples please let me know! Thank you.
Edit: This is the code for the 'ExcelConnection' module that is triggered by a keyword in subject line of email.
Public Sub ExcelConnect(msg As Outlook.MailItem, LType As String)
'// Declare all variables needed for excel functionality and open appropriate document
Dim oXL As Object
Dim oWS As Object
Dim lngRow As Long
Set oXL = CreateObject("Excel.Application")
oXL.Workbooks.Open FileName:="T:\Capstone Proj\TimeStampsOnly.xlsx", AddTOMRU:=False, UpdateLinks:=False
'// Change sheet name to suit
Set oWS = oXL.Sheets("TimeStamps")
lngRow = oWS.Range("A" & oXL.Rows.Count).End(-4162).Offset(1).Row '// -4162 = xlUp. not available late bound
Dim subArray() As String
Dim jRow As Long
Dim jobnum As Variant
subArray = Split(msg.Subject, "-", 2) '// Need the hypen to end the standardized subject line
jobnum = Trim(Right(subArray(0), 8))
jRow = IsExist(jobnum, lngRow, oWS)
Select Case LType '// Choose actions based on proccess step
Case "MDIQE"
If oWS.cells(jRow, 3).Value <> 0 Then
GoTo ExitSave
Else
With oWS
.cells(jRow, 1).Value = jobnum
.cells(jRow, 2).Value = msg.ReceivedTime
.cells(jRow, 3).Value = msg.ReceivedTime
End With
End If
'-------------------------------------------------------------
Case "MDIQ"
If oWS.cells(jRow, 2).Value <> 0 Then
GoTo ExitSave
Else
With oWS
.cells(jRow, 1).Value = jobnum
.cells(jRow, 2).Value = msg.ReceivedTime
End With
End If
'-------------------------------------------------------------
Case "MDIE"
If oWS.cells(jRow, 3).Value <> 0 Then
GoTo ExitSave
Else
With oWS
.cells(jRow, 1).Value = jobnum
.cells(jRow, 3).Value = msg.ReceivedTime
End With
End If
'-------------------------------------------------------------
Case "MDIR"
If oWS.cells(jRow, 4).Value <> 0 Then
GoTo ExitSave
Else
With oWS
.cells(jRow, 1).Value = jobnum
.cells(jRow, 4).Value = msg.ReceivedTime
End With
End If
'-------------------------------------------------------------
Case "MDIP"
If oWS.cells(jRow, 5).Value <> 0 Then
GoTo ExitSave
Else
With oWS
.cells(jRow, 1).Value = jobnum
.cells(jRow, 5).Value = msg.ReceivedTime
End With
End If
'-------------------------------------------------------------
Case "MDIF"
If oWS.cells(jRow, 6).Value <> 0 Then
GoTo ExitSave
Else
With oWS
.cells(jRow, 1).Value = jobnum
.cells(jRow, 6).Value = msg.ReceivedTime
End With
End If
End Select
ExitSave:
With oXL
.activeworkbook.Save
.activeworkbook.Close SaveChanges:=1 '// 2 = xlDoNotSaveChanges but not availabe late bound
.Application.Quit
End With
Set oXL = Nothing
Set oWS = Nothing
End Sub
Function IsExist(jobnum As Variant, upper As Long, oWS As Object) As Long
Dim i As Integer, ValueToFind As Variant
ValueToFind = jobnum
For i = (upper - 1) To 1 Step -1
If CStr(oWS.cells(i, 1).Value) = ValueToFind Then
IsExist = i
Exit Function
End If
Next i
IsExist = upper 'If found nothing
End Function
Thanks for all the help everyone.