Word VBA - Trigger F2 whilst in textbox and main document

Posted on 2013-12-15
Medium Priority
Last Modified: 2013-12-20
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
     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.

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....
Question by:hj_daisy
  • 3
  • 3

Accepted Solution

DiscGolfDad earned 2000 total points
ID: 39720082
Declarations section:

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_F12 = &H58
Private Const KEYEVENTF_KEYUP = &H2

Actual Code:

  keybd_event VK_F12, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0

Expert Comment

ID: 39720084
Oops - F2 not F12.

Change VK_F12 to VK_F2 and it's value to &H3C

Author Comment

ID: 39720510
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_KEYUP = &H2

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


  keybd_event VK_F2, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 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....
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 39720528
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:


Assisted Solution

hj_daisy earned 0 total points
ID: 39720567
Hi DiscGolfDad

I checked your link and can see where you got the 3C from.  Anyway, I went down a long and windy road which took me to numerous webpages as I tried to find alternative values.  I finally found it.  It's &H71.  Here is the link which might help others.


Thank you, though, for helping me with so much of the code.  Happy to award you the points.

Author Closing Comment

ID: 39731385
Including my own comment because the link is important to find the correct values that will work in Word for Windows.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
If Skype for Business came with your office 2016 or office 365 installation, you may find that it's almost impossible to either disable or remove it. The application will often launch with each start of Windows, even when explicitly configured not t…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question