【发布时间】:2018-11-09 10:49:50
【问题描述】:
我的 VBA 技能非常有限,但到目前为止我想完成这个项目。
下面的 VBA 代码在我的 Outlook 中运行良好。它将所需的电子邮件保存到我的驱动器中。
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo ErrorHandler
'Only act if it's a MailItem
Dim Msg As Outlook.MailItem
If TypeName(item) = "MailItem" Then
Set Msg = item
'Change variables to match need. Comment or delete any part unnecessary.
If (Msg.SenderEmailAddress = "noreply@test.com") Or _
(Msg.Subject = "Smartsheet") Or _
(Msg.Subject = "Defects") And _
(Msg.Attachments.Count >= 1) Then
'Set folder to save in.
Dim olDestFldr As Outlook.MAPIFolder
Dim myAttachments As Outlook.Attachments
Dim Att As String
'location to save in. Can be root drive or mapped network drive.
Const attPath As String = "C:\"
' save attachment
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
' mark as read
Msg.UnRead = False
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.number & " - " & Err.Description
Resume ProgramExit
End Sub
我现在想将代码添加到保存附件后移动电子邮件到我的测试文件夹。测试文件夹在我的 Outlook 收件箱下。
我添加了
Set FldrDest = Session.Folders("Address1").Folders("Inbox").Folders("Test")
在 Private Sub Application_Startup() 下,然后我将代码添加到我的 VBA 中。
代码在'标记为已读之后
If .Parent.Name = "Test" And .Parent.Parent.Name = "Inbox" Then
' MailItem is already in destination folder
Else
.Move FldrDest
End If
没有其他变化,但它给了我编译错误。
【问题讨论】: