cancel
Showing results for 
Search instead for 
Did you mean: 

Is macro enabled in XL?

PvD_SE
Level 12

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

Happy coding!
Paul, Sweden
(By all means, do not mark this as the best answer!)
3 REPLIES 3

PvD_SE
Level 12

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

Happy coding!
Paul, Sweden
(By all means, do not mark this as the best answer!)

abhis3kh
Level 4
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();
    }
}

PvD_SE
Level 12

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:
35941.png
Any idea?

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

Happy coding!
Paul, Sweden
(By all means, do not mark this as the best answer!)