Triggering web form controls from vb6-VBForums
Results 1 to 12 of 12

Thread: Triggering web form controls from vb6

  1. #1
    A-Team99
    Guest

    Post Triggering web form controls from vb6

    Hello everyone. I'm new to these forums for Visual Basic, and I must say, I am impressed with the knowledge and help available here.

    I program in VB6, and I am a semi-experienced programmer. I have some projects I have coming up that I need to find a way to use web pages in a certain way.

    The programs will be much like a standard database program, with a built-in web browser. What I need to be able to do is "trigger" things on pages that are loaded into the web browser progrmatically. So, for instance, if there is a page with a form and a Submit button on it, I need to be able to have my program fill in values in the form boxes (I know a way to do this) and then trigger the Submit button, as if the user clicked on it, but without the user having to manually do that.

    The essential idea would be to load the HTML file into a text box, "fill in" the various values for the boxes, write that to a new (temp) file, then load it into the web browser and "trigger" the submit button remotely.

    I am trying to automate some processes this way, and dont have the ability to swich to other platforms like .NET. I need a way in VB6, something clear and simple. I know little of ASP or anything like that, but I am good at parsing HTML with VB, so that is representative of my skill level.

    Thank you for any help anyone can provide on this.

  2. #2
    rskelsey
    Guest

    Smile

    It looks like you are basically asking the same question I posted. I use the WebBrowser control to get the HTML pages and the Document property to get at the HTML.

    How are you able to fill in the form in the HTML page? If you can do that, you should be able to click a button (see my post "Acting on HTML objects within the WebBrowser Control " - posted @ 11:00 on 4/11).

    Any advice would be greatly appreciated.

  3. #3
    A-Team99
    Guest

    Suggestions

    Hey I actually did find your post as well.

    Filling in the form is relatively easy, just tedious because you have to string-parse the HTML file. I do it by downloading the HTML file to a temp file, then load it into a text box control, preferably one like the Rich Text control so it supports large data size. Then, you use the Instr function to find the various areas of the page.

    For most web-form controls, there is a "value" field that contains the initial setting for that field. Most forms have a value of "" for most of these fields. What you can do is (its a bit time consuming but it works) use that instr function to isolate each form control, find it's value field in the code, then insert the info you want between the quotes. Basically, once you find the quotes for the value for a field, you set the selstart to the byte after the first quote (which puts it between the quotes) and set the sellength to 0. Then, you set the seltext to the new text and it will effectively "insert" exactly as if you had hilighted a range of text and C&P over it.

    Once you have filled the fields, you save the file out to another temp file name, then load it into the web browser proper. What you have effectively done is rewrite the HTML file with the default values for those fields filled in with what you want them to be. Then all you have to do is get that damn Submit button to work programatically, like I am trying to do. <G>

    I'm generalizing a bit here, its many steps to do, but its do-able, I have done it several times.

  4. #4
    rskelsey
    Guest
    You gave me a wonderful idea. I don't know if this is workable for your application but it is an easier way to fill in your input fields without having to use files:

    Again, I'm using the WebBrowser control so all of the HTML information is available through the document property.

    This is what the HTML tag looks like for the www.google.com search field:

    <input maxLength=256 size=55 name=q value="">

    Let's say I've got a combo box named cboData

    The following code will look for an INPUT tag with name = "q". When it finds it, the current value of the combo box will be put into the search field.


    Set docCurrentHTML = MyBrowser.Document.documentElement.All

    For Each Item In docCurrentHTML

    If Item.tagName = "INPUT" Then
    If Item.Name = "q" Then
    Item.Value = cboData.Text
    Exit For
    End If
    End If

    Next Item

    You'll see your data in the Search field and can now click the Search button (manually) and search on your data.

    I tried this (not on Google but on a site I'm working with) and it works. Now to handle the button!!

    Do you know how to get these postings to format correctly?


  5. #5
    A-Team99
    Guest

    nice job!

    Dude, that is an EXCELLENT way to do things. I think I will use that instead, as its tons easier to directly fill a specific box. It also neatly solves the problem of going from one page to the next, ala submission forms that are multi-page, without losing or resetting your "Session info" from page to page. We need that submit button working to make it happen though!<G>

  6. #6
    rskelsey
    Guest
    Okay. Here's how to make the whole thing work (button included!):

    1. Start a new Project.

    2. In Project/Components check "Microsoft Internet Controls". This will give you access to the WebBrowser object.

    3. In Project/References check "Microsoft HTML Object Library". This will allow you to create a usable button.

    4. Add a ComboBox, a Command Button and a WebBrowser control to a blank Form. For this example, leave the names of the objects as defaulted.

    Paste the following code in the default (Form) module (I don't know how to format the posting. I apologize for the lack of indenting):

    --------------------------------------------------------------------------------------------------------------------
    Private Sub Combo1_Click()

    ' The user selected data from the combo box.
    ' Put it into the Google Search field and do a search
    '
    Dim htmlSearchButton As HTMLButtonElement

    Set docCurrentHTML = WebBrowser1.Document.documentElement.All

    For Each Item In docCurrentHTML

    If Item.tagName = "INPUT" Then
    If Item.Name = "q" Then
    Item.Value = Combo1.Text
    Exit For
    End If
    End If

    Next Item

    ' The data is in. Now find the button and click it

    For Each Item In docCurrentHTML

    If Item.tagName = "INPUT" Then
    If Item.Value = "Google Search" Then
    Set htmlSearchButton = Item
    htmlSearchButton.Click
    Exit For
    End If
    End If

    Next Item

    End Sub

    Private Sub Command1_Click()

    WebBrowser1.GoBack

    End Sub

    Private Sub Form_Load()

    Combo1.AddItem "VB WebBrowser"
    Combo1.AddItem "VB Help"
    Combo1.AddItem "VB Internet"
    Combo1.Text = "VB WebBrowser"

    WebBrowser1.Navigate "www.google.com", navNoReadFromCache

    End Sub
    --------------------------------------------------------------------------------------------------------------------

    When you run this, you will navigate to www.google.com.

    Select one of the items in the combobox. This will trigger the Combo1_Click() routine which does the following:

    It finds the Search field (based on Google's HTML code) and copies your selection into that field.

    It then locates the button and creates an HTMLButtonElement that is tied to the page.

    Now it's a simple matter of clicking the button by using the "click" method of the button.

    The Command Button is used as a Back button.


  7. #7
    Lively Member
    Join Date
    Mar 2001
    Location
    Graduate Office (aka "Pit of Despair")
    Posts
    88
    Thanks, rskelsey and A-Team99!

    One side note about your code. It appears that "NavNoReadFromCache" is not yet implemented? By removing this variable from the code, all else works as promised.

    You've saved me a lot of trouble.
    - Kronix


    "I have not failed. I have merely found 10,000 ways that won't work." - Thomas Edison

  8. #8
    rskelsey
    Guest
    When I use Option Explicit, I get an error for "NavNoReadFromCache". If you look deeper you'll discover that the its value is 4. So I've been putting a 4 in its place (I don't want to waste time using cache, you may have other intentions).

    As for whether it's actually working or not, I haven't bothered to check to see it the Cache is being used.

  9. #9
    Lively Member
    Join Date
    Mar 2001
    Location
    Graduate Office (aka "Pit of Despair")
    Posts
    88
    Aha, thanks for the explanation. I was actually disappointed it didn't work. I'll try it with a 4, and we'll see.
    - Kronix


    "I have not failed. I have merely found 10,000 ways that won't work." - Thomas Edison

  10. #10
    Lively Member
    Join Date
    May 2003
    Posts
    78
    The Code that u guys provided is great but how do I use it in VB.net. It won't let me have things like Item and doesn't let me do the set bit.

    Does anyone know how to change this:
    Private Sub Combo1_Click()

    ' The user selected data from the combo box.
    ' Put it into the Google Search field and do a search
    '
    Dim htmlSearchButton As HTMLButtonElement

    Set docCurrentHTML = WebBrowser1.Document.documentElement.All

    For Each Item In docCurrentHTML

    If Item.tagName = "INPUT" Then
    If Item.Name = "q" Then
    Item.Value = Combo1.Text
    Exit For
    End If
    End If

    Next Item

    ' The data is in. Now find the button and click it

    For Each Item In docCurrentHTML

    If Item.tagName = "INPUT" Then
    If Item.Value = "Google Search" Then
    Set htmlSearchButton = Item
    htmlSearchButton.Click
    Exit For
    End If
    End If

    Next Item

    End Sub


    into usable VB.net code

    Could anyone tell me what Item could bbe replaced by


    Cheers

  11. #11
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Ok, guys.
    It's not really nessesary to go through the loop to fill fields or click on Subbmit button.
    I think you know what is the name for each text box object.
    Just use this:

    Code:
    WebBrowser1.document.all.item("TextBoxName").value="Hello!"
    to click on submit button:

    Code:
    WebBrowser1.document.item("Submit").Click

  12. #12
    Lively Member
    Join Date
    May 2003
    Posts
    78
    Cheers for the reply, but the whole point is (for me anyway) I don't know what the names of the fields are.

    The code is fine and it works in VB6 but in VB.net (following the instructions in the

    "Okay. Here's how to make the whole thing work (button included!):

    1. Start a new Project.

    2. In Project/Components ..........................................."

    I have a promblem that this bit:

    "Set docCurrentHTML = WebBrowser1.Document.documentElement.All
    "

    VB.net doesnt allow me to use "Set"

    And it also doesn't recogonise the word "Item"

    So how can I replace these, Any ideas?

    Cheers

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.