none
COMException wurde nicht vom Benutzercode behandelt RRS feed

  • Frage

  • Moin,

    ich schreibe gerade ein kleine Add-In zum "Mails speichern unter" auf Knopfdruck. Funktioniert soweit auch solange ich nicht die Klasse/Mehtode Outlook.MailItem() anspreche. Sobald ich das mache um z.b. die Betreffzeile aus zu lesen bekomme ich folgende Fehlermeldung. Hab schon bis auf Seite 10 von Google gesucht und nix wirklich hilfreiches gefunden. Bin absoluter Neuling was Visual Studio angeht.

    "Eine Ausnahme vom Typ "System.Runtime.InteropServices.COMException" ist in mscorlib.dll aufgetreten, doch wurde diese im Benutzercode nicht verarbeitet.

    Zusätzliche Informationen: Retrieving the COM class factory for component with CLSID {00061033-0000-0000-C000-000000000046} failed due to the following error: 80040154 Klasse nicht registriert (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."

    private void cmdKnopf_Click(object sender, RibbonControlEventArgs e)
            {
                Outlook.Application olApp;
                Outlook.Explorer olExplorer;
                Outlook.MailItem olMail = new Outlook.MailItem(); <--- Hier wird die Fehlermeldung ausgelöst
    
                olApp = new Outlook.Application();
                olExplorer = olApp.ActiveExplorer();
                string sName;
                
                foreach(Outlook.MailItem item in olExplorer.Selection)
                {
                    sName = olMail.Subject;
                    sName = replace(sName);
                   item.SaveAs("C:\\TestFileSave\\"+sName+".msg" ,Outlook.OlSaveAsType.olMSG);
                }
    
    
    
            }
    
            private string replace(string sName)
            {
                string result = sName.Replace("/","_");
                result = sName.Replace(":","_");
                result = sName.Replace("?","_");
                result = sName.Replace("<","_");
                result = sName.Replace(">","_");
                result = sName.Replace("|","_");
                return result;
            }

    Donnerstag, 29. Oktober 2015 10:39

Antworten

  • Hallo,

    es ist gefährlich so direkt  ein Object aus selection nach MailItem zu konvertieren. Selection kann auch die Objekte von MeetingItem , ContactItem, oder TaskItem enthalten. In diesem Fall wird eine Ecxeption geworfen.

    Man braucht nur ein  MailItem Objekt.    

    Besser wäre:

                string sSubject = string.Empty;
                for(int iLoop=1 ; iLoop<= selection.Count; iLoop++)
                {
                    Object selObject = olExplorer.Selection[iLoop];
                    if (selObject != null && selObject is Outlook.MailItem)
                    {
                        Outlook.MailItem mail = (Outlook.MailItem)selection[iLoop];
                        sName = string.Format("{0:yyyyMMddHHmmss_tt}", mail.ReceivedTime);
                        sSubject = mail.Subject;
                        sName += replace(sSubject);
                        mail.SaveAs("C:\\TestFileSave\\" + sName + ".msg", Outlook.OlSaveAsType.olMSG);
                    }
                   
                    
                }

    Grüße


    • Als Antwort markiert Gevatter Freitag, 30. Oktober 2015 10:03
    • Bearbeitet Iso7 Freitag, 30. Oktober 2015 10:05
    Freitag, 30. Oktober 2015 09:44

Alle Antworten

  • Hallo,

    für das Erstellen des Objekts von MailItem soll die Methode  CreateItem verwendet werden.

    Grüße

    Donnerstag, 29. Oktober 2015 10:59
  • Danke erst mal für die Antwort. Leider verstehe ich deine Antwort noch nicht ganz.

    Könntest du mir anhand meines Codes da ein Beispiel zeigen?

    Grüße

    Donnerstag, 29. Oktober 2015 11:51
  • Hallo,

    Outlook.MailItem ist ein Interface, man kann kein Objekt von einem Interface direkt erzeugen.

                Outlook.MailItem item = this.Application.CreateItem(Outlook.OlItemType.olMailItem);
    
                if (item != null)
                {//new MailItem is created 
                    
                }

    Noch ein Beispiel: Programmgesteuertes Erstellen von E-Mail-Elementen..

    Grüße



    • Bearbeitet Iso7 Donnerstag, 29. Oktober 2015 12:21
    Donnerstag, 29. Oktober 2015 12:16
  • Ah Danke.

    Ja da liegt ein genereller Fehler vor wie ich nun sehe. Gemeint war nicht das ich eine neue Mail erzeuge, sondern das ich von einer selektierten Mail den Subject auflese, in den sName(Filename) übergebe und die Mail dann Speicher.

    Eine Idee wie ich das mache?

    Grüße

     
    Donnerstag, 29. Oktober 2015 12:38
  • Ungefähr so:

    Outlook.Explorer olExplorer = this.Application.ActiveExplorer();
    
                if (olExplorer.Selection.Count > 0)
                {
                    string sName ="NoName";
                    Object selObject = olExplorer.Selection[1];
                    if (selObject is Outlook.MailItem)
                    {
                        Outlook.MailItem item = (Outlook.MailItem)selObject;
                        if (string.IsNullOrEmpty(item.Subject) == false)
                        {
                            sName = item.Subject;
                            //hier noch sName noch überprüfen  
                        }
                        item.SaveAs("C:\\" + sName + ".msg", Outlook.OlSaveAsType.olMSG);
                    }
                }

    Grüße

    Donnerstag, 29. Oktober 2015 13:23
  • Vielen Dank für deine Hilfe. Hat mich wirklich weiter gebracht.

    Ich hab es jetzt so gelöst. Einfach nur mal für andere die mal danach suchen sollten.

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Outlook.Application myApplication = Globals.ThisAddIn.Application;
                Outlook.Explorer myActiveExplorer = (Outlook.Explorer)myApplication.ActiveExplorer();
                Outlook.Selection selection = myActiveExplorer.Selection;
    
                System.IO.StreamReader myFile = new System.IO.StreamReader("c:\\Test1.txt");
                string sPath = myFile.ReadToEnd();
    
                string sName;
    
                int i = 1;
                foreach (Outlook.MailItem item in selection)
                {
    
                    Outlook.MailItem mail = (Outlook.MailItem)selection[i];
                    sName = string.Format("{0:yyyyMMddHHmmss_tt}", mail.ReceivedTime);
                    sName += replace(mail.Subject);
                    item.SaveAs("C:\\TestFileSave\\" + sName + ".msg", Outlook.OlSaveAsType.olMSG);
                    i++;
                }
            }
    
    
    
            private string replace(string sName)
            {
                sName = Regex.Replace(sName, "[^0-9A-Za-zäöüÄÖÜ ,]", "_");
                sName = sName.Replace(" ", "_");
                return sName;
            }

    Freitag, 30. Oktober 2015 08:56
  • Hallo,

    es ist gefährlich so direkt  ein Object aus selection nach MailItem zu konvertieren. Selection kann auch die Objekte von MeetingItem , ContactItem, oder TaskItem enthalten. In diesem Fall wird eine Ecxeption geworfen.

    Man braucht nur ein  MailItem Objekt.    

    Besser wäre:

                string sSubject = string.Empty;
                for(int iLoop=1 ; iLoop<= selection.Count; iLoop++)
                {
                    Object selObject = olExplorer.Selection[iLoop];
                    if (selObject != null && selObject is Outlook.MailItem)
                    {
                        Outlook.MailItem mail = (Outlook.MailItem)selection[iLoop];
                        sName = string.Format("{0:yyyyMMddHHmmss_tt}", mail.ReceivedTime);
                        sSubject = mail.Subject;
                        sName += replace(sSubject);
                        mail.SaveAs("C:\\TestFileSave\\" + sName + ".msg", Outlook.OlSaveAsType.olMSG);
                    }
                   
                    
                }

    Grüße


    • Als Antwort markiert Gevatter Freitag, 30. Oktober 2015 10:03
    • Bearbeitet Iso7 Freitag, 30. Oktober 2015 10:05
    Freitag, 30. Oktober 2015 09:44
  • Auf die Idee bin ich gar nicht gekommen das der User versucht was anderes außer einer Mail zu speichern.

    Du hast damit natürlich vollkommen recht und ich bedanke mich nochmals für deine Hilfe.

    Freitag, 30. Oktober 2015 10:03