0

I have a ribbon button and I want to store the mail body text, subject and senders email address into three separate string variables. The code I have been working on so far:

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);
        }

    }
}

But the message boxes are showing empty.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You need to make use of the out keyword in your extract method. Your local versions of s1, s2, s3 in the method were hiding the global versions, and it was the globals you were trying to display in the 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);
        }

    }
}
DonBoitnott
  • 10,787
  • 6
  • 49
  • 68