65.9K
CodeProject 正在变化。 阅读更多。
Home

使用自定义发送按钮创建 Outlook 2010 加载项

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5投票s)

2014 年 12 月 22 日

CPOL

3分钟阅读

viewsIcon

46358

downloadIcon

1489

这是“创建 Outlook 2010 加载项”的替代方案。

引言

本技巧将介绍如何在功能区上创建一个按钮并发送自定义邮件。它还将介绍安装过程。它使用 C# 在 VS2012 和 Office 2010 中开发。

背景

我被要求以某种方式创建条件,允许非 IT 技术人员编辑圣诞贺卡,以便他们可以向关心的人发送自定义消息。

我在功能区上创建了一个按钮,它:

  1. 打开一个可以填写消息的表单
  2. 编辑图像并在其中写入消息
  3. 发送带有附件图像的电子邮件

Using the Code

首先,使用 Office – Outlook 2010 加载项模板创建一个新项目。

然后添加功能区(添加新项 – 功能区(Visual Designer))。功能区带有内置选项卡和用于组织功能区上控件的组。更改选项卡和组的标签。将选项卡的ControlIdType属性更改为自定义,并将RibbonRibbonType更改为Microsoft.Outlook.Explorer

此时,您可以单击“开始”,它将打开带有Ribbon的 Outlook。使用工具箱,将 Office 功能区控件按钮添加到组。

如果需要,可以向按钮添加图像并在其上写入标签。

向项目添加一个新表单。

在下面的按钮上,添加代码,单击后将显示表单。这是Ribbon.cs的代码。

public partial class MyOutlookAddIn
{
    private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
    {

    }

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        ShowForm();
    }

    private Form1 form1 = null;
    //this is to show the form    
    private void ShowForm()
    {
        if (form1 == null)
        {                //uses the Global class variable to fetch the Form
            form1 = new Form1(Globals.ThisAddIn.Application);
        }
        form1.ShowDialog();
    }
}

绘制一个表单,用于捕获所有必要信息(主题、邮件内容、电子邮件等)。向项目添加一个图像,该图像将作为贺卡的背景。在属性中,将“复制到输出目录”设置为“如果较新则复制”。

确保将表单的构造函数更改为在构造函数中接收 Outlook 应用程序。

public partial class Form1 : Form
    {
        protected Microsoft.Office.Interop.Outlook.Application App;

        public Form1(Microsoft.Office.Interop.Outlook.Application _app)
        {
            App = _app;
            InitializeComponent();
        }

单击“发送”按钮,它将使用下面的代码将邮件内容写入图像的特定位置。

//Create the location point
PointF location = new PointF(137f, 150f);           

//get the message located on the installation folder of the add-in
string imageFilePath = Path.Combine
(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "picture.png");

//Convert the image to a manageable form (Bitmap)                           
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);

using (Graphics graphics = Graphics.FromImage(bitmap))
{
   using (Font arialFont = new Font("Comic Sans", 2))
   {       
       if (!string.IsNullOrWhiteSpace(to))
       {     //draw on it
            graphics.DrawString(to + ",", arialFont, Brushes.CornflowerBlue, location);
       }
   }
}

   //it then saves the image using another name, avoiding it to be overriden.
  imageFilePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, 
  "picture1.png")   bitmap.Save(imageFilePath);

然后发送图像。

private void SendMessage(Microsoft.Office.Interop.Outlook.Application oApp, 
List<string> bodyMessage, string stringBodyMessage, 
string receiver, string subject, string from, string to )
        {
           try
           {                              
               // Create a new mail item. Pass the application received on the form as parameter
               Microsoft.Office.Interop.Outlook.MailItem oMsg = 
               (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem
               (Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                //add the body of the email
                oMsg.HTMLBody = stringBodyMessage; 
                //Add an attachment.
                String sDisplayName = "MyAttachment";
                int iPosition = (int)oMsg.Body.Length + 1;
                int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
                //now attached the file
                prepareMessage(bodyMessage, from, to);
                clearFieldsAndClose();

                string thePath = Path.Combine
                (AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "picture1.png");
                Microsoft.Office.Interop.Outlook.Attachment oAttach = 
                    oMsg.Attachments.Add(thePath, iAttachType, iPosition, sDisplayName);
                
                //Subject line
                oMsg.Subject = subject;
                // Add a recipient.
                Microsoft.Office.Interop.Outlook.Recipients oRecips = 
                    (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
                // Change the recipient in the next line if necessary.
                Microsoft.Office.Interop.Outlook.Recipient oRecip = 
                    (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(receiver);
                oRecip.Resolve();
                // Send.
                oMsg.Send();
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
                
            }//end of try block
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Ocorreu um erro " + ex.Message);
            }//end of catch
        }

安装插件

单击文件 => 添加 => 新建项目 => 其他项目类型 => 安装和部署 => InstallShield。如果您没有 InstallShield,可以从此处下载:这里

使用项目向导并填写应用程序信息和安装要求。特别注意应用程序文件。单击“添加项目输出”并添加主要输出。然后还添加清单文件和 vsto 文件(都位于构建后创建的bin文件夹中)。也添加图像。

安装的最后一步是在应用程序注册表中添加注册表项。最初,它可能在HKEY_CURRENT_USER下为空白,但如果您单击左侧的注册表,您将被重定向到一个可以拖放的视图。

将顶视图中的软件 – Microsoft – Office – Outlook – 加载项 – 加载项名称拖动到底视图的HKEY_CURRENT_USER下。

此视图最初将显示开发路径。您必须将Manifest条目编辑为[INSTALLDIR],以便它可以使用安装路径运行。(查看上一张图片。)

关注点

有关更多信息,请参阅:

一些故障排除技巧

历史

  • 2014 年 12 月 22 日 - 初始发布
© . All rights reserved.