Skip to main content

Targeting Sendkeys to a Particular Window using Excel VBA


Hi Everyone,

Sometimes while doing some VBA stuff, we want to target Sendkeys to a particular window which may or may not be active or present. Here is the code which will first check if the Target window is available by trying to activate it by AppActivate and then Sendkeys as desired. If Target window isn't available then you may wait until it shows up or show an error message. So Here are the code:-

1. For waiting until Windows shows up:-

       

Sub Target_Sendkeys()
    On Error Resume Next                    'Resuming next Statement on Error(5)
    Do
        Err.Clear                           'Reseting the Err
        AppActivate "Blogger"               'Put the name of title bar of Target window here.
        DoEvents                            'Avoidings hangs
    Loop While Err.Number = 5
    
    SendKeys "%{ }r", True                  'Restoring the window
    Application.Wait DateAdd("s", 1, Now)   'a halt of one sec
    SendKeys "{ESC})"                       'Cancelling Right click if windows is already restored.
    SendKeys "^t"                           'Sending Ctrl + t shortcut key.
End Sub

       
 



Remarks:-
The above code will wait for the Target window to appear (in my testing Blogger's window in the Chrome Browser) and send the Ctrl (^) + t short cut keys after activating it. You don't need to write the whole name of the Title bar as the AppActivate argument, instead you can use the some Prefix part to refer Target Window.

2. Showing a Message if Target window is not available, otherwise Sendkeys

       

Sub Target_Sendkeys_Msgbox()
    On Error Resume Next                        'Resuming next Statement on Error(5)
    AppActivate "Blogger"
    
    If Err.Number = 5 Then
        MsgBox "Target window is not available."
    Else
        SendKeys "%{ }r", True                  'Restoring the window
        Application.Wait DateAdd("s", 1, Now)   'a halt of one sec
        SendKeys "{ESC})"                       'Cancelling Right click if windows is already restored.
        SendKeys "^t"                           'Sending Ctrl + t shortcut key.
    End If
End Sub


       
 

3. How to restrict a window to show up OR Raining SendKeys (ESC) on a particular window.

Actually, the Case I am going to explain now, was an outcome of the Query which a member asked on Excel forum. Here is the Link of it.
http://www.excelforum.com/excel-programming-vba-macros/1064815-auto-refresh-the-excel-every-1-second-2.html
Actually, the member was thinking of an Auto Updating workbook opened at Main Display. This Workbook was further linked to a Shared Workbook which was updated by many users and the changes were required to be reflected on Main Display in the Real time. I took care of the requirements and was able to update the Main Display as required. But the problem was, Sometimes, the Links to Shared Workbook gets broken and the UPDATE VALUES Dialog box, got shown many times. So to Cancel that Update Values Dialog box, I researched and experimented and got rewarded with what you are reading.

Here is the code for Avoiding particular window:-

       

Sub Restrict_Window()
    Sec = 1
    On Error Resume Next
    AppActivate "Update Values"
    
    If Err.Number = 5 Then
        Application.OnTime DateAdd("s", Sec, Now), "Restrict_Window"
    Else
        SendKeys "{ESC}"
        Application.OnTime DateAdd("s", Sec, Now), "Restrict_Window"
    End If
End Sub

       
 

The above code will catch the Update Values Dialogue Box when pops up and Send an ESC Key to close the same. Actually this is an al1 time running code and needs an extra instance of Excel window to do the job. So better if you install two excel versions at the same time Or Open a separate instance of Excel (using Excel /x in the Run Command).

So it was all from my side. Now its you turn to try it.

Regards,
Vikas Gautam

Comments

  1. Vikas, Thanks for sharing these examples. I was able to use this from Excel to fill in my timesheet in Chrome.

    My technique is to SendKeys "^l" to set focus to the address bar, and then "+{TAB 30}" to shift-tab to the bottom row of the timesheet. This way it doesn't have to detect how many rows have already been entered.

    ReplyDelete

Post a Comment

Popular posts from this blog

Highlighting Duplicates across multiple sheets

Hi One and All, This time I have come up with some conditional formatting stuff. The aim is to highlight the duplicates across multiple sheets and with in the sheet as well. Assumptions:- 1. I am assuming that sheet names goes on like sheet1, sheet2, sheet3.... 2. The Target Column No. is same in all the sheets. I mean, as in the attached example, its Column A which is being targeted in both sheets.

Automation:- Sending Invitation to Meeting Using Excel VBA

Hello Everyone, In one of the previous post, I wrote about automating Sending Emails using Excel VBA. This time I have come up with a pretty similar code. The code below sends Outlook Meeting Invitations to recipients on one click. here is the Code:- Sub Send_Invite_Auto() Dim olApp As Outlook.Application Dim olApt As AppointmentItem Set olApp = New Outlook.Application 'Creating Outlook Session Set olApt = olApp.CreateItem(olAppointmentItem) 'Creating an Appointment With olApt .Subject = "Enter the subject here." 'Subject .Start = DateAdd("d", 5, Now) 'Enter Date + Time here. .Recipients.Add ("example@gmail.com") 'Recipient Name, Alias, or any other Attribute. .MeetingStatus = olMeeting 'olAppointmentItem with Meeting status olMeeting 'becom

Making Password Protected PDFs using Excel Vba and PDFtk Tool

Hi Everyone, This time, I have come up a VBA Code to generate Password protected PDFs using Excel. Actually, Excel Vba has .ExportAsFixedFormat Method to generate PDFs but this hasn't any Parameter which takes password to protect the PDFs. So I have used PDFtk Tool which provide Command Line Interface to make PDFs protected using Password. Actually, You can do various things using PDFtk Tool command line varying from creating, merging, Protecting and many other. So Download the PDFtk Tool from the following link:- Download PDFtk Tookit Here are the Steps:- 1. Install the PDFtk Toolkit. 2. Use the following code to Print or Export the Activesheet with a password.