Search code examples
c#blueprism

Explicit Conversion


Consider the following code:

using Microsoft.Office.Interop.Outlook;
using OutlookApp = Microsoft.Office.Interop.Outlook.Application;

namespace OutlookInterop
{
    class Program
    {
        static void Main(string[] args)
        {
            OutlookApp outlookApp = new OutlookApp();
            MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
            mailItem.To = "[email protected]";
            mailItem.Subject = "Test Email Alert";
            mailItem.HTMLBody = "<html><body>Test email body.</body></html>";
            mailItem.Display(false);
        }
    }
}  

This code works fine when I run it on Visual Studio, but when I use it in BluePrism, I get the following compiler error:

Page: Send Email Alert c#
Stage: Code1
Type: Error
Action: Validate
Description: Compiler error at line 2: Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Outlook.MailItem'. An explicit conversion exists (are you missing a cast?)
Repairable: No

I think I have referenced the correct assemblies in BluePrism i.e.:
enter image description here

And then this is the code inside the actual code stage:

    OutlookApp outlookApp = new OutlookApp();
    MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem); //this would be line 2
    mailItem.To = "[email protected]";
    mailItem.Subject = "Test Email Alert";
    mailItem.HTMLBody = "<html><body>Test email body.</body></html>";
    mailItem.Display(false);

So, why would that code work on Visual Studio but not in BluePrism? what is this cast that is missing?


Solution

  • I strongly suspect that the difference is in terms of how the Office library is imported. If you look at the properties for the reference in your Visual Studio project, I suspect it will show that the "Embed Interop Types" property is set to True. This not only embeds the interop types, but also makes methods and properties that would return object return dynamic instead - which is why the code would compile.

    I don't know whether BluePrism has an option for COM libraries like that. If it doesn't, just at an explicit cast to MailItem.