Solved

Word VBA - Trigger F2 whilst in textbox and main document

Posted on 2013-12-15
6
637 Views
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
     .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....
0
Comment
Question by:hj_daisy
  • 3
  • 3
6 Comments
 
LVL 2

Accepted Solution

by:
DiscGolfDad earned 500 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_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

Actual Code:

  keybd_event VK_F12, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
 
  keybd_event VK_F12, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
0
 
LVL 2

Expert Comment

by:DiscGolfDad
ID: 39720084
Oops - F2 not F12.

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

Author Comment

by:hj_daisy
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_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....
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:DiscGolfDad
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:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx
0
 

Assisted Solution

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

http://msdn.microsoft.com/en-us/library/ms927178.aspx

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

Author Closing Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
recent documents in word and Excel 5 40
vb6 connector to SQL Server 2 32
Windows 10 Modified 2 30
Protection area in Word 2 23
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now