我在 Outlook VBA 中编写了一个程序,它根据 Excel 电子表格的内容创建一些电子邮件。一切正常,但是当程序终止时,我继续运行“EXCEL.EXE”进程,该进程会锁定电子表格,因此其他人无法打开它。
在代码中,我有三个 Excel 对象:
Dim xl As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlsheet As Excel.Worksheet
最后,我关闭工作簿并将所有变量设置为 Nothing:
xlwb.Close
Set xlsheet = Nothing
Set xlwb = Nothing
Set xl = Nothing
我错过了什么?
编辑:
这是代码的基本部分,包括新的“退出”行:
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
最佳答案
您需要退出应用程序xl.Quit
Set "" = Nothing
真的没有必要
关于excel - 锁定 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33777031/