A process is identified by filtering parent process ID numbers, when the correct process is found that should receive a keystroke. This application is an Excel workbook. A macro is run inside this application that requires the keystroke. The provider of the macro cannot change this behaviour.
This user form showing up when the macro finishes inhibits automation due to its manual input requirement.
I have tried a couple of different approaches to send the return key, but they failed. The most promising is below:
[DllImport("User32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr hWnd);
//...then at the bottom of my script
//p is the Excel process selected from a bunch of Excel workbooks
IntPtr ptrFF = p.Handle;
SetForegroundWindow(ptrFF);
SendKeys.Send("{w 3}"); //<- it falls over at this stage
The error message is:
"SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method."
If I use the
SendKeys.SendWait("{w 3}");
command then it seems all right, but the keystroke does not get to its desired destination somehow.
Do you have any idea how to find a workaround of this issue with the Excel macro either by sendkeys or other ways?