none
Fehler beim Verändern eines Anhangs einer Outlook Email Message RRS feed

  • Frage

  • Guten Tag,

    Beim Zugriff auf die Binärdaten eines Anhanges als Reaktion auf das absenden der Nachricht, entsprechend des Vorbildes unter http://msdn.microsoft.com/en-us/library/hh290849.aspx,

    Object attachmentData = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN);

    erhalte ich die Daten als Byte-Array, welche ich in der Folge verändere:

    Byte[] data = execute((Byte[]) attachmentData);

    Beim zurückschreiben der Daten mittels

    attachment.PropertyAccessor.SetProperty(PR_ATTACH_DATA_BIN, data);

    erhalte ich dann den Fehler "Auf eine geschlossene Datei kann nicht zugegriffen werden."

    Welche Umstände führen zu diesem Fehler?

    Mit freundlichem Gruß

    Thomas

    Sonntag, 29. April 2012 17:54

Antworten

  • Hallo kernel.exe,

    Folgender Code mit Kleine Änderungen (CreateItem) aus dem Artikel How to copy attachments funktioniert einwandfrei. Schau Dir den Code an, vielleicht kann er Dir weiterhelfen.

    (Verweis auf Microsoft.Office.Interop.Outlook (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Outlook.dll) hinzufügen.)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using System.IO;
    
    namespace OL_Attachment_Modify
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                const string PR_ATTACH_DATA_BIN ="http://schemas.microsoft.com/mapi/proptag/0x37010102";
                //Create a mail item
                Outlook.Application OlApp = new Outlook.Application();
                Outlook.MailItem mail = (Outlook.MailItem)OlApp.Application.CreateItem(Outlook.OlItemType.olMailItem);
                mail.Subject = "Demo Attachment Stream";
    
                //Create the c:\demo folder if it doesn't exist
                if(!Directory.Exists(@"c:\demo"))
                {
                    Directory.CreateDirectory(@"c:\demo");
                }
    
                //Write attach.txt
                StreamWriter sw = new StreamWriter(@"c:\demo\attach.txt");
                char charA = 'A';
                string myString = new string(charA ,4096);
                sw.WriteLine(myString);
                sw.Close();
    
                //Add attach.txt as an attachment
                Outlook.Attachment attach =  mail.Attachments.Add(@"c:\demo\attach.txt",Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
    
                //Save the item
                mail.Save();
    
                //Use PropertyAccessor to retrieve attachment byte stream
                byte[] attachStream = attach.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN) as byte[];
    
                //Iterate the stream and change "A" to "B"
                for (int i = 0; i < attachStream.Length; i++)
                {
                    attachStream[i] = 0x42; //Hex for "B"
                }
    
                //Set PR_ATTACH_DATA_BIN to attachStream
                attach.PropertyAccessor.SetProperty(PR_ATTACH_DATA_BIN, attachStream);
    
                //Save the item again
                mail.Save();
            }
        }
    }

    Grüße,

    Robert


    Robert Breitenhofer, MICROSOFT  Twitter Facebook
    Bitte haben Sie Verständnis dafür, dass im Rahmen dieses Forums, welches auf dem Community-Prinzip „Entwickler helfen Entwickler“ beruht, kein technischer Support geleistet werden kann oder sonst welche garantierten Maßnahmen seitens Microsoft zugesichert werden können.

    Dienstag, 8. Mai 2012 07:32
    Moderator

Alle Antworten