Jon Artus
Jon Artus

Reputation: 6358

Execute VBA Macro via C# Interop?

just wondering if anyone could suggest why I might be getting an error? I'm currently trying to execute a macro in a workbook by calling the Application.Run method that the interop exposes.

It's currently throwing the following COM Exception:

{System.Runtime.InteropServices.COMException (0x800A03EC): Cannot run the macro Macro1'. 
The macro may not be available in this workbook or all macros may be disabled.

I've put the containing workbook in a trusted location, set all of the security settings to their minimum values, and trusted programmatic access to my object model. I'm entirely out of ideas and Google's failed me so far!

Has anyone done this, or can you suggest anything which I could try?

Many thanks!

Upvotes: 3

Views: 5393

Answers (4)

Swab.Jat
Swab.Jat

Reputation: 1268

I ran into same issue, Macro is stored in a "Module" named "MainModule".

I tried

App.Run("Abc.xlsm!CalcSomething");

App.Run("MainModule!CalcSomething");

App.Run("Abc.xlsm.CalcSomething");

App.Run("MainModule.CalcSomething");

App.Run("CalcSomething);

Nothing worked.

Upvotes: 0

nano
nano

Reputation: 303

Although this is already answered, Ive had the SAME symptoms. Finally, ive resolved the problem with this hotfix of Microsoft

Upvotes: 0

Jon Artus
Jon Artus

Reputation: 6358

It turned out that there were actually two identically-named macros which had been injected into the workbook. The exception above translates to a "Compile Error: Ambiguous name detected" error in VBA - I should have gone straight into VBA to look at this, rather than assuming that something odd was happening with the interop.

Upvotes: 1

Fionnuala
Fionnuala

Reputation: 91356

If the macro is stored in the personal workbook, you may have to reference the workbook name: personal.xls!macroname

Upvotes: 0

Related Questions