3

I need to go through hundreds of Excel files to find those that have macros, using a C# client.

I have tried using the code in the accepted answer here:

Using Interop.Excel to check if Excel file contains VBA Macros

However, this code opens the file first. Many of the files are opening pop up Windows and VBA error messages even after adding _appExcel.DisplayAlerts = false

Its also very slow. Is there a better way to go about this, ideally without opening the Excel file first?

I have looked for documentation on MSDN for Excel interop here: HasVBProject but there are no examples I can work with.

3
  • 1
    Are those .XLS or .XLSM ? Commented Jan 13, 2017 at 15:29
  • There are XLSM and XLSB files.cheers Commented Jan 13, 2017 at 16:57
  • Too bad there are XLSB. XLSM are simply .zip files, you could work directly on that... Commented Jan 13, 2017 at 17:44

2 Answers 2

0

try this

f = Dir("*.xls*")
While f <> ""
On Error Resume Next
    Set wb = Workbooks.Open(f)
    If wb.HasVBProject Then
       Debug.Print wb.Name & " has macro"
    Else
        Debug.Print wb.Name & "does not have macro"
    End If
    wb.Close
    f = Dir
Wend
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks but this looks like VBA.im using a C# client to enumerate the files.
0

if workbook events is the problem than you can disable all macros using Interop.Excel namespace via AutomationSecurity property :

MyExcel.Application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

1 Comment

Thanks I'll give it a try.

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.