0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using RDotNet;
using RDotNet.NativeLibrary;

namespace OutlookAddinSendMailToWebService
{
    public partial class ThisAddIn
{
    #region Variables
    private Outlook.Explorer activeExplorer = null;
    #endregion

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        activeExplorer = Application.ActiveExplorer();
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    //Send Message to Webservice
    public void SendMailToWebservice()
    {
        string mailTo = String.Empty;
        string mailCC = String.Empty;
        string mailSubject = String.Empty;
        string mailBody = String.Empty;
        string mail = String.Empty;

        REngine.SetEnvironmentVariables();
        // There are several options to initialize the engine, but by default the following suffice:
        REngine engine = REngine.GetInstance();

        if (activeExplorer.Selection.Count > 0)
        {
            foreach (Object selectedItem in activeExplorer.Selection)
            {
                if (selectedItem is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selectedItem as Outlook.MailItem);
                    if (mailItem != null)
                    {
                        mailCC = "CC: " + mailItem.CC;
                        mailTo = "To: " + mailItem.To;
                        mailSubject = "Subject: " + mailItem.Subject;
                        mailBody = "Body: " + mailItem.Body;
                        mail = mailTo + '\n' + mailCC + '\n' + mailSubject + '\n' + mailBody;
                        //System.Windows.Forms.MessageBox.Show(mail);

                        engine.Evaluate("load('~/key.RData')");
                        var key = engine.Evaluate("key(mail)").AsCharacter()[0];

                        engine.Evaluate("load('~/add.RData')");
                        var add = engine.Evaluate("trim(key)").AsCharacter()[0];

                        engine.Evaluate("load('~/match.RData')");
                        var mat = engine.Evaluate("match(add,key)").AsCharacter()[0];

                        System.Windows.Forms.MessageBox.Show(mat);
                    }
                }
            }
        }
    }

Iam getting error in the following lines of code.Its giving the error as argument mail is not found when iam trying to call key function.

engine.Evaluate("load('~/key.RData')");
var key=engine.Evaluate("key(mail)").AsCharacter()[0];
engine.Evaluate("load('~/add.RData')");
var add = engine.Evaluate("trim(key)").AsCharacter()[0];
engine.Evaluate("load('~/match.RData')");
var mat = engine.Evaluate("match(add,key)").AsCharacter()[0];

1 Answer 1

1

R indeed does not know about your variable "mail", it is a .NET variable.

CharacterVector mailVec = engine.CreateCharacterVector(new[] { mail });
engine.SetSymbol("mail", mailVec);

See the tutorial basic types with R.NET in the documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.