2

Outside the out-of-the-box MS VBOs available with BluePrism. Has anyone successfully managed to import/reference (COM) Office Interop objects (Word, Excel, Outlook, etc...) for use in BluePrism code stages? Please, tell how?

I note there was a similar question but it was not successfully answered: DLLImportAttribute C# - Outlook.

Badda_Bing
  • 640
  • 3
  • 20

1 Answers1

2

Please have a look at my answer to the question you are referencing, that should give you and idea on how to do that using VB.NET.

If you insist on doing it with C#, you need to ensure that you add a namespace and reference to the object's Code Options (my path just points to somewhere in GAC, you should copy the DLL somewhere else):

enter image description here

Then you can use the objects from the DLL within code stages:

Application app = new Application(){
    Visible = true
};

Workbook wb = app.Workbooks.Add();
Worksheet ws = (Worksheet)wb.Worksheets.Add();
ws.Visible = XlSheetVisibility.xlSheetVisible;

As you can see, you can also use the named enums, but just like in C# (and unlike in VBA) you need to specify it fully (XlSheetVisibility.xlSheetVisible instead of just xlSheetVisible)

EDIT: The benefit of VB.NET is that you can use the CreateObject() method to interact with the Excel Interop DLL without explicitly specifying the path to that DLL. This is useful if you do not have the option to store the DLL to a location common to all Resource PCs. If you can, however, then the C# approach will cause you much less headache.

And as for being able to refer to Excel objects with the Excel "prefix" (for example: Excel.Workbook instead of just Workbook) you need to specify an alias for your Excel namespace. That you can do like this:

enter image description here

Afterwards your code will work:

var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbooks books = excelApp.Workbooks;

This is actually not a bad approach as some of the Excel classes may have the same name as other .NET classes. I believe I had to do this as well because of Table or Document or something...

Marek Stejskal
  • 2,698
  • 1
  • 20
  • 30
  • Ah I see, so it needs to be referenced from its root directory in any case. I have to write custom stages in c# because that's what my team agreed on for consistency, but I'm glad to see you've covered that in your answer. I would need to scale this up for use on VM Citrix desktops, but I think I get where I am going now and that is the right answer. Thanks Marek. – Badda_Bing Feb 20 '19 at 15:03
  • So if I tried: var excelApp = new Excel.Application(); excelApp.Visible = true; Excel.Workbooks books = excelApp.Workbooks; Excel.Workbook sheet = books.Open(mySheet); I still need to use XlSheetVisibility.xlSheetVisible? – Badda_Bing Feb 20 '19 at 15:05
  • 1
    I edited my answer with some more details. But you are correct, if you have the option to store the DLL at a common location, you are safe to use this approach. – Marek Stejskal Feb 20 '19 at 15:21
  • 1
    And regarding the enums, yes, you'll always need to specify the full name (or use a numerical value). – Marek Stejskal Feb 20 '19 at 15:23