PDA

View Full Version : What is the VBA code to enter text from a cell into a website's form?



Advice Pro
05-18-2009, 08:29 AM
How about multiple keystrokes such as TAB, TAB, and Ctrl+V?

CreganTur
05-18-2009, 09:11 AM
Welcome to the forum- it's always good to see new members.

All of your posts that I've seen so far are very, VERY non-specific, when it comes to your questions. The more detail you give us, the more we will be able to help.

In order to place data into an object on a webpage, you have to know what that object is named. Generally you can figure it out by viewing the web page's source code. Once you know the object's name, you can generally automate it, as shown in this example. It requires a reference (Tools-> Reference) to Microsoft Internet Controls.

Place the following code in a new module, place your cursor in it somewhere, and press F5 to run it:

Sub WebForm()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://zip4.usps.com/zip4/welcome.jsp"
ie.Visible = True
While ie.busy
DoEvents
Wend
ie.Document.All("address2").Value = "123 Carraige Way"
ie.Document.All("city").Value = "Shreveport"
ie.Document.All("state").Value = "CA"
ie.Document.All("zip5").Value = "9021"
End Sub

You cannot automate all controls on a webpage, especially if they are custom objects. This is a lot of guesswork and trial and error.

HTH:thumb

Kenneth Hobs
05-18-2009, 09:18 AM
Using that sort of method, SendKeys(), is not the best route. Instead, use the InternetExplorer's object model. Notice how I put a value in A1 and then used it as part of the input. A specific solution depends on your web site and what elements it is using. In your browser, select View > Source, to view some of the element names.
Sub DataStuff()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://zip4.usps.com/zip4/welcome.jsp"
ie.Visible = True
While ie.Busy
DoEvents
Wend
'ie.document.all("address2").Value = "123 Carraige Way"
Range("A1").Value = "123 Carraige Way"
ie.document.all("address2").Value = Range("A1").Value
ie.document.all("city").Value = "Shreveport"
ie.document.all("state").Value = "CA"
ie.document.all("zip5").Value = "9021"
End Sub

Norie
05-18-2009, 12:36 PM
Advice Pro

There's practically no way we can give you help with this sort of thing without further information.

As Cregan and Jim have pointed out it's specific to the webpage/site, so at least a valid URL would be needed to help.

enrique63
05-28-2010, 02:30 PM
The code below opens the website and transfers data into the website form, but how do I "submit"?

Thanks!



Sub DataStuff()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://zip4.usps.com/zip4/welcome.jsp"
ie.Visible = True
While ie.Busy
DoEvents
Wend
'ie.document.all("address2").Value = "123 Carraige Way"
Range("A1").Value = "123 Carraige Way"
ie.document.all("address2").Value = Range("A1").Value
ie.document.all("city").Value = "Shreveport"
ie.document.all("state").Value = "CA"
ie.document.all("zip5").Value = "9021"
End Sub

Shred Dude
05-28-2010, 08:34 PM
ie.document.getelementbyid("submit").click

enrique63
05-29-2010, 07:15 PM
Thanks Shred Dude, that did the trick!

enrique63
05-29-2010, 10:04 PM
Hi again, how would I print that internet page?

Thanks!

Norie
05-30-2010, 04:40 AM
Try submitting the form.


Option Explicit

Sub DataStuff()
Dim ie As Object

Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://zip4.usps.com/zip4/welcome.jsp"
ie.Visible = True
While ie.Busy
DoEvents
Wend
'ie.document.all("address2").Value = "123 Carraige Way"
ie.document.all("address2").Value = "123 Carraige Way"
ie.document.all("city").Value = "Shreveport"
ie.document.all("state").Value = "CA"
ie.document.all("zip5").Value = "9021"
ie.document.all("form1").submit

End Sub

enrique63
05-30-2010, 07:58 AM
Thanks Norie, both solutions to submit work great, but how would I PRINT that internet page?

Thanks again.

Shred Dude
05-30-2010, 08:32 AM
Thanks Norie, both solutions to submit work great, but how would I PRINT that internet page?

Thanks again. Try this...

ie.execwb OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

That will send it straight to the default printer. Read the Help file on EXECWB for other options.

Norie
05-30-2010, 10:47 AM
Do you really need to print the page?

Why not just get the data you want from it and put it on a worksheet?

You could do that for multiple queries and then print off all the results.

enrique63
05-31-2010, 07:54 AM
Thanks Shred Dude. I read the help file and to make this work I had add some constants:
Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2

ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, Null, Null

It got me thinking that if I set my default printer to Adobe PDF, I can PDF the page, but I don't know how to write text in the "Save as PDF file" dialog box that come up or how to hit the save button. Any ideas?

Norie: That's an interesting idea. How would you suggest I do this?

Thanks again!!!