Consulting

Results 1 to 13 of 13

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

  1. #1

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

    How about multiple keystrokes such as TAB, TAB, and Ctrl+V?

  2. #2
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    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:

    [vba]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[/vba]

    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
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    [VBA]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[/VBA]

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  5. #5
    The code below opens the website and transfers data into the website form, but how do I "submit"?

    Thanks!

    [vba]
    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
    [/vba]

  6. #6

    Submit the Form

    [VBA]ie.document.getelementbyid("submit").click[/VBA]

  7. #7
    Thanks Shred Dude, that did the trick!

  8. #8
    Hi again, how would I print that internet page?

    Thanks!

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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

  10. #10
    Thanks Norie, both solutions to submit work great, but how would I PRINT that internet page?

    Thanks again.

  11. #11
    Quote Originally Posted by enrique63
    Thanks Norie, both solutions to submit work great, but how would I PRINT that internet page?

    Thanks again.
    Try this...

    [vba]ie.execwb OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER[/vba]

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

  12. #12
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  13. #13
    Thanks Shred Dude. I read the help file and to make this work I had add some constants:
    [vba]Const OLECMDID_PRINT = 6
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2

    ie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, Null, Null[/vba]

    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!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •