Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word VBA - Trigger F2 whilst in textbox and main document

Posted on 2013-12-15
6
Medium Priority
?
657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 2

Accepted Solution

by:
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_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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
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 …
Suggested Courses

718 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