使用 VBA 禁用 Outlook 安全设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6574463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable outlook security settings using VBA
提问by guest1
I am trying to auto email a report from access using VBA in a macro. The report is sent from Access2007 by outlook2007. When the report is being sent, I get a security message from outlook saying "a program is trying to access your Address book or Contacts" or "a program is trying to access e-mail addresses you have stored in Outlook..." . This message is a problematic for me because I want to use windows task scheduler to automatically send the report without any human interaction.So I want to disable this security notification. I searched on Google and here is the code I have so far but giving me errors and I am not sure what else I should do. Thanks for your help in advance. I am a beginner programmer. The error is
我正在尝试使用宏中的 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 = "[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
采纳答案by Jean-Fran?ois Corbett
You get the error because OlSecurityManager
is nothing. You haven't declared it, you haven't set it to anything, so when you attempt to use it, VBA has no idea what you're talking about!
你得到错误是因为OlSecurityManager
什么都没有。您还没有声明它,也没有将其设置为任何内容,因此当您尝试使用它时,VBA 不知道您在说什么!
It looks like you're trying to use Outlook Security Manager, which is an add-in sold here. Have you purchased it? Because if not, then you probably don't have it on your system.
看起来您正在尝试使用 Outlook 安全管理器,它是此处出售的加载项。你购买了吗?因为如果没有,那么您的系统上可能没有它。
If you do have it, then you probably need to declare and set it like this:
如果你有它,那么你可能需要像这样声明和设置它:
Dim OlSecurityManager As AddinExpress.Outlook.SecurityManager
Set OlSecurityManager = New AddinExpress.Outlook.SecurityManager
If you, as I suspect, don't have it, then an alternative is sending e-mail using CDO. Here's an example:
如果我怀疑您没有它,那么另一种方法是使用 CDO 发送电子邮件。下面是一个例子:
First, set a reference to the CDO library in Tools > References > checkmark next to Microsoft CDO for Windows Libraryor something like that.
首先,在Microsoft CDO for Windows Library旁边的 Tools > References > checkmark或类似内容中设置对 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
This is a bit more annoying than Outlook, because you need to know in advance the address of the SMTP server to be used.
这比Outlook更烦人,因为您需要提前知道要使用的SMTP服务器的地址。
回答by IHaveSumQuestions
I know this is a late answer, but I just ran into a similar problem. There is another solution using Outlook.Application
!
我知道这是一个迟到的答案,但我遇到了类似的问题。还有另一种解决方案使用Outlook.Application
!
I stumble upon it while looking for the solution, full credit here: http://www.tek-tips.com/faqs.cfm?fid=4334
我在寻找解决方案时偶然发现了它,完全归功于这里:http: //www.tek-tips.com/faqs.cfm?fid=4334
But what this site's solution simply suggest, instead of using the .send
command, use the `.Display" command and then send some keys from the keyboard to send the email, like below:
但是这个站点的解决方案只是简单地建议,不要使用.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 = "[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