I'm creating a c# program that executes a vba macro in an excel sheet.
我正在创建一个c#程序,它在excel表中执行vba宏。
After the vba macro finishes its execution, it displays a modal window, that does not allow to continue with the execution of my program.
在vba宏完成执行之后,它将显示一个模态窗口,不允许继续执行我的程序。
Is there any way how I can close that modal window? or is it even possible??
有什么方法可以关闭模式窗口吗?还是有可能?
I have tried:
我有尝试:
appExcel.DisplayAlerts = false;
appExcel.EnableEvents = false;
but it did not work.
但这并不奏效。
NOTE: I do not have access to the vba macro code.
注意:我无法访问vba宏代码。
best regards!
最好的问候!
1 个解决方案
#1
1
Here's a "lightweight" solution modified from my answer here to a similar question that was purely VBA. It uses two Win32 API functions: FindWindow
and SendMessage
.
这里有一个“轻量级”解决方案,从我这里的答案修改为一个类似的问题,那就是纯VBA。它使用两个Win32 API函数:FindWindow和SendMessage。
[DllImport("user32.dll")] static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
// This function endlessly waits for a window with the given
// title and when found sends it a WM_CLOSE message (16)
public static void killMbox(Object windowTitle)
{
for (int h = 0; h == 0;)
{ Thread.Sleep(1000);
h = FindWindow(null, windowTitle.ToString());
if (h != 0)
SendMessage(h, 16, 0, 0);
}
}
static void Main(string[] args)
{
Thread mboxKiller = new Thread(killMbox);
// Excel message-boxes usually have title "Microsoft Excel".
// Change if your message-box has a different title
mboxKiller.Start("Microsoft Excel");
Application xlApp = new Application();
Workbook wb = xlApp.Workbooks.Open("C:/SO/SO.xlsm");
xlApp.Visible = true;
xlApp.Run("doMessageBox"); // launches a macro that displays a message box
// now the mboxKiller will close that mbox, code here proceeds
// ...
xlApp.Quit();
}
}
#1
1
Here's a "lightweight" solution modified from my answer here to a similar question that was purely VBA. It uses two Win32 API functions: FindWindow
and SendMessage
.
这里有一个“轻量级”解决方案,从我这里的答案修改为一个类似的问题,那就是纯VBA。它使用两个Win32 API函数:FindWindow和SendMessage。
[DllImport("user32.dll")] static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
// This function endlessly waits for a window with the given
// title and when found sends it a WM_CLOSE message (16)
public static void killMbox(Object windowTitle)
{
for (int h = 0; h == 0;)
{ Thread.Sleep(1000);
h = FindWindow(null, windowTitle.ToString());
if (h != 0)
SendMessage(h, 16, 0, 0);
}
}
static void Main(string[] args)
{
Thread mboxKiller = new Thread(killMbox);
// Excel message-boxes usually have title "Microsoft Excel".
// Change if your message-box has a different title
mboxKiller.Start("Microsoft Excel");
Application xlApp = new Application();
Workbook wb = xlApp.Workbooks.Open("C:/SO/SO.xlsm");
xlApp.Visible = true;
xlApp.Run("doMessageBox"); // launches a macro that displays a message box
// now the mboxKiller will close that mbox, code here proceeds
// ...
xlApp.Quit();
}
}