Reputation: 11
I'm trying to send a keystroke to a third party window. Sounds simple, yes? Doesn't mean I'm getting it...
I'm working within a fairly heavily locked down system so I'm limited as to what data I can access and what programs I can use, hence why I'm using VBA and not a stand alone compiler.
The window in question identifies as "WindowsForms10.Window.8.app.0.33c0d9d" and it opens whenever a user attempts to send an email (to force users to include a protective marking on every email).
I effectively need to send either a {space} or an {enter} (yes, I'm thinking sendkeys for simplicity sake) to this window to allow full automation. As it stands, the user is having to click OK on the window with each emailed report.
The window itself does not have a visible title when it displays.
I assume it is possible to bring this window to the front by the windows handle and then send the keystroke to it, but I'm a little stumped on how to get there.
I've done some research and have cobbled together the following code which will provide me with the windows handle but that's about as far as I've managed to get. Tried tacking on a sendkeys which isn't doing anything and I'm out of ideas.
Dim DeskTophWnd As Long, WindowhWnd As Long
Dim Buff As String * 255, WindowsCaption() As String
Dim WindowsHandle() As Long
Dim lngret As Long
Dim strtext As String
ReDim WindowsCaption(0)
ReDim WindowsHandle(0)
DeskTophWnd = GetDesktopWindow
WindowhWnd = GetWindow(DeskTophWnd, 5)
Do While (WindowhWnd <> 0)
GetWindowText WindowhWnd, Buff, 255
strtext = String$(100, Chr$(0))
lngret = GetClassName(hwnd, strtext, 100)
If (Trim(Buff) <> "") And (IsWindowVisible(WindowhWnd) > False) Then
'ShowWindowAsync WindowhWnd, 0
ReDim Preserve WindowsCaption(UBound(WindowsCaption) + 1)
ReDim Preserve WindowsHandle(UBound(WindowsHandle) + 1)
WindowsCaption(UBound(WindowsCaption)) = Buff
WindowsHandle(UBound(WindowsHandle)) = WindowhWnd
End If
WindowhWnd = GetWindow(WindowhWnd, 2)
msgbox WindowhWnd
if left(buff, 31)= "Protective Markings for Outlook" then sendkeys "{ENTER}", true
Loop
'The caption of window is in WindowsCaption()
'The handle of window is in WindowsHandle()
If anyone can assist, it would be greatly appreciated.
Regards
M
Upvotes: 1
Views: 3711
Reputation: 701
Here is the code: in excel (or access) paste this into a new module; try to open the window on which you have to send the keys, run this module and then press Ctrl-G to popup the debugger; here there is the list of all the title of the windows opened in this time. Find your title and then copy here in the code in the part of CaptionWindowsString
; put your sendkeys in the statement sendkeys
and try.
You can make a try with another window (ie notepad.exe)
Option Explicit
Declare PtrSafe Function GetDesktopWindow Lib "USER32" () As LongPtr
Declare PtrSafe Function GetWindow Lib "USER32" (ByVal hwnd As LongPtr, ByVal wCmd As Long) As LongPtr
Declare PtrSafe Function GetWindowText Lib "USER32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long
Sub oleee()
Dim DeskTophWnd As LongPtr
Dim WindowhWnd As LongPtr
Dim Buff As String * 255
Dim strtext As String
Dim hwnd As Long
Dim CaptionWindowsString As String
CaptionWindowsString = "insert here the title(or part) of the window"
CaptionWindowsString = "Blocco"
DeskTophWnd = GetDesktopWindow
WindowhWnd = GetWindow(DeskTophWnd, 5)
Do While (WindowhWnd <> 0)
Buff = String$(255, Chr$(0))
GetWindowText WindowhWnd, Buff, 255
Debug.Print Buff
strtext = String$(100, Chr$(0))
WindowhWnd = GetWindow(WindowhWnd, 2)
If InStr(Buff, CaptionWindowsString) <> 0 Then
AppActivate Buff, True
DoEvents
SendKeys "inser here the string of keys to send", True
DoEvents
Exit Do
End If
Loop
End Sub
Upvotes: 2