Running Excel VBA from C# and processing time when running VBA directly from Excel

Asked 2 years ago, Updated 2 years ago, 151 views

I'm thinking of running Excel VBA on C#.
Currently, it takes an hour to run a VBA with a button click from Excel, but the C# side uses "Microsoft.Office.Interop.Excel" to

xlApp.Run("Sheet1.Btn_Calc_Click";

Running the same macro on the takes three hours.

I've heard that Excel processing in Interop.Excel is slow, but will it affect Excel's macro execution?

Please let me know if there is any reason or way to reduce the processing speed.

c# vba excel

2022-09-30 21:37

1 Answers

I don't know if this is the cause, so it may be a wrong answer, but

Is the [STathread] attribute specified for the Main method? If not specified, the thread is initialized as a multithreaded apartment.
If you are manipulating objects from a multi-threaded apartment to a single-threaded apartment, you may need marshaling, which may delay the processing.

According to the comment, [STathread] has already been specified, but is the calling C# program a console application? Single Thread Apartment Requirements requirements to rotate a GUI or other message loop.
Wouldn't it be improved by removing [STAThread] or running it on a separate thread in a multi-threaded apartment, or by turning the message loop in some way with [STAThread] attached?


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.