【问题标题】:Disable outlook security settings using VBA使用 VBA 禁用 Outlook 安全设置
【发布时间】:2011-07-04 17:30:44
【问题描述】:

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

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 = "example@yahoo.com"
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

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    您收到错误是因为OlSecurityManager 什么都不是。你没有声明它,你没有设置任何东西,所以当你尝试使用它时,VBA 不知道你在说什么!

    您似乎正在尝试使用 Outlook 安全管理器,它是here 出售的加载项。你买了吗?因为如果没有,那么您的系统上可能没有它。

    如果你有它,那么你可能需要像这样声明和设置它:

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

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

    首先,在 Microsoft CDO for Windows Library 旁边的工具 > 参考 > 复选标记中设置对 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 = "recipient@somehwere.com"
        .from = "you@here.com"
        .subject = "Testing CDO"
        .TextBody = "It works just fine."
        .Attachments.Add "C:\myfile.pdf"
        .Send
    End With
    

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

    【讨论】:

    • 你是对的!我没有 Outlook 安全管理器加载项。我将使用您的第二种方法并更新您的信息。
    • 嗨,Jean,我没有在您的第二个代码中看到将访问报告作为 PDF 附加的论点。它也允许发送附件吗?非常感谢!
    • 感谢 Jean 的快速回复!实际上,附件是一份驻留在 Access 数据库中的报告。它没有从本地驱动器附加文件。为了更清楚,我在 Access 中有一个数据库,它有一个报告。我正在尝试直接从 Access 自动通过电子邮件发送此报告并将其作为附件发送出去。这有意义吗?
    • 请将此作为新问题发布。当前问题的主题是“使用 VBA 禁用 Outlook 安全设置”。
    【解决方案2】:

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

    我在寻找解决方案时偶然发现了它,这里完全归功于: http://www.tek-tips.com/faqs.cfm?fid=4334

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

    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 = "Someone@Somewhere.com"
            .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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2011-08-13
      相关资源
      最近更新 更多