替换或删除邮件中的inlineshapes仅在Outlook中运行一次

时间:2015-10-13 11:55:46

标签: vb.net outlook

我在vb.net中编写了一个outlook addin,它可以保存outlook 2010中的邮件。 用户可以决定使用邮件保存附件,也可以单独保存。

无论邮件是否有附件,插件都能正常工作并保存邮件。 插件也可以保存邮件的可能内联形状。

但只有一次。

如果我保存带有一个或多个inlineshape的邮件,一切正常(保存inlineshape,用文本替换inlineshape或删除邮件中的inlineshape,将邮件保存到磁盘)。

之后,如果我带另一个带有inlineshape的邮件,保存工作正常,但替换或删除inlineshape不起作用。

为什么? 我做错了什么?

非常感谢你的帮助。

这是调用保存邮件,附件和内联形状的函数的代码:

Dim outlookApp As Outlook.Application
Dim outlookExp As Outlook.Explorer
Dim outlookSel As Outlook.Selection

outlookApp = New Outlook.Application
outlookExp = outlookApp.ActiveExplorer
outlookSel = outlookExp.Selection 

For i As Integer = 1 To outlookSel.Count
   outlookObject = outlookSel.Item(i)
   SaveMail(outlookObject, onlyMails, strUser, strFormat, backupFolderMails, logFolder, bugFolder, indicator)
   System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookObject)
   outlookObject = Nothing
   GC.Collect()
   GC.WaitForPendingFinalizers()
   GC.Collect()
   GC.WaitForPendingFinalizers()
Next

以下是“savemail”函数中的代码,它保存了inlineshape并用文本替换了inlineshape:

If GetSeparateAttachments() = True Then
   outlookAllAttachments = outlookObject.Attachments
   attachmentCounter = outlookAllAttachments.Count
   counter = 0
   While attachmentCounter <> counter
      outlookAttachment = outlookAllAttachments(attachmentCounter)
      If outlookAttachment.Type = Outlook.OlAttachmentType.olOLE Then
         If outlookObject.BodyFormat = 3 Then
            outlookInspector = outlookObject.GetInspector
            outlookWordEditor = outlookInspector.WordEditor
            If outlookWordEditor.ProtectionType <> Microsoft.Office.Interop.Word.WdProtectionType.wdNoProtection Then
               outlookWordEditor.Unprotect()
            End If
            wordInlineShapes = outlookWordEditor.InlineShapes
            For j As Integer = wordInlineShapes.Count To 1 Step -1
               wordShape = wordInlineShapes.Item(j)
               wordShape.Select()
               outlookWordEditor.Application.Selection.CopyAsPicture()
               dataObject = Clipboard.GetDataObject()
               If dataObject.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
                  image = CType(dataObject.GetData(GetType(System.Drawing.Bitmap)), System.Drawing.Bitmap)
                  secureNameAttachment = CheckPath(GetDuplicateAttachments(), saveFolderAttachment & "\" & CheckFileName(mailTimestamp & "_" & outlookAttachment.DisplayName & j, replaceSign) & GetPictureFormat(image.RawFormat), fileExtension)
                  image.Save(secureNameAttachment)
                  attachmentsString = attachmentsString & ";" & secureNameAttachment
               Else
                  Throw New Exception("Error")
               End If
               wordRange = outlookWordEditor.Range(wordShape.Range.Characters.First.Start, wordShape.Range.Characters.Last.End)
               wordRange.Text = secureNameAttachment & vbCrLf
               System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordRange)
               wordRange = Nothing
               System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordShape)
               wordShape = Nothing
               counter = counter + 1
            Next
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordInlineShapes)
            wordInlineShapes = Nothing
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookWordEditor)
            outlookWordEditor = Nothing
            outlookInspector.Close(Outlook.OlInspectorClose.olSave)
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookInspector)
            outlookInspector = Nothing
         Else
            Throw New Exception("Error")
         End If
      Else
         '
      End If
      System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookAttachment)
      outlookAttachment = Nothing
      System.Runtime.InteropServices.Marshal.FinalReleaseComObject(outlookAllAttachments)
      outlookAllAttachments = Nothing
      outlookObject.Save()
   End While                        
End If

0 个答案:

没有答案