Link to home
Start Free TrialLog in
Avatar of hj_daisy
hj_daisyFlag for Afghanistan

asked on

Word VBA - Trigger F2 whilst in textbox and main document

Hello Experts...

I am using Word 2003 in a Windows 7 environment.

This is the problem.  I have a client who has a third party software that will be automatically inserting a timestamp into Word for Windows whenever the F2 function key is pressed.  I have seen it work when it is pressed manually and the cursor is in a word document or if the cursor is sitting in a textbox that is within a dialog box.  So I know that their F2 command works perfectly.

What I need to do is trigger this F2 command programmatically and not rely on the user (this part is not negotiable... it has to be done using VBA code).  And I can't put the time in myself as it is not the current time that is being inserted.

I have checked the value of the F2 keypress and it returns KeyCode  = 113.  Not sure how to use that information to trigger the F2 key.  I've tried asc(113) and chr(113) but they don't trigger F2.  I don't have the client's timestamping software so I have had to fudge it a little by creating another procedure that puts information into a variable and then I write that into the textbox or document when F2 is triggered.  This works if I press F2 'manualy' in the textbox and document but not programmatically so I know that I am not using the correct code structure.

So this is the sequence of events that I need to do:

With frmPromptBox
     .txtAnswer.setfocus
     
     This is where I need to trigger the F2 command using vba whilst the cursor is focused in the textbox because I need the external program to insert the timestamp.

    .Show
End with

The user will now see the dialog box display and the timestamp is already in the textbox.  They can now insert other information in the  textbox and will then click the OK button to close it.  I will insert the contents of the textbox into the document and close the dialog box.  That part I can do.


Now we are back in the document and I need to trigger the F2 command again as I need another timestamp to be inserted (it will be different information so I can't use the one inserted into the textbox).  I've tried using sendkeys which normally works but I get a permission denied error (70) when I use:

sendkeys "{F2}", true          

I read somewhere that sendkeys doesn't work in Windows 7 but I am sure I have used it successfully before.  Anyway, hoping for a better alternative to using Sendkeys.

I'd be really grateful to anyone who can give me the code to trigger F2 whilst in the Textbox AND whilst in the main document.  

Thank you....
ASKER CERTIFIED SOLUTION
Avatar of DiscGolfDad
DiscGolfDad
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops - F2 not F12.

Change VK_F12 to VK_F2 and it's value to &H3C
Avatar of hj_daisy

ASKER

Thank you, DiscGolfDadPo, for the code and I made your changes.  I think I have something not quite right and hoping you can spot it.  I am wondering if &H45 is incorrect?


Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
                                              ByVal bScan As Byte, _
                                              ByVal dwFlags As Long, _
                                              ByVal dwExtraInfo As Long)

Private Const VK_F2 = &H3C
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2


'This code is in my userform which just has a textbox in it called txtAnswer
Private Sub UserForm_Initialize()

   txtAnswer.Setfocus

  keybd_event VK_F2, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
  keybd_event VK_F2, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0

  'insert the informtion into the textbox
  txtanswer.text = sTime

End Sub


I have another procedure that is mapped to F2 in Word and that assigns the current time to the variable sTime and so I am wanting that to be triggered.  Then I write it into the textbox.  I have to do this because I don't have the third party program to do a proper test yet.

Public sTime as string

'this procedure is linked to F2 in Word and works when I press F2 manually
Sub F2Key()
    sTime = format(Now, "HH:MM:SS")
End Sub


I then went back and used your original code and reassigned my F2Key macro to also trigger when F12 is pressed and I got further than before.  I got the letter x inserted into my textbox but the F2Key procedure wasn't triggered.

Look forward to your advice on the above....
The &H45 is a flags parameter.  I have not seen any documentation of this API call that explains the use of that number or the use of any number except that.  Check this link for a complete explanation of the routine (sans the &H45 parameter) here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Including my own comment because the link is important to find the correct values that will work in Word for Windows.