excel - 锁定 Excel 电子表格

标签 excel vba outlook

我在 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/

相关文章:

java - Apache POI 创建 xlsx 文件,以后无法打开。检测到 zipper 炸弹

excel - 如何从 Access VBA 引用 Excel 用户窗体

excel - VBA Office 2007 创建 2003 Excel 工作表

vba - 在 ms-access 数据库中验证用户

excel - 比较 A 列和 B 列,并将两列中的项目复制到 C 列中

mysql - 如果日期格式不正确或空白,如何突出显示单元格?

java - Apache POI 的 getCachedFormulaResultType() 返回错误类型

c# - 作为流的 Outlook MailItem

excel - 如何根据标准转发电子邮件?

excel - 如何在 VBA 中根据我的文本创建表格