Hobby Chef

July, 2008 - Jive into Messaging world - Site Home - MSDN Blogs

Blog - Title

July, 2008

  • Jive into Messaging world

    Why can't I replace attachments to original location in RTF?

    • 1 Comments

    If you ever tried to manipulate attachments within RTF emails then you are probably aware of this weird issue. Say you have 4 attachments in the email body of a RTF email. Now if you programmatically tries to delete those attachments and place new attachments to their original location then you will see how messy it could be.

    The attachments loose their position and can be placed anywhere in the body. So whom to blame? The code.. really? Not exactly but yes to code. This is a weird timing issue and we still do not know the real root cause of the problem. But the below code can do the job for you.

     

    Sub AttachmentRTF() 
    Dim oItem As Object 
    Dim oAttachments As Outlook.Attachments 
    Dim iCount As Integer 
    Dim arrPos() 
    Dim arrPath() 
    Dim sAttPathFileName 
    Dim sAttFileName 
    Dim i As Integer 
    Const sAttPath = "C:\" 
    
    Set oItem = Application.ActiveInspector.CurrentItem 
    Set oAttachments = oItem.Attachments 
    iCount = oAttachments.Count 
    
    If iCount > 0 Then 
    
        ReDim arrPos(iCount) 
        ReDim arrPath(iCount) 
        ' Remove all attachments 
        For i = iCount To 1 Step -1 
            arrPos(i) = oAttachments(i).Position 
            arrPath(i) = sAttPath & "Restored_" & oAttachments.Item(i).FileName 
            oAttachments.Item(i).SaveAsFile arrPath(i) 
            oAttachments(i).Delete 
            oItem.Save 
        Next 
    
        ' Add back attachments in original positions 
        For i = 1 To iCount 
            oItem.Attachments.Add arrPath(i), , arrPos(i) 
            oItem.Save 
        Next 
    End If 
    End Sub

    Please do let me know if you face any problems with this code.

    References:

    Attachments Object
    http://msdn2.microsoft.com/en-us/library/bb147604.aspx

    "To ensure consistent results, always save an item before adding or removing objects in the Attachments collection of the item."

  • Jive into Messaging world

    DevMsgTeam: New Team Aggregate Blog Feed

    • 0 Comments
    There are several members of my team at Microsoft that have active blogs with interesting content.  We've often thought about creating a team blog, much like the Exchange team or Outlook team or even the Exchange Developer team.  However, those seemed like time consuming solutions that would create yet another place to get our posts from.

    Instead we decided to create a common tag among all our common Exchange and Outlook developer support blogs posts (DevMsgTeam) so that there is one URL for all the posts on the team.  Enjoy!

    The feed will aggregate here.

    You can subscribe to the RSS feed here.

Page 1 of 1 (2 items)