c# - How to get the window handle for a VBA MsgBox? -


i'm running c# script on large number of excel workbooks involves calling macro in each workbook; macro produces msgbox because of error handler, , pauses execution of script until click "ok" in msgbox.

the title text of msgbox "error in processsub", , main text "error (type mismatch)".

i thought maybe have concurrent thread finds open windows, , if finds msgbox, clicks "ok". i'm trying find window using this:

using system.diagnostics; public process geterrorwindow()     {         process[] processlist = process.getprocesses();         foreach (process process in processlist)         {              if (process.mainwindowtitle=="error in processsub")             {                 return process;             }         }      } 

but doesn't find anything. when through processlist[], seems find main excel window, , not of child windows vba code produces. there way find msgbox , click ok button?

you can use winapi function findwindow retrieve handle of window title , class. add following code program:

[dllimport("user32.dll", setlasterror = true)] private static extern intptr findwindow(string lpclassname, string lpwindowname);  [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] private static extern bool setforegroundwindow(intptr hwnd);  public static intptr findexcelerrorpopup() {     return findwindow(null, "error in processsub"); } 

to click button:

intptr hwnd = findexcelerrorpopup(); if (hwnd != intptr.zero) {     setforegroundwindow(hwnd); // activates window     sendkeys.sendwait("{enter}"); // send enter key } 

if default button not "ok", send tab strokes select before enter. don't forget put using system.runtime.interopservices; dllimport.

edit: remote desktop try native method:

[dllimport("user32.dll")] private static extern void keybd_event(keys bvk, byte bscan, uint dwflags, uintptr dwextrainfo);  private const uint keyeventf_extendedkey = 0x0001; private const uint keyeventf_keyup = 0x0002; const uint keyeventf_extendedkey = 0x0001; const uint keyeventf_keyup = 0x0002; 

and raise keys this:

keybd_event(keys.enter, 0x45, keyeventf_extendedkey, uintptr.zero); // key down keybd_event(keys.enter, 0x45, keyeventf_extendedkey | keyeventf_keyup, uintptr.zero); // key 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -