Blue Prism Product

 View Only
last person joined: 10 hours ago 

This community covers the core Blue Prism RPA product.

 Is macro enabled in XL?

Paul JHM van Doorn's profile image
Paul JHM van Doorn posted 11-22-2023 09:22

Hi folks,

In one of our processes we get an XL file via email. The XL file contains a macro that has to be enabled by the sender. If not enabled, the data will be incomplete and the XL will be rejected. 

I'm trying to establish in an early stage if the macro is indeed enabled or not. The XL VBO does not provide a suitable action to check this, so I'm thinking to make a utility action that checks for this.

Being not much of a C# or VBA programmer, I wonder if anyone can point me in the direction of what code would return a 'Excel Macro enabled' flag?


------------------------------
Happy coding!
Paul
Sweden

Paul JHM van Doorn's profile image
Paul JHM van Doorn

Ok, I will conclude that this is not possible to determine in C# or VBA. Which sounds strange to me, but I haven't been able to come up with anything useful after a few hours of googling either.

----------------
Happy coding!
Paul
Sweden

Abhisekh Mukherjee's profile image
Abhisekh Mukherjee
Hi @Paul,
Can you try the below and let me know if that works for you ?
using Excel = Microsoft.Office.Interop.Excel;
 
class Program
{
    static void Main()
    {
        string filePath = "path_to_your_excel_file.xlsx";
 
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook workbook = excelApp.Workbooks.Open(filePath);
 
        if (workbook != null)
        {
            Excel.VBProject vbProject = workbook.VBProject;
            Excel.VBComponents vbComponents = vbProject.VBComponents;
 
            // Check if the workbook contains macros
            if (vbComponents.Count > 0)
            {
                // Macros are enabled
                Console.WriteLine("Macros are enabled in the file.");
            }
            else
            {
                // Macros are disabled
                Console.WriteLine("Macros are disabled in the file.");
            }
        }
 
        excelApp.Quit();
    }
}

Paul JHM van Doorn's profile image
Paul JHM van Doorn

Hi Abhisekh,

Thanks for the code. Tried to put it in a code stage but then the object complains about this:

Page: Initialise
Description: Compiler error at top section line -6: Metadata file 'Microsoft.Office.Interop.Excel.dll' could not be found

The Check Code button on the code stage shows no errors and the object is set to C#. I probably need to add some settings for Code Options in the Initialize page. I got these setting already:

Any idea?

----------------
Happy coding!
Paul
Sweden