明确控制前景

时间:2015-11-20 07:17:28

标签: excel vba excel-vba outlook outlook-vba

我正在尝试编写一个简单的程序来自动从excel中的列表发送电子邮件,并且它可以正常工作,但Outlook会不断打开要求权限的弹出窗口。你如何获得不再要求许可的前景,只是在没有弹出窗口的情况下做excel告诉它的事情

这是我到目前为止的代码:

Sub SendMessage()

      Dim objOutlook As Outlook.Application
      Dim objOutlookMsg As Outlook.MailItem
      Dim objOutlookRecip As Outlook.Recipient
      Dim objOutlookAttach As Outlook.Attachment
      Dim recemail
      Dim i As Integer

      i = 1

      recemail = Sheet1.Cells(i, 1)

      ' Create the Outlook session.
      Set objOutlook = CreateObject("Outlook.Application")

      ' Create the message.
      Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

      With objOutlookMsg
          ' Add the To recipient(s) to the message.
          Set objOutlookRecip = .Recipients.Add(recemail)
          objOutlookRecip.Type = olTo

         ' Set the Subject, Body, and Importance of the message.
         .Subject = "TEST!"
         .Body = "DOES THIS WORK!?"

         ' Should we display the message before sending?
         If DisplayMsg Then
             .Display
         Else
             .Save
             .Send
         End If
      End With
      Set objOutlook = Nothing

      i = i + 1
  End Sub

1 个答案:

答案 0 :(得分:1)

这是您需要执行的手动操作:

  1. 以管理员身份运行Outlook
  2. 转到工具(Outlook 2007)或文件,选项(Outlook 2010及更高版本)
  3. 转到信托中心
  4. 将程序访问设置更改为:Never warn me about suspicious activity
  5. 您现在可以关闭Outlook,从现在开始,您每次都可以访问而无需弹出窗口!

    顺便说一句,为了避免打开一个新的Outlook实例(如果已经有),请使用:

      'Create or Get the Outlook session.
      On Error Resume Next
      Set objOutlook = GetObject(, "Outlook.Application")
      If Err.Number > 0 Then Set objOutlook = CreateObject("Outlook.Application")
      On Error GoTo 0
    
相关问题