2016-05-24 12 views
-1

Ich versuche Outlook NewMail mit C# -Code zu öffnen (ohne COM) aber leider erhalte ich den folgenden Fehlercode:C# -Code, der versucht, neue Outlook Mail automatisch zu öffnen. Fehler 0x80020006 (DISP_E_UNKNOWNNAME)

0x80020006 (DISP_E_UNKNOWNNAME) 

Der C# -Code ist knapp unter.


using System; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Windows.Forms; 
using Erp.Adapters; 
using Erp.UI; 
using Ice.Lib; 
using Ice.Adapters; 
using Ice.Lib.Customization; 
using Ice.Lib.ExtendedProps; 
using Ice.Lib.Framework; 
using Ice.Lib.Searches; 
using Ice.UI.FormFunctions; 
using System.Net.Mail; 
using System.Runtime.InteropServices; 
using System.Reflection; 
using System.Linq; 
using Microsoft.CSharp; 
using System.IO.Compression; 
using System.Reflection; 
using System.Text; 
using System.IO; 

public class Script 
{ 
    // ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! ** 
    // Begin Wizard Added Module Level Variables ** 

    // End Wizard Added Module Level Variables ** 

    // Add Custom Module Level Variables Here ** 

    public void InitializeCustomCode() 
    { 
     // ** Wizard Insert Location- Do not delete 'Begin/End Wizard Added Variable Initialization' lines ** 
     // Begin Wizard Added Variable Initialization 


     this.POForm.AfterToolClick += new Ice.Lib.Framework.AfterToolClickEventHandler(this.POForm_AfterToolClick); 
     // End Wizard Added Variable Initialization 

     // Begin Wizard Added Custom Method Calls 

     // End Wizard Added Custom Method Calls 
    } 

    public void DestroyCustomCode() 
    { 
     // ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines ** 
     // Begin Wizard Added Object Disposal 


     this.POForm.AfterToolClick -= new Ice.Lib.Framework.AfterToolClickEventHandler(this.POForm_AfterToolClick); 
     // End Wizard Added Object Disposal 

     // Begin Custom Code Disposal 

     // End Custom Code Disposal 
    } 

private void POForm_AfterToolClick(object sender, Ice.Lib.Framework.AfterToolClickEventArgs args) 
    { 
    if(args.Tool.Key == "EmailFaxTool") 
{ 

Assembly interopAssembly = Assembly.LoadFile(@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Outlook.dll"); 

object outlookApplication = interopAssembly.CreateInstance("Microsoft.Office.Interop.Outlook.ApplicationClass"); 
Type outlookApplicationType = interopAssembly.GetType("Microsoft.Office.Interop.Outlook.ApplicationClass"); 



dynamic mailItem = outlookApplicationType.InvokeMember("CreateItem", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, outlookApplication, new object[] { 0 }); 


//ADDRESS 
//object recipients = outlookApplication.GetType().InvokeMember("Recipients",BindingFlags.GetProperty, null, outlookApplication, null); 
//string To = "[email protected]"; 
//object[] address = new object[1]; 
//address[0] = To; 

//SUBJECT1 
//recipients.GetType().InvokeMember ("Add", BindingFlags.InvokeMethod,null, recipients, address); 
//string subject = "Mail Message Subject"; 
//parms [0] = subject; 

//SUBJECT 
//outlookApplication.GetType().InvokeMember("Subject", BindingFlags.SetProperty,null, outlookApplication, new object[] { 0 }); 
//string msg = "Just a message saying hello"; 


//BODY 
//outlookApplication.GetType().InvokeMember("Body", BindingFlags.SetProperty,null, outlookApplication, new object[] { 0 }); 

//DISPLAY OR SEND 
// Invoke the Send method of the mail item. 
outlookApplication.GetType().InvokeMember("Display", BindingFlags.InvokeMethod,null, outlookApplication,new object[] { true }); 


{ 
throw new Exception("OK."); 
} 
} 

} 
} 
+2

Haben Sie diesen Fehler googeln - es scheint eine Menge Informationen darüber zu sein – BugFinder

+0

Die Mitglieder der Outlook-Benutzeroberfläche sind [hier dokumentiert] (https://msdn.microsoft.com/en-us/library/microsoft. office.interop.outlook.application_members.aspx). Wie Sie sehen können, hat es keine Display() -Methode. Sie erhalten also den Laufzeitfehler "unbekannter Name". Diese Probleme treten auf, wenn Sie das Schlüsselwort * dynamic * verwenden. Schreiben Sie den Code zuerst, indem Sie einen Verweis auf Outlook hinzufügen, damit der Compiler Ihnen sagen kann, wenn Sie es falsch machen. –

Antwort

0

Anruf MailItem.Display. Aopplication.Display-Methode existiert nicht. Wie @Hans Passant im Kommentar erwähnt, ist die Verwendung von "Dynamic" eine ziemlich schreckliche Idee.