If you perform the process using the keyboard you will note that things do not happen instantly on screen - especially the initial opening of a dialog box where the action takes place. The main problem with SendKeys is that the code usually runs faster than Windows can carry out the required activity - this is especially so if manipulating an external (not the one with the code) application or opening a dialog box or menu. We therefore need to slow the action down by using Wait statements. SendKeys also has its own "Wait" argument True or False which needs to be used correctly. One example of using it set to True is when manipulating external applications that require extra time to carry out actions. This is not always sufficient, so extra code is required.
A big key to success in this area is to break down the process into small steps. It does need a bit of trial and error to get it right. Experience shows that operators are prepared to wait a bit longer if it works. Sometimes the speed of external applications varies so much that the job is impossible using SendKeys, when using API calls with KeyUp and KeyDown work better. This area is never an exact science. See VBA Help 'SendKeys' and 'Shell' for more information.
This is some sample code to demonstrate the principle :-
'------------------------------------------
Sub SET_A3()
'- change Printer setting to A3 size
'- nb. assumes current setting is A4
Dim AltKey As String
Dim CtrlKey As String
Dim ShiftKey As String
Dim TabKey As String
Dim EnterKey As String
'--------------------------
AltKey = "%"
CtrlKey = "^"
ShiftKey = "+"
TabKey = "{TAB}"
EnterKey = "~"
'--------------------------
'- open print dialog
SendKeys AltKey & "(FP)", False
'- delay 1 second
Application.Wait Now + TimeValue("00:00:01")
'- open printer properties
SendKeys AltKey & "(R)", False
'- tab to paper size
SendKeys TabKey, False
SendKeys TabKey, False
'- change A4 to A3
SendKeys "{UP}", False
'- Enter twice
SendKeys EnterKey, False
SendKeys EnterKey, False
End Sub
'----------------------------------------------
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.