Excel VBA:停止在Outlook电子邮件创建子例程中工作

时间:2015-01-30 16:46:54

标签: excel vba excel-vba outlook subroutine

前几天我问过这个问题并且认为我找到了答案,但现在Excel继续进入“无响应”模式。我的问题是该程序在某些计算机上运行良好,但在其他计算机上爆炸。

背景 用户填写Excel表单并单击“提交”后,该程序即可运行。该程序验证数据,操纵数据,通过Outlook创建电子邮件,最后保存和关闭文档。

该程序在许多同事的计算机上运行。我们都在Windows 7,Office 2013上运行,但我们中的一些人有不同的笔记本电脑(惠普,戴尔等)

这是对子程序的调用,这是excel开始“未响应”的代码行 - 仅供参考,这是 NOT 调试停止:

Call EmailApprovalP1(GeneralSection, ItemSection, VendorSection, MarketSection, Part1Section, wbLog, wbPCD, resubmission)

以下是调用子程序的开始:

Public Sub EmailApprovalP1(ByRef GeneralSection As Object, ByRef ItemSection As Object, ByRef VendorSection As Object, ByRef MarketSection As Object, ByRef Part1Section As Object, ByRef wbLog As Workbook, ByRef wbPCD As Workbook, ByRef resubmission As Boolean)

'Declare In Memory
Dim i As Integer
Dim toEmail As String
Dim subject As String
Dim body As String
Dim OL As New Outlook.Application
Dim olMail As Outlook.MailItem
Set olMail = OL.CreateItem(olMailItem)    

如果我停止Call EmailApprovalP1()上的代码,请按F8,Excel停止响应。有没有人对可能造成这种情况有什么想法?

更新2-3-15

所有计算机上的代码

Dim OL As Object
Set OL = CreateObject("Outlook.Application")
Dim olMail As Object
Set olMail = OL.CreateItem(olMailItem)

3 个答案:

答案 0 :(得分:1)

使用CreateObject代替New。已知New会导致一些不稳定问题。

Dim OL As Outlook.Application
Set OL = CreateObject("Outlook.Application")

答案 1 :(得分:1)

首先,确保在运行代码之前添加了对Outlook的引用。您需要在必须运行代码的每台PC上执行此操作。 How to automate Outlook from another program文章介绍了从其他应用程序自动化Outlook所需的所有步骤。

如果它没有帮助,请尝试单步执行(F8)函数并找到导致问题的确切代码行。

答案 2 :(得分:0)

在内存中声明为对象而不是 Outlook.Application ,这有所不同。谢谢你指点我正确的方向@Eugene& @Jeanno!

以下是有效的代码:

Dim OL As Object
Set OL = CreateObject("Outlook.Application")
Dim olMail As Object
Set olMail = OL.CreateItem(olMailItem)