繁体   English   中英

Outlook无法通过VBA保存附件

[英]Outlook not saving attachment via VBA

我有一些可以在我的机器上正常运行的VBA代码,但在我的客户上却没有。 挂断的地方是打开电子邮件附件并将其保存到计算机上的某个位置。

For Each nm in file_names 'file_names is just an array of strings
    found_file=False
    curr_date=CDate("1-1-9999")
    For Each olItem in olItems
        If olItem.ReceivedTime < curr_date and olItem.SenderEmailAddress=email and TypeName(olItem)="MailItem" then
            Set olAttach=olItem.attachments.Item(1)
            If not olAttach is Nothing then
                If olAttach.Filename Like nm & ".*" then
                    found_file=True
                    curr_date=olItem.ReceivedTime
                end if
            end if
        end if
    Next
    If found_file then
        olAttach.SaveAsFile pth & olAttach.Filename 'errors out here
        ...

错误消息是Cannot save the attachment ,未指定原因”。

我试图让他启用所有宏,关闭受保护的视图选项,重新启动excel和Outlook,尝试将不同的文件位置保存到其中,当文件路径与文件名连接在一起时不会出现双\\,并且我确定他没有使用Mac。 显然其中一个附件文件确实打开了,但是它拒绝保存。

看起来传递给SaveAsFile方法的文件路径/名称字符串不是格式正确的路径。 例如, FileName可能包含禁止的符号等。请尝试使用以下代码作为测试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM