I've written a program in Outlook VBA which creates emails dependent upon the contents of an Excel spreadsheet.
When the program terminates I continue to have an "EXCEL.EXE" process running which locks the spreadsheet so no-one else can open it.
Within the code I have three Excel objects:
Dim xl As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlsheet As Excel.Worksheet
At the end I close the workbook and set all of the variables to Nothing:
xlwb.Close
Set xlsheet = Nothing
Set xlwb = Nothing
Set xl = Nothing
This is the bare bones of the code including the new "Quit" line:
Dim xl As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim ol As Outlook.Application
Dim Mail As MailItem
Set xl = Excel.Application
Set ol = Outlook.Application
Set xlwb = xl.Workbooks.Open("C:\sheet.xlsx", ReadOnly)
For Each xlsheet In xlwb.Worksheets
for xlrow = 1 to 5
If xlsheet.Cells(xlRow, 1).Value = "John" Then
msg=msg & xlsheet.Cells(xlRow, 2).Value
end if
next
next
Set Mail = ol.CreateItem(olMailItem)
Mail.To = "A@b.c"
Mail.Subject = "John's email"
Mail.Body = msg
Mail.Send
xlwb.Close
xl.Quit
Set ol = Nothing
Set xlsheet = Nothing
Set xlwb = Nothing
Set xl = Nothing