Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 自动outlook回复_Excel_Vba_Automation_Outlook - Fatal编程技术网

Excel 自动outlook回复

Excel 自动outlook回复,excel,vba,automation,outlook,Excel,Vba,Automation,Outlook,我又在找指针了。 因此,我从这两篇文章中构建了以下代码 我几乎让它运行,但当我收到会触发自动响应的电子邮件时,代码正在创建新的自动生成电子邮件的多个实例, 有没有关于如何在创建第一个新邮件项后退出循环的指针?我开始理解所有这些是如何工作的,但循环是我的一个主要弱点 代码如下: Private WithEvents Items As Outlook.Items Private Sub Application_Startup() Dim olApp As Outlook.Application D

我又在找指针了。 因此,我从这两篇文章中构建了以下代码

我几乎让它运行,但当我收到会触发自动响应的电子邮件时,代码正在创建新的自动生成电子邮件的多个实例, 有没有关于如何在创建第一个新邮件项后退出循环的指针?我开始理解所有这些是如何工作的,但循环是我的一个主要弱点

代码如下:

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim Fldr As MAPIFolder 'Dim Fldr As MAPIFolder
Dim olMail As Variant 'Dim olMail As Variant
Dim SigString As String
Dim Signature As String
Dim i As Integer

Set olApp = Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Items = olNs.GetDefaultFolder(olFolderInbox).Items
'Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim Fldr As MAPIFolder 'Dim Fldr As MAPIFolder
Dim olMail As Variant 'Dim olMail As Variant
Dim SigString As String
Dim Signature As String
Dim i As Integer

On Error GoTo ErrorHandler
  Dim Msg As Outlook.MailItem
  If TypeName(item) = "MailItem" Then
    Set Msg = item

i = 1
sDate = Date
sWeeknum = Excel.Application.WorksheetFunction.WeekNum(sDate) - 2


   strBody = "<BODY style='font-familiy:Calibri;font-size:11pt'>Dear Valued Customer,<br><br>Text text.<br>" & vbCrLf & vbCrLf
  strBody2 = "<BODY style='font-familiy:Calibri;font-size:11pt'>More Text.<br>" & vbCrLf & vbCrLf
  strBody3 = "<BODY style='font-familiy:Calibri;font-size:11pt'>Some more text" & vbCrLf & vbCrLf
  strBody4 = "<BODY style='font-familiy:Calibri;font-size:11pt'>Even More text.<br><br>" & vbCrLf & vbCrLf
  strBody5 = "<BODY style='font-familiy:Calibri;font-size:11pt'>DISCLAIMER" & vbCrLf & vbCrLf
  
For Each olMail In Outlook.Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
    If InStr(olMail.Subject, "Email Subject" & sWeeknum) <> 0 Then 'And olMail.SenderEmailAddress = "customer_email@example.com" Then (Customer email is commented out here since I'm still running tests and I'm sending myself the email to verify the autoreponse triggered)
        With olMail.Forward
         .To = "recipient1"
         .CC = "recipient2;recipient3"
         .Recipients.ResolveAll
         .HTMLBody = strBody & strBody2 & strBody3 & strBody4 & strBody5 & .HTMLBody & "</font>"
         .Display
         End With
        i = i + 1
    End If
    Next olMail
End If

ProgramExit:
  Exit Sub
  
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit

End Sub
Private,事件项作为Outlook.Items
私有子应用程序_启动()
Dim olApp作为Outlook.Application
将olNs设置为Outlook.NameSpace
Dim Fldr As MAPIFolder“Dim Fldr As MAPIFolder”
Dim olMail作为变体“Dim olMail作为变体”
字符串作为字符串
作为字符串的数字签名
作为整数的Dim i
设置olApp=Outlook.Application
Set olNs=olApp.GetNamespace(“MAPI”)
Set Items=olNs.GetDefaultFolder(olFolderInbox).Items
'Set Fldr=olNs.GetDefaultFolder(olFolderInbox)
端接头
私有子项\u ItemAdd(ByVal项作为对象)
Dim olApp作为Outlook.Application
将olNs设置为Outlook.NameSpace
Dim Fldr As MAPIFolder“Dim Fldr As MAPIFolder”
Dim olMail作为变体“Dim olMail作为变体”
字符串作为字符串
作为字符串的数字签名
作为整数的Dim i
关于错误转到错误处理程序
将消息作为Outlook.mailtim
如果TypeName(item)=“MailItem”,则
设置Msg=item
i=1
sDate=日期
sWeeknum=Excel.Application.WorksheetFunction.WeekNum(sDate)-2
strBody=“亲爱的尊贵客户,

文本。
”&vbCrLf&vbCrLf strBody2=“更多文本。
”&vbCrLf&vbCrLf strBody3=“更多文本”&vbCrLf&vbCrLf strBody4=“更多文本。

”&vbCrLf&vbCrLf strBody5=“免责声明”&vbCrLf&vbCrLf 对于Outlook.Application.GetNamespace(“MAPI”).GetDefaultFolder(olFolderInbox).Items中的每个olMail 如果InStr(olMail.Subject,“Email Subject”和sWeeknum)为0,则“和olMail.SenderEmailAddress=”客户_email@example.com“然后(客户电子邮件在这里被注释掉,因为我仍在运行测试,并且我正在向自己发送电子邮件以验证触发的自动回复) 用电子邮件,转发 .To=“收件人1” .CC=“收件人2;收件人3” .Recipients.ResolveAll .HTMLBody=strBody&strBody2&strBody3&strBody4&strBody5&HTMLBody&“ .展示 以 i=i+1 如果结束 下一封邮件 如果结束 程序退出: 出口接头 错误处理程序: MsgBox错误编号&“-”&错误说明 恢复程序退出 端接头

如果您的Sub已经获得了
项作为参数,您将非常感谢您对解决此问题的任何帮助,您只需查看一下即可。不需要在文件夹内容上循环。通常HTML只有一个
主体
元素。