Vba 嵌入附件时保存附件时出错
我正在保存Outlook附件(作为副本的一部分) 当附件是嵌入图像时,我从行Vba 嵌入附件时保存附件时出错,vba,outlook,email-attachments,Vba,Outlook,Email Attachments,我正在保存Outlook附件(作为副本的一部分) 当附件是嵌入图像时,我从行objAtt.SaveAsFile strFile中收到一条错误消息 代码(感谢复制!)是: 完整的错误消息是: 我不需要嵌入图像,因此跳过它们也可以。首先,确保文件路径是完全限定的,也就是说,您在这里得到了一个有效的字符串: strFile = strPath & objAtt.FileName 其次,当您调用附件时,请确保磁盘上存在该文件。附件的源可以是一个文件(由带有文件名的完整文件系统路径表示)或构成
objAtt.SaveAsFile strFile
中收到一条错误消息
代码(感谢复制!)是:
完整的错误消息是:
我不需要嵌入图像,因此跳过它们也可以。首先,确保文件路径是完全限定的,也就是说,您在这里得到了一个有效的字符串:
strFile = strPath & objAtt.FileName
其次,当您调用附件时,请确保磁盘上存在该文件。附件的源可以是一个文件(由带有文件名的完整文件系统路径表示)或构成附件的Outlook项目
您可以尝试运行以下代码,将附件保存在磁盘上:
Sub SaveAttachment()
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myInspector = Application.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the first attachment " & _
"in the current item to the Documents folder? If a file with the " & _
"same name already exists in the destination folder, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _
myAttachments.Item(1).DisplayName
End If
Else
MsgBox "The item is of the wrong type."
End If
End If
End Sub
这是RTF消息吗?RTF消息将图像和对象(如Excel电子表格)嵌入为OLE对象而不是文件,并且
附件。对于OLE附件,SaveAsFile
将失败。如果要筛选出这样的附件,请确保跳过带有Attachment.Type=olOLE(6)
的附件,或者只处理类型为olByValue
或olembeddetem
的附件
如果您仍然需要保存OLE附件,可以使用-its.
SaveAsFile
方法将从最常见的OLE附件(如Word文档、PDF文件、Excel电子表格、图像等)中提取文件数据。您好,谢谢您的回复。它不是rtf,类型是1。所以我还是很困惑。你得到了一个有效的Attachment.FileName值吗?是的,这个名称对我来说是有效的。我今天晚些时候再查。作为一种解决方法,我捕获了错误。Dimitry,是的,文件名是有效的。捕获错误工作得很好,所以不幸的是我会用它来存储。那么它总是发生在同一个附件上吗?确切的文件名是什么?嗨,谢谢你的回复。我认为路径正确且完整,但我将在调试器中再次检查。添加附件是好的,它正在保存它,这就是问题所在。
Sub SaveAttachment()
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myInspector = Application.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you sure you want to save the first attachment " & _
"in the current item to the Documents folder? If a file with the " & _
"same name already exists in the destination folder, " & _
"it will be overwritten with this copy of the file."
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _
myAttachments.Item(1).DisplayName
End If
Else
MsgBox "The item is of the wrong type."
End If
End If
End Sub