简体   繁体   English

Attachment.SaveAsFile方法(Outlook)-错误:无法保存附件。 找不到此文件。 验证路径和文件名正确

[英]Attachment.SaveAsFile Method (Outlook) - Error: Cannot Save the Attachment. Cannot Find This File. Verify the Path and File Name are Correct

I am working with an Outlook AddIn which has the functionality to save an email and its attachments to a network drive. 我正在使用Outlook AddIn,它具有将电子邮件及其附件保存到网络驱动器的功能。

For one of the users of the application, when saving 10 pdfs of about 1MB in a row, the application crashes with the following error message: 对于应用程序的用户之一,当连续保存10个pdf(约1MB)时,该应用程序崩溃并显示以下错误消息:

Cannot Save the Attachment. Cannot Find This File. Verify the Path and File Name are Correct.

The user did three tests and the issue happened twice on the same pdf and once on another. 用户进行了三项测试,同一pdf上发生了两次该问题,另一次发生了该问题。

Here is the snippet of code in question: 这是有问题的代码片段:

For myCount As Integer = 1 To inMailItem.Attachments.Count
    If inMailItem.Attachments(myCount).Type <> Outlook.OlAttachmentType.olOLE Then

        Dim thisFileName = IO.Path.GetFileName(inMailItem.Attachments(myCount).FileName)
        Dim thisExt = IO.Path.GetExtension(thisFileName)

        Dim charsAvailable = 46 - thisExt.Length ' [filename][random].ext <= 50 chars
        Dim tmpFileName = IO.Path.GetFileNameWithoutExtension(thisFileName.Substring(0, Math.Min(thisFileName.Length, charsAvailable))) + GetRandomChars(4) + thisExt
        fileName = EscapeSqlInput(tmpFileName)
        attachmentFilePath = myDirectoryName & "\" & fileName
        fileInfo = New IO.FileInfo(attachmentFilePath)
        isValidFileType = "1"
        isEmailBody = "0"
        sortOrder += 1
        inMailItem.Attachments(myCount).SaveAsFile(attachmentFilePath)
        attachmentFileSize = fileInfo.Length

        sb = New StringBuilder()
        sb.Append("INSERT INTO EmailDocuments (EmailFileID, DirectoryPath, FileName, IsValidFileType, AttachmentFileSize, IsEmailBody, SortOrder) ")
        sb.AppendFormat("VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')",
                        emailFileId,
                        directoryPath,
                        fileName,
                        isValidFileType,
                        attachmentFileSize,
                        isEmailBody,
                        sortOrder)
        SqlHelper.SqlExecuteNonQuery(sb.ToString())
    End If
Next

On the network drive I can see a .tmp file containing the exact same number of bytes as the original file. 在网络驱动器上,我可以看到一个.tmp文件,其中包含与原始文件完全相同的字节数。 If I switch the .tmp extension to .pdf I can open the file using adobe reader. 如果将.tmp扩展名切换为.pdf,则可以使用Adobe Reader打开文件。 I see another .pdf file on the same drive, this one containing 0 bytes. 我在同一驱动器上看到另一个.pdf文件,其中包含0个字节。

I am assuming the empty pdf file and the tmp files are related to the SaveAsFile method process. 我假设空的pdf文件和tmp文件与SaveAsFile方法过程有关。

Three users tested the issue. 三个用户测试了此问题。 Two in San Diego, CA and one in New York, NY. 两台在加利福尼亚州圣地亚哥,一台在纽约州纽约。 The person in New York had the failure and the two people in San Diego didn't encounter the issue. 纽约的那个人失败了,圣地亚哥的两个人没有遇到这个问题。

Would somebody have an idea of what could be the cause of this issue? 有人会对造成此问题的原因有一个想法吗?

It sure sounds like you are running out of RPC channels. 听起来好像您快要用完RPC频道了。 Avoid using multiple dot notation (especially in a loop) and release all object as soon as you are done with them 避免使用多个点表示法(尤其是在循环中),并在完成处理后立即释放所有对象

Attachments attachments = inMailItem.Attachments;
for (int myCount = 1; myCount <= attachments.Count; myCount++)
{
  Attachment attach = attachments.Item(myCount);
  attach.SaveAsFile(attachmentFilePath);
  Marshal.ReleaseComObject(attach);
}
Marshal.ReleaseComObject(attachments);

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

相关问题 带有重音文件名的 Outlook Attachment.SaveAsFile 导致找不到文件 - Outlook Attachment.SaveAsFile with accented filename results in file not found C#Outlook Attachment.SaveAsFile在.msg文件类型上失败 - C# Outlook Attachment.SaveAsFile fails on .msg file type Python / Outlook附件:“找不到此文件。验证存在” - Python/Outlook Attachment: “cannot find this file..verify exists” Outlook 以编程方式避免 XSL 上的 Attachment.SaveAsFile() 弹出窗口 - Outlook programmatically avoid Attachment.SaveAsFile() Pop-up on XSL 无法保存附件。 操作失败。 服务器中找不到对象错误 - Cannot save the attachment. The operation failed. An object cannot be found error in server 宏错误“无法保存附件。 没有适当的许可” - Macro error 'Cannot save the attachment. Don't have appropriate permission' Outlook将附件保存到文件不起作用 - Outlook Save attachment to file not working 检索 Outlook email 时出现“无法保存附件”错误 - 'Cannot save the attachment' error when retrieving Outlook email 如何将文本附件从特定的Outlook文件夹保存到文件。 使用Excel VBA - How do i save a text attachment form a specific outlook folder to file. using Excel VBA VBA Outlook SaveAsFile 方法创建空文件 - VBA Outlook SaveAsFile method creates empty file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM