【问题标题】:Access Excel 2010 thru Outlook 2010 C# add-In通过 Outlook 2010 C# 加载项访问 Excel 2010
【发布时间】:2012-07-27 03:55:06
【问题描述】:

我编写了一个 Outlook 2010 加载项,用于解析电子邮件正文中的数据。目前,我将数据写入 .CSV 文件,然后打开 Excel 工作簿,它会自动导入数据。我想跳过 .CSV 并在加载项中直接打开工作簿并写入数据。我正在使用.NET 4.0。 VS2010 Outlook 加载项使用 Outlook 14.0 库。当我尝试包含 Excel 14.0 库的引用时,编译器给我一个重复 Office.dll 的错误。重新表述问题 3 天后,互联网搜索无法提供任何答案。 这把我难住了! '莉莉在这里帮忙? :-/

这是我的插件代码:(希望我的格式正确。这是我的第一篇文章)

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
}

【问题讨论】:

    标签: c# excel outlook add-in


    【解决方案1】:

    Tim - 如原始帖子中所述,错误是“编译器给了我一个“重复 Office.dll”的错误” 谢谢,无论如何,蒂姆,但我大约在 2 小时前就知道了。我不确定使用哪个 Outlook 互操作库 VS2010 Outlook 加载项项目构建器/模板(我猜是 COM,但不确定),但我启动了一个新的 Outlook 2010 加载项并使用了 NET 版本的 Excel 互操作图书馆 14.0 参考和一切都像一个魅力。我仍然想弄清楚如何判断项目构建器/模板使用哪些库(COM 或 NET)以供将来参考,所以我知道我正在使用什么。我浏览了 Outlook 互操作库参考的属性以获取信息,但我能找到的只是版本号。

    【讨论】:

      【解决方案2】:

      为了蒂姆, 确切的错误信息是:

      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

      【讨论】:

        猜你喜欢
        • 2012-08-24
        • 2012-10-14
        • 2014-05-10
        • 1970-01-01
        • 2013-07-15
        • 1970-01-01
        • 2012-12-05
        • 1970-01-01
        • 2012-07-06
        相关资源
        最近更新 更多