使用C#处理启用了宏的Excel文件的警报弹出对话框

Mr.*_* Go 3 c# excel vba excel-vba

我正在尝试使用c#编辑启用宏的Excel文件。

我已经完成了编辑,需要使用已经定义的宏[VBA-按钮]验证所有数据。我可以使用下面提到的代码运行宏:

workbook.Application.Run("Sheet1.validate_Click");
Run Code Online (Sandbox Code Playgroud)

现在的问题是,每当调用该宏时,该宏都会验证我在Excel工作表中插入的数据并给出如下所示的输出:

在此处输入图片说明

现在,我需要以编程方式单击“是”按钮。它将要求保存该文件,该文件将由该“验证”按钮自动创建。

我被困在这里,那我该如何以编程方式单击出现的“警报对话框”的“是”按钮。

请帮助我,我在这里一无所知,我在Google上搜索了很多,但找不到任何符合我目的的东西。

Com*_*ern 5

正如@Tom在评论中提到的那样,真正的解决方案是更改VBA代码。但是,由于这不是您可以使用的选项,因此您必须使用某种变通的解决方法。我没有做太多测试,但是我假设由于模式对话框,您的COM Interop调用也处于阻塞状态。

关于处理VBA由托管代码生成的对话框的唯一方法是,使用win32函数轮询目标窗口,然后在找到模拟鼠标时发送模拟鼠标单击。这是我使用的课程:

public class DialogClicker
{
    private delegate bool EnumWindowsProc(int hWnd, int lParam);

    private const int BM_SETSTATE = 0x00F3;
    private const int WM_LBUTTONDOWN = 0x0201;
    private const int WM_LBUTTONUP = 0x0202;

    [DllImport("user32.dll")]
    private static extern int EnumWindows(EnumWindowsProc callbackFunc, int lParam);
    [DllImport("user32.dll")]
    private static extern int EnumChildWindows(int hWnd, EnumWindowsProc callbackFunc, int lParam);
    [DllImport("user32.dll")]
    private static extern int GetWindowText(int hWnd, StringBuilder buff, int maxCount);
    [DllImport("user32.dll")]
    private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

    private const int MsgBufferSize = 256;
    private bool _textFound;
    private int _btnhWnd;
    private readonly Timer _timer;

    public string TargetHeader { get; private set; }
    public string ButtonText { get; private set; }
    public string SearchText { get; private set; }
    public int TimerInterval { get; private set; }

    public DialogClicker(string header, string button, string search, int interval)
    {
        TargetHeader = header;
        ButtonText = button;
        SearchText = search;
        TimerInterval = interval;
        _timer = new Timer(interval);
        _timer.Elapsed += ElapsedHandler;            
    }

    public void Toggle(bool active)
    {
        _timer.Enabled = active;
    }

    private void ElapsedHandler(object sender, ElapsedEventArgs e)
    {
        _btnhWnd = 0;
        _textFound = string.IsNullOrEmpty(SearchText);
        EnumWindows(EnumProc, 0);
    }

    private bool EnumProc(int hWnd, int lParam)
    {
        var heading = new StringBuilder(MsgBufferSize);
        GetWindowText(hWnd, heading, MsgBufferSize);
        var title = heading.ToString();

        if (string.IsNullOrEmpty(title) || !title.Equals(TargetHeader)) return true;
        EnumChildWindows(hWnd, EnumChildProc, 0);
        return false;
    }

    private bool EnumChildProc(int hWnd, int lParam)
    {
        var title = new StringBuilder(MsgBufferSize);
        GetWindowText(hWnd, title, MsgBufferSize);
        var text = title.ToString();

        if (string.IsNullOrEmpty(text)) return true;
        if (!_textFound) _textFound = text.Contains(SearchText);
        if (text.Equals(ButtonText)) _btnhWnd = hWnd;
        if (_btnhWnd <= 0 || !_textFound) return true;

        SendMessage(_btnhWnd, BM_SETSTATE, 1, 0);
        SendMessage(_btnhWnd, WM_LBUTTONDOWN, 0, 0);
        SendMessage(_btnhWnd, WM_LBUTTONUP, 0, 0);
        SendMessage(_btnhWnd, BM_SETSTATE, 1, 0);
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

调用代码:

var clicker = new DialogClicker("Microsoft Excel", "&Yes", "No error found in sheet.", 100);
clicker.Toggle(true);  //Start polling.

//Do whatever triggers the dialog.

clicker.Toggle(false); //Stop polling.
Run Code Online (Sandbox Code Playgroud)