0

我有一个功能区按钮,我想将邮件正文、主题和发件人电子邮件地址存储到三个单独的字符串变量中。到目前为止我一直在处理的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Outlook=Microsoft.Office.Interop.Outlook;
using office = Microsoft.Office.Core;
using System.IO;

namespace OutlookAddIn1
{
    public partial class Ribbon2
    {
        string s1,s2,s3;

        private void Ribbon2_Load(object sender, RibbonUIEventArgs e)
        {

        }

        void extract(string s1, string s2, string s3)
        {
            string Body,address,subject;
            Outlook._Application oApp = new Outlook.Application();
            if (oApp.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = oApp.ActiveExplorer().Selection[1];

                if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
                    subject = mailItem.Subject;
                    address = mailItem.SenderEmailAddress;
                    Body = mailItem.Body;
                    s1 = Body;
                    s2 = address;
                    s3 = subject;
                }
            }

        }
        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            extract(s1,s2,s3);
            System.Windows.Forms.MessageBox.Show(s1);
            System.Windows.Forms.MessageBox.Show(s2);
            System.Windows.Forms.MessageBox.Show(s3);
        }

    }
}

但是消息框显示为空。

4

1 回答 1

1

您需要out在方法中使用关键字extract。您在方法中的本地版本s1, s2, s3隐藏了全局版本,这是您试图在 MessageBoxes 中显示的全局变量。

namespace OutlookAddIn1
{
    public partial class Ribbon2
    {
        private void Ribbon2_Load(object sender, RibbonUIEventArgs e) { }

        void extract(out String s1, out String s2, out String s3)
        {
            String s1 = String.Empty, s2 = String.Empty, s3 = String.Empty;
            String Body,address,subject;
            Outlook._Application oApp = new Outlook.Application();
            if (oApp.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = oApp.ActiveExplorer().Selection[1];

                if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
                    subject = mailItem.Subject;
                    address = mailItem.SenderEmailAddress;
                    Body = mailItem.Body;
                    s1 = Body;
                    s2 = address;
                    s3 = subject;
                }
            }
        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            String s1 = String.Empty, s2 = String.Empty, s3 = String.Empty;
            extract(out s1, out s2, out s3);
            System.Windows.Forms.MessageBox.Show(s1);
            System.Windows.Forms.MessageBox.Show(s2);
            System.Windows.Forms.MessageBox.Show(s3);
        }

    }
}
于 2013-06-06T10:54:07.590 回答