Vba 如何为来自共享收件箱的新邮件触发outlook宏

Vba 如何为来自共享收件箱的新邮件触发outlook宏,vba,outlook,Vba,Outlook,此代码适用于普通收件箱,但如何更改代码以从共享邮箱触发确认(仅适用于新邮件,需要排除重新发送和转发到收件箱文件夹的邮件)(xxx@mail.com).文件夹(收件箱) 如何修改此代码以从特定共享邮箱“收件箱”触发 完整代码: Public WithEvents xlItems As Outlook.Items Private Sub Application_Startup() Set xlItems = Session.GetDefaultFolder(olFolderInbox

此代码适用于普通收件箱,但如何更改代码以从共享邮箱触发确认(仅适用于新邮件,需要排除重新发送和转发到收件箱文件夹的邮件)(xxx@mail.com).文件夹(收件箱)

如何修改此代码以从特定共享邮箱“收件箱”触发

完整代码:

Public WithEvents xlItems As Outlook.Items
    Private Sub Application_Startup()
    Set xlItems = Session.GetDefaultFolder(olFolderInbox).Items
    End Sub
    Private Sub xlItems_ItemAdd(ByVal objItem As Object)
    Dim xlReply As MailItem
    Dim xStr As String
    If objItem.Class <> olMail Then Exit Sub
    Set xlReply = objItem.Reply
    With xlReply
         xStr = "<p>" & "Hi Team, Acknowledging that we have received the Job. Thank you!" & "</p>"
         .HTMLBody = xStr & .HTMLBody
         .Send
    End With
End Sub
Public,以事件xlItems作为Outlook.Items
私有子应用程序_启动()
Set xlItems=Session.GetDefaultFolder(olFolderInbox).Items
端接头
私有子xlItems\u ItemAdd(ByVal objItem作为对象)
请以邮件形式回复
Dim xStr作为字符串
如果是objItem.Class olMail,则退出Sub
Set xlReply=objItem.Reply
答复如下
xStr=“”和“大家好,我们已经收到了这份工作。谢谢!”
.HTMLBody=xStr和.HTMLBody
.发送
以
端接头
我试图修改代码,但没有成功

Option Explicit
Private WithEvents olInboxItems As Items
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olInboxItems = objNS.Folders("xxxxxxxx@gmail.com").Folders("Inbox").Items
  Set objNS = Nothing
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim xlReply As MailItem
Dim xStr As String
If objItem.Class <> olMail Then Exit Sub
Set xlReply = objItem.Reply
With xlReply
     xStr = "<p>" & "Hi Team, Acknowledging that we have received the Job. Thank you!" & "</p>"
     .HTMLBody = xStr & .HTMLBody
     .Send
End Sub
选项显式
Private WithEvents和BoxItems作为项目
Dim OBJN作为名称空间
设置objNS=Application.Session
'实例化用事件声明的对象
设置olInboxItems=objNS.Folders(“xxxxxxxx@gmail.com“”。文件夹(“收件箱”)。项目
Set objNS=Nothing
端接头
私有子项\u ItemAdd(ByVal项作为对象)
请以邮件形式回复
Dim xStr作为字符串
如果是objItem.Class olMail,则退出Sub
Set xlReply=objItem.Reply
答复如下
xStr=“”和“大家好,我们已经收到了这份工作。谢谢!”
.HTMLBody=xStr和.HTMLBody
.发送
端接头

我终于自己算出了代码。但它会发送所有电子邮件,包括(RE和FWD)


这是原始/直观的版本。
主题必须保持不变,并使用英语

在本次展望会上

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Public WithEvents olItems As Items

Private Sub Application_Startup()

    Dim objNS As namespace
    
    Set objNS = GetNamespace("MAPI")
    Set olItems = objNS.Folders("xxxx@xxx.com").Folders("Inbox").Items
    
End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)

    Dim olReply As MailItem
 
    If Item.Class = olMail Then
        
        If Left(UCase(Item.Subject), 4) = UCase("Re: ") Or _
           Left(UCase(Item.Subject), 4) = UCase("Fw: ") Then
            Exit Sub
        
        Else
        
            Set olReply = Item.reply
    
            With olReply
                .Body = "Reply to first email."
                .Display
            End With
        
        End If
    
    End If
    
End Sub
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Public WithEvents olItems As Items

Private Sub Application_Startup()
    
    Set olItems = Session.Folders("xxxx@xxx.com").Folders("Inbox").Items
    
End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)

    Dim olReply As MailItem
 
    If Item.Class = olMail Then
        
        If Len(Item.ConversationIndex) > 44 Then
            Exit Sub
        
        Else
        
            Set olReply = Item.reply
    
            With olReply
                .Body = "Reply to first email."
                .Display
            End With
        
        End If
    
    End If
    
End Sub

这应该比检查主题中的“Re:”和“Fw:”更可靠

在本次展望会上

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Public WithEvents olItems As Items

Private Sub Application_Startup()

    Dim objNS As namespace
    
    Set objNS = GetNamespace("MAPI")
    Set olItems = objNS.Folders("xxxx@xxx.com").Folders("Inbox").Items
    
End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)

    Dim olReply As MailItem
 
    If Item.Class = olMail Then
        
        If Left(UCase(Item.Subject), 4) = UCase("Re: ") Or _
           Left(UCase(Item.Subject), 4) = UCase("Fw: ") Then
            Exit Sub
        
        Else
        
            Set olReply = Item.reply
    
            With olReply
                .Body = "Reply to first email."
                .Display
            End With
        
        End If
    
    End If
    
End Sub
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Public WithEvents olItems As Items

Private Sub Application_Startup()
    
    Set olItems = Session.Folders("xxxx@xxx.com").Folders("Inbox").Items
    
End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)

    Dim olReply As MailItem
 
    If Item.Class = olMail Then
        
        If Len(Item.ConversationIndex) > 44 Then
            Exit Sub
        
        Else
        
            Set olReply = Item.reply
    
            With olReply
                .Body = "Reply to first email."
                .Display
            End With
        
        End If
    
    End If
    
End Sub

发帖时请描述您看到的任何问题。在修改后的代码中,如果objItem.Class olMail Then不存在,则应在
上出现错误,因为
objItem
不存在。修改后的代码缺少
专用子应用程序\u Startup()
。从你的编辑器直接复制代码。我试过了,但我想不出代码是什么,请你修复这个。完整代码中提到的代码运行良好。当我在modifiedal下包含共享文件夹中的外观时,我遇到了一个问题。因此,如果我包含“Private Sub Application_Startup()”,它会触发应答邮件以及转发邮件。如何限制这些。感谢你的帮助,尼顿,谢谢。这段代码工作得很好。