2011-07-04 116 views
2

我试图使用VBA在宏中自动通过电子邮件发送报告。该报告由Outlook2007从Access2007发送。在发送报告时,我从Outlook收到一条安全消息,说“某个程序试图访问您的地址簿或联系人”或“某个程序尝试访问您存储在Outlook中的电子邮件地址...”。此消息对我来说是个问题,因为我想使用Windows任务计划程序自动发送报告而无需任何人为交互。所以我想禁用此安全通知。我在谷歌搜索,这是迄今为止的代码,但给我错误,我不知道我应该做什么。感谢您的帮助提前。我是一名初学者程序员。错误是使用VBA禁用Outlook安全设置

Public Sub Send_Report() 
Dim strRecipient As String 
Dim strSubject As String 
Dim strMessageBody As String 
Dim outlookapp As Outlook.Application 

Set outlookapp = CreateObject("Outlook.Application") 

OlSecurityManager.ConnectTo outlookapp 'error is here says object required 

OlSecurityManager.DisableOOMWarnings = True 
On Error GoTo Finally 

strRecipient = "[email protected]" 
strSubject = "Tile of report" 
strMessageBody = "Here is the message." 

DoCmd.SendObject acSendReport, "Report_Name", acFormatPDF, strRecipient, , ,  strSubject, strMessageBody, False 

Finally: 
OlSecurityManager.DisableOOMWarnings = False 


End Sub 

回答

2

您收到错误,因为OlSecurityManager什么都不是。你还没有宣布它,你没有设置它,所以当你尝试使用它时,VBA不知道你在说什么!

它看起来像你试图使用Outlook安全管理器,这是一个加载项销售here。你买了吗?因为如果没有,那么你可能没有在你的系统上。

如果你拥有它,那么你可能需要声明,并将其设置是这样的:

Dim OlSecurityManager As AddinExpress.Outlook.SecurityManager 
Set OlSecurityManager = New AddinExpress.Outlook.SecurityManager 

如果您作为我怀疑,没有它,那么另一种方法是发送电子邮件使用CDO。这里有一个例子:

首先,在工具>参考>微软CDO的Windows库或类似的东西旁边的复选标记设置CDO库的引用。

Dim cdoConfig 
Dim msgOne 

Set cdoConfig = CreateObject("CDO.Configuration") 
With cdoConfig.Fields 
    .Item(cdoSendUsingMethod) = cdoSendUsingPort 
    .Item(cdoSMTPServerPort) = 25 'your port number, usually is 25 
    .Item(cdoSMTPServer) = "yourSMTPserver.com" 
    '.Item(cdoSendUserName) = "your username if required" 
    '.Item(cdoSendPassword) = "your password if required" 
    .Update 
End With 

Set msgOne = CreateObject("CDO.Message") 
With msgOne 
    Set .Configuration = cdoConfig 
    .To = "[email protected]" 
    .from = "[email protected]" 
    .subject = "Testing CDO" 
    .TextBody = "It works just fine." 
    .Attachments.Add "C:\myfile.pdf" 
    .Send 
End With 

这比Outlook有点烦人,因为您需要事先知道要使用的SMTP服务器的地址。

+0

你是对的,我没有Outlook安全管理插件,我会用你的第二个方法,并更新你我所得到 – guest1

+0

你好。让,我没有看到将您的访问报告附加为PDF格式的观点ond代码。它是否允许发送附件?非常感谢! – guest1

+0

感谢让您的快速回复!实际上,附件是驻留在Access数据库中的报告。它不从本地驱动器附加文件。为了更清楚地说明,我在Access中有一个数据库,它有一个关于它的报告。我试图直接从Access中自动发送此报告并将其作为附件发送出去。那有意义吗? – guest1

0

我知道这是一个迟到的答案,但我碰到类似的问题。还有另一种解决方案,使用Outlook.Application

我偶然发现了它,而寻找解决方案,完整的信用在这里: http://www.tek-tips.com/faqs.cfm?fid=4334

但这个网站的解决方案只是建议,而不是使用.send命令,使用'。显示”命令,然后发送一些从键盘的键发送电子邮件,如下图所示:

Sub Mail_workbook_Outlook() 
'Working in Excel 2000-2016 
    Dim OutApp As Object 
    Dim OutMail As Object 

    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 

    With OutMail 
     .To = "[email protected]" 
     .CC = "" 
     .BCC = "" 
     .Subject = "This is an automated email!" 
     .Body = "Howdy there! Here, have an automated mail!" 
     .Attachments.Add ActiveWorkbook.FullName 
     .Display 'Display instead of .send 
     SendKeys "%{s}", True 'send the email without prompts 
    End With 
    On Error GoTo 0 

     Set OutMail = Nothing 
     Set OutApp = Nothing 
    End 
End Sub 
相关问题