2012-07-27 63 views
1

我已經編寫了一個Outlook 2010加載項,用於解析電子郵件正文中的數據。目前,我將數據寫入.CSV文件,然後打開Excel工作簿,該工作簿會自動導入數據。我想跳過.CSV並在加載項中直接打開工作簿並寫入數據。我正在使用.NET 4.0。並且VS2010 Outlook加載項使用Outlook 14.0庫。當我嘗試包含Excel 14.0庫的引用時,編譯器給我一個重複的Office.dll錯誤。在重新解釋問題3天后,互聯網搜索未能提供任何答案。 這是我的難倒! '莉在這裏幫忙? : -/訪問Excel 2010到Outlook 2010 C#加載項

這裏是我的外接代碼:(希望我正確的格式呢?這是我第一次發佈。)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.Reflection; 
using System.Windows.Forms; 
using System.Xml.Linq; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using Office = Microsoft.Office.Core; 


public partial class ThisAddIn 
{ 
    Outlook.Explorer currentExplorer = null; 
    private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     currentExplorer = Application.ActiveExplorer(); 
     currentExplorer.SelectionChange += new  Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event); 
    } 

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
    { 
    } 
    private void CurrentExplorer_Event() 
    { 
     Outlook.Selection selection = this.Application.ActiveExplorer().Selection; 
     Outlook._Application olApp = new Outlook.Application(); 
     if (Application.ActiveExplorer().Selection.Count > 0) 
     { 
      Object selObject = Application.ActiveExplorer().Selection[1]; 
      if (selObject is Outlook.MailItem) 
      { 
       Outlook.MailItem message = (Outlook.MailItem)selObject; 
       //MessageBox.Show(message.Subject); 
       if (message.Subject == "Powerball Drawing Info") 
       { 
        string fileString = string.Empty; 
        string body = string.Empty; 
        string[] numbers = new string[7]; 
        int start = 0; 
        int y = 0; 
        // char ch; 

        for (int x = 0; x < 7; x++) 
        { 
         numbers[x] = string.Empty; 
        } 
        //Outlook.MailItem message = (Outlook.MailItem)e.OutlookItem; 
        body = message.Body; 
        start = body.IndexOf(":"); 

        for (int x = start; x < start + 40; x++) 
        { 
         if (Char.IsDigit(body[x])) // copy entire number to array element 
         { 
          numbers[y] += body[x]; 
         } 

         if (Char.IsWhiteSpace(body[x]) && numbers[y].Length > 0) // increment number array index 
         { 
          y++; 
          if (y == 5) // skip the word " Powerball " and jump the array index to match spreadsheet 
          { 
           y = 6; 
           x += 10; 
           continue; 
          } 
         } 
         if (y == 6 && Char.IsDigit(body[x + 1]) == false) // test for finish of Powerball number 
         { 
          x = start + 40; 
         } 
        } 
        for (int x = 0; x < 7; x++) // build a string to write to file and display 
        { 
         fileString += numbers[x]; 
         if (x != 6) 
         { 
          fileString += ", "; 
         } 
        } 

        System.IO.File.WriteAllText(@"C:\Users\rrichard39\Documents\Powerball.csv", fileString); 
        Process.Start(@"C:\Users\rrichard39\Documents\Powerball_Test.xlsm"); 

       } 
      } 
     } 
    } 

    #region VSTO generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InternalStartup() 
    { 
     this.Startup += new System.EventHandler(ThisAddIn_Startup); 
     this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
    } 

    #endregion 
} 
+1

確切的錯誤信息? – 2012-07-27 05:46:58

回答

0

蒂姆 - 在原來的職位說明,錯誤是「編譯給我一個「重複的Office.dll」的錯誤「 謝謝,無論如何,蒂姆,但我大概在2個小時前計算出來。我不確定哪個Outlook Interop Library VS2010 Outlook Add-In項目構建器/模板使用(我在猜測COM,但不確定),但是我啓動了一個新的Outlook 2010加載項並使用了.NET Interop的.NET版本圖書館14.0參考和一切工作就像一個魅力。我仍然想知道如何判斷項目構建器/模板使用哪些庫(COM或NET)以供將來參考,以便我知道我正在使用哪些庫。我在Outlook Interop Library Reference的屬性中查找了信息,但我能找到的只有版本號。

0

蒂姆, 確切的錯誤信息是:

Error 1 An assembly with the same identity 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' has already been imported. Try removing one of the duplicate references. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Office.dll