Solved

Using VB6, need to call the "paste" event on an ihtmlelement (datetimepicker) and paste in a value

Posted on 2011-09-30
11
748 Views
Last Modified: 2012-05-12
Dear experts,

One thing I'm required to do alot in my work is navigate a web page using vb6 and fill in values on web controls, hit search buttons, etc. For the most part on standard controls I know how to do this, simply by doing something like:

Set anElem = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DatePick2_StartDatePicker_dateInput_text")
    anElem.value = Format(whichDay, "mm/dd/yyyy")

In most cases this works fine.. however, in cases like these datetimepicker controls you can set the value but when you click the search button it instantly reverts back to the original value. The only way to make it stick is to trigger events on the control such as actually pasting in a value or typing it in. As a matter of fact, in the past I've overcome this barrier by using SendKeys to the control, after setting the focus, but this is a clunky solution and I always have to check the value afterwards to see if sendkeys even put it in right. Furthermore, this takes the control of the screen so the user can't run anything else while my program is running and filling out web pages.

I need a better way so bad.

Since setting the value doesn't work I've been thinking maybe I could actually call the "paste" event on the control or something like that and pass it the value I want pasted in. Is this doable?

I'm even willing to use the keydown event and just send it the keys I want one at a time if necessary.

Go to this page if you want to see an EXAMPLE of the same kind of calendar control I'm talking about:
https://recorder.co.clark.nv.us/RecorderEcommerce/default.aspx
Then click on the "Document Type" link and you'll see that there is a Start Date control and an End Date control.

I like to use google chrome and inspect the element to see all the events on the control and all the properties and everything you could imagine. Chrome reveals that the paste event, and the keydown, keyup and keypress events all have event listeners attached to them.

How do I call these events from my code, and pass along values with them?

This is something I've been wishing I could learn for a very long time and any help on this would be greatly appreciated!

Thanks so much! ~Jeffrey
0
Comment
Question by:JeffreyDurham
  • 7
  • 3
11 Comments
 

Author Comment

by:JeffreyDurham
ID: 36894562
In addition if you can send me a short javascript example doing this I can actually fire the javascript directly off the webbrowser, I have already tested this. ~Jeffrey
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 36897048
0
 
LVL 27

Accepted Solution

by:
Ark earned 250 total points
ID: 36898189
Did you try
anElem.focus()
anElem.document.selection.createRange.execCommand "Paste"
or
anElem.createTextRange.execCommand "Paste"
0
 

Author Comment

by:JeffreyDurham
ID: 36911622
puppydogbuddy:

I read through that script and it was definitely interesting. However, instead of creating a date time picker control what I actually want to do is use javascript to set the value on one. The particular control I'm using as an example is located here..
https://recorder.co.clark.nv.us/RecorderEcommerce/default.aspx
Then click on the "Document Type" link and you'll see that there is a Start Date control and an End Date control.

The issue I'm having with this control is simply setting it's value doesn't make the value stay. It seems like the value only sticks if the value is actually typed into the control or if the date is chosen on the calendar control side (it must call some sort of event that I'm not aware of to make it stick).

Me & another office worker were trying to write a script in java that would call the events manually in the control but couldn't figure out how to create a keyboard event that would type letters into the box but that's where we left off on it. If you know how to manually create and call the keyboard events from javascript on a certain control that would help me immensely.

Thanks, Jeffrey
0
 

Author Comment

by:JeffreyDurham
ID: 36911676
Ark:

I tried this bit of code I put below based on what you told me, and I was actually suprised it called the Paste event and put my text in. That's really awesome.. :)

Sadly, once I moved the focus away from the control the text I put in it reverted so I guess I was wrong about the paste event maybe working.

Do you know how to send keys to a web control using events? I want to simulate me typing it in without using SendKeys since that is very klunky and literally most of the time doesn't even type what I tell it to. As a matter of fact, I can post the way I'm doing it now if anybody thinks it might be helpful, but I really don't like the way I'm doing it. (It uses a combination of windows API and web automation to bring the browser window to the front and set the focus on the control in question and then sendKeys to send the keystrokes..)

But yeah, using the key events might work since using SendKeys does. I can't test it though because I can't find any vb6 documentation on how to call it.
On the other hand, I thought the paste event would do it since it's a programmed event on the control.

Thanks for all your help!, Jeffrey
'Testing Experts Stuff
Dim myValue2$
myValue2 = Format(whichDay, "mm/dd/yyyy")
Dim clipboard As New MSForms.DataObject
clipboard.SetText myValue2
clipboard.PutInClipboard  'put our text into the clipboard
    
'Call the paste event on the control
Set anElem = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DatePick2_StartDatePicker_dateInput_text")
anElem.focus
anElem.document.selection.createRange.execCommand "Paste"
    
'Move focus to another control to see if the value stays (it doesn't)
Set anElem = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DatePick2_StartDatePicker_popupButton")
anElem.focus

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:JeffreyDurham
ID: 36911944
Ark:

I've been continuing to look into this and looking up all the different possible commands to use with execCommand. I found if I use the "delete" command first and then follow it by the "paste" command where I add a chr(13) to the end of the date string in the clipboard the text is staying. Now, when I click the Search button it reverts, but otherwise is staying. My only conclusion is there must be a validator routine in the java I must call to really set it.. I'm going to mess with this some more and see if I can come to a complete answer, but I think you've got me started on a path that might work if I can find the right way to convince the control to call it's validation command.

One idea I've got is to still try to send a key to the control like a tab or something and then when the focus is leaving the control hopefully it'll validate the data. I thought sending the return key at the end of my string might do that. I dunno, I'm going to use chrome to inspect the element and see if I can find another event to call or something to finalize the data.

Getting closer though.. lemme know if you have any ideas what else I might try..
Thanks! ~Jeffrey
0
 
LVL 38

Assisted Solution

by:puppydogbuddy
puppydogbuddy earned 250 total points
ID: 36911957
Jeffrey,
<The issue I'm having with this control is simply setting it's value doesn't make the value stay. It seems like the value only sticks if the value is actually typed into the control or if the date is chosen on the calendar control side (it must call some sort of event that I'm not aware of to make it stick).>

Based on what you've said above, it might be that the paste may require a refresh or requery to "make it stick". Try it and see what happens.
0
 

Author Comment

by:JeffreyDurham
ID: 36912157
puppydogbuddy:

I have learned more about this. There is another control which is invisible which if you set the value on, the search works successfully.. So I'm thinking that the validator function must set the value on this control also when you put in a good date. I'm also wondering if this is a standard calendar control made by microsoft, and if I'll always be able to set it this way.

So, I guess at this point I have a couple of working methods, but I'm still curious if I can make it work through user input on just the one field where you type in stuff.

I tried calling .Refresh and .Requery on the control (I have anElem defined as an IHTMLControl) but neither function exists.

Is this a java function I can call on the control? Can you send me a sample script on how to refresh a control on a form? Or is this something I should do through vb6?

Thanks! ~Jeffrey


'Here is the code that actually successfully sets it
Set anElem = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DatePick2_StartDatePicker_dateInput_text")
    clipboard.SetText "09/20/2011"
    clipboard.PutInClipboard
    anElem.focus
    anElem.document.selection.createRange.execCommand "Delete"
    anElem.document.selection.createRange.execCommand "Paste"

    'Set this other control that's invisible that we found that goes with this one
    Set anElem = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DatePick2_StartDatePicker_dateInput")
    anElem.value = "2011-09-20-00-00-00"

Open in new window

0
 

Author Comment

by:JeffreyDurham
ID: 36912509
PuppyDogBuddy:

Ok found the Refresh command on the execCommand() function. It caused it to revert immediatly after leaving the control but it was worth trying.

I also learned that the "blur" event means to leave the focus. I had no idea.. I was thinking if I could call the Blur event on the control once I was done pasting in data it would cause it to validate.

~Jeffrey
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 36912511
Jeffrey,
Refresh and requery are vb6 methods.  I utilize vb6 with MS Access.  I have used a javascript or two to as needed within an MS Access application, but otherwise my experience with javascript is limited.

I think you ought to revisit the javascript link I gave you.  It has a working example of selecting a date from a datepicker and storing the selection in a textbox control, which is exactly what you are saying you want to accomplish.  Here is the authors statement of what his javascript does:

"Make it easy for your visitors to fill out the date/time field(s) of your form, by selecting this info from a popup calendar."

There is nothing that says you can't substitute your own datepicker control for the control he references in his script.
0
 

Author Comment

by:JeffreyDurham
ID: 36912859
Whew.. thanks to you guys I found tons of ways of doing this, whether it was the execCommand or what. A Special thanks Ark for teaching me how to use that, I know so much more now than I used to.

This is what I ended up with since it works and is so very simple...
    'Fill in the Start Date
    Set anElem = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DatePick2_StartDatePicker_dateInput_text")
    anElem.focus
    anElem.value = Format(whichDay, "mm/dd/yyyy")
    anElem.FireEvent "onBlur"
   
Since both of you gave me ideas on where to go and how to get there I'm going to split the points. Thanks so much for all the help!

I really appreciate it, ~Jeffrey
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

10 Experts available now in Live!

Get 1:1 Help Now