【发布时间】:2020-06-26 06:41:26
【问题描述】:
我尝试编写一些 VBA 将附件文件从某些电子邮件保存到文件夹 但我得到了错误
运行时错误“424”
需要对象
这是我正在尝试使用的代码
Sub test_extraer()
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
If (Msg.SenderName = "sender@email.com") And _
(Msg.Subject = "subject of the email") 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
Const attPath As String = "C:\temp\"
Set myAttachments = item.Attachments
Att = myAttachments.item(1).DisplayName
myAttachments.item(1).SaveAsFile attPath & Att
End If
End Sub
脚本进入this if时触发错误
If (Msg.SenderName = "sender@email.com") And _
(Msg.Subject = "subject of the email") And _
(Msg.Attachments.Count >= 1) Then
任何建议
提前致谢
【问题讨论】:
-
为什么这个标签是visual studio?
-
Msg这是什么?你似乎少了一两步。 -
这段代码的问题让我很头疼。我的第一个有助于消除大部分错误的建议是在这个模块中使用
Option Explicit。这将阻止您尝试使用msg和item现在的未声明变量