Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can i fill the fields of a web form from VB code? 1

Status
Not open for further replies.

dimsis

Programmer
Aug 6, 2000
76
0
0
GR
I want to automatically fill all the fields of a specific form (using the form name) loaded in a browser (IE, Firefox etc) with predifined values, from Visual Basic 6.
Is it possible? any example?
 
I wouldn't have thought so, unless either:

1 / You'll settle on just one browser, which you probably can't do, or

2/ The VB is ran on the server, by another script in whatever that generates the page.
 
First of all mmilan thanks for answering,
what i want to do is to create an .exe that runs from the client and it just fill's some form form of an already opened browser window of the client.
This means that the client has open for example IE, and he is in a url, for example: and in that page there is a form with 5 different fields.
I want to fill the text and textarea on this page with my values with a simple click on a VB form button.

I don't want the vb application to use the webrowser control to navigate2 the site and then wait until it loads and fill the form in the webbrowser, i just want to send some values to another window ... (findwindow with IE according title and sendmessage to specific text field names i guess)

Thanks in advance.
 
I have some stuff like this in the pass my using session variables.
You will have to reading the information from the activex and then store it in session.

Just a suggestion
 
I don't think there is a need for activex or session management.
In theory if you find the "taget" window, for example with the findwindow API searching by it's title and give it the focus and then use sendmessage API to send text to the specific form fields.
 
Finding the target window depends rather a lot on knowing which browser the user is running. IE has market dominance at the moment (90%ish), but this is falling rapidly because of all the security issues with it.

If you don't know the browser, how do you proceed?

Even if you do know the browser (presumably the default browser sits in the registry somewhere), you're still left with having to write code for every browser on the planet - good luck.

mmilan
mmilan.
 
As i already wrote, i need this to run in a client's PC. This means that i can tell the client what requirements are needed to run the application and that's inlcudes IE as the browser.
But even if someone want to make something like what i need compartible with "every browser on the planet", the code could ENUMERATE (list) all opened windows, and search for a specific FORM name or even specific FORM FIELD name to fill.
I think this this could work with "every browser on the planet" (even if this is what I need).
 
.. (even if this is NOT what I need)...
 
Okay - even if I knew for sure that the client was using IE, I'm not entirely sure how I would go about doing this. There is a tool called Spy that comes with Visual Studio that might be able to get the control handles, but I would reluctant to simply assume that they never change.

This particular side of the API is not something I claim to be an expert in, but there are other members of the forum (Strongm, DrJavaJoe etc.) who are a good deal more knowledgeable on this than I.

Are you writing this for a specific organisation in which you control the browser, or is this intended for public release. If the latter, think carefully about what you're doing. If I saw some sales literature for a product that required me to use IE instead of my browser of choice, it would be in the bin a few seconds later...
 
Thanks for the answer mmilan,

if i write something that can help other users i always publish it as a freeware as you can see at (and all other software listed in the left column). I developed FIR because i needed a fast method to right click an Internet Explorer image, and have it's resized thumbnail without trouble and use it to my sites. I've never find anything so simple to the public domain, so i've develop it and "through" it as freeware (even if it has it's bugs) with the thought that someone might need it...
All (or most) of the code i'm writting in VB is mostly for personal use or to give an extra tool to a client to make his/her work faster. First i'm looking if there is already a freeware utility or code that makes what i need, and then if i can not find what i need i'm developing it.
Very often i ask about help or code examples to forum like this one because i don't have a lot of spare time.
If i accomplish what i'm trying to do, i'll publish it.
What i'm trying to do in the simpliest explanation is an "form autofill" but not from the browser's window, instead i want to do this from a Visual Basic form / application
 
Hi did you find a solution to this prob as I have done something similar recently
 
No i have 'nt 5ilver5aracen.
If you do find anything please post it here too...
 
Hello, I just wanted to interject a thought here. Ya know when you go to Google, and you type a search into the searching bar and click "Search"? Well, if you look up at the address bar once Google has provided you with it's results, then you will find your search terms listed there, and subsequently the terms are listed in the search box. With that said, a programmer, such as yourself, could do the revese (i.e. put the search terms in the URL of the initial Navigate funtion). When, and if, you do this, you will see that the search terms magically show up in the search box of Google. Now, I am not familiar with asp, but wouldn't it be worth a try to do something like this and see what happens? For instance, with your regular browser, not your program, go to the site in question and populate the boxes which you want to populate, then see what shows up in the address bar after, or during, the transition of the web page. If it turns out that it is much like I explained, then you could potentially write your code to accomodate this.

Also, if you can contact the webmaster of that site, perhaps they can provide you with the ins and outs of doing this if my suggestion does not work out for you. I have a program that does something similar to what you are asking here. I use it to snipe bids on Ebay. Ebay, if I remember corectly, has a SDK for developers to do things like this. Just a suggestion...


LF
 
Thanks for the suggestion Homersim2600,

i don't think this is what i'm trying to do.
I just want to do what google's toolbar AUTOFILL does, but instead of clicking from it's toolbar, i want to click an Autofill button from my visual basic form and fill the "target" IE window (form).

(and except this i don't want to use the google's toolbar or the webrowser control. To highlight the search result into a web page is just an one line javascript code)

Thanx again for your suggestion.
 
Sure you can...

There have been several post lately that you can put together and do this... (not sure about with firefox... but you can with IE)

In VB you Have the WebBrowser control under Microsoft Internet Controls

As strongm has pointed out in several places, this is a powerful tool...

in this thread... thread222-962137
he shows how to loop through the Browser Windows until you find the one you are looking for...
strongm said:
Or, assuming that you know the hWnd:
Code:
' Requires reference to:
' Microsoft Shell Controls and Automation
' Microsoft Internet Controls
Private Function GetWindowsExplorerWindowFolder(hWnd As Long) As String
    Dim myShell As Shell
    Dim myExplorerWindow As WebBrowser
    Set myShell = New Shell
    
    For Each myExplorerWindow In myShell.Windows
    On Error Resume Next
        If myExplorerWindow.hWnd = hWnd Then
            If Err = 0 Then GetWindowsExplorerWindowFolder = myExplorerWindow.Document.Folder.Self.Path
        End If
    On Error GoTo 0
    Next

End Function

In the same thread,
I show how to get the Window hWnd needed by strongm's method by draging a target (from a picturebox, or whatever) onto a window...


After you get the Browser Object set to the IE window, you just need to get the Document from it...

It helps if you have a reference set to...
Microsoft HTML Object Library

Then with a slight modification to strongm's code you can use this...

Module Code...
Code:
' Requires reference to:
' Microsoft Shell Controls and Automation
' Microsoft Internet Controls
' Microsoft HTML Object Library
Option Explicit
Private Type POINTAPI
  X As Long
  Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetAncestor Lib "user32" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long

Public Function GetBrowserDocument() As IHTMLDocument2
  Dim TargetHWND As Long
  Dim p As POINTAPI
  Dim myShell As Shell
  Dim myBrowserWindow As WebBrowser
  Dim myBrowserDoc As IHTMLDocument2

  GetCursorPos p
  TargetHWND = GetAncestor(WindowFromPoint(p.X, p.Y), 2)

  Set myShell = New Shell
   
  For Each myBrowserWindow In myShell.Windows
  On Error Resume Next
    If myBrowserWindow.hwnd = TargetHWND Then
      If Err = 0 Then Set myBrowserDoc = myBrowserWindow.Document
    End If
  On Error GoTo 0
  Next
  Set GetBrowserDocument = myBrowserDoc
End Function

You can then set up a picture box or something to drag from and get the HTML document... then use it Just as you do with vbscript in a webpage...

Here is an example of how to get the Source for a Window...

Form Code...
Code:
Dim TargetDoc As IHTMLDocument2

'Selects the Explorer Window's Document
'Requires:
'  PictureBox (Picture1) [Drag From this box onto an explorer window to select]
'*Note: Picture Box turns blue when window is selected...
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button Then
    Set TargetDoc = GetBrowserDocument()
    If TargetDoc Is Nothing Then
      Picture1.BackColor = vbButtonFace
    Else
      Picture1.BackColor = vbBlue
    End If
  End If
End Sub

'Get HTML Source
'Requires:
'  CommandButton (Command1)
'  TextBox (Text1) [Multiline w/ scrollbars]
Private Sub Command1_Click()
  Dim elem As IHTMLElement
  If Not TargetDoc Is Nothing Then
    Text1 = TargetDoc.body.innerHTML
  End If
End Sub

'Loop through Tags example
'Requires:
'  CommandButton (Command2)
'  ListBox (List1)
Private Sub Command2_Click()
  Dim elem As IHTMLElement
  If Not TargetDoc Is Nothing Then
    List1.Clear
    For Each elem In TargetDoc.All
      On Error Resume Next
      List1.AddItem elem.tagName & "  ::  " & elem.Name & "  ::  " & elem.Value    'txt.Value
      On Error GoTo 0
    Next
  End If
  Beep
End Sub

Well, That should get you started...

Hope this Helps...

Let me know if you have any questions about the posted code ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
btw...
mmilan said:
Okay - even if I knew for sure that the client was using IE, I'm not entirely sure how I would go about doing this. There is a tool called Spy that comes with Visual Studio that might be able to get the control handles, but I would reluctant to simply assume that they never change.

>> There is a tool called Spy that comes with Visual Studio that might be able to get the control handles

The code I posted above is something that I came up with (with help from various post on this forum) to simulate the functionality of Spy++ (where you drag the target from the picturebox to get the hWnd aka Window Handle... You can use a few line objects and make a cross hair if you want...)

>> but I would reluctant to simply assume that they never change.

The handles are unique for every control on every window that is created... so if this answers the underlying question... Yes, they do change, in a sense, but the specific handle for a window does not change, unless it is deleted and recreated...

Also... FYI...
The code I posted above, specifically the part about returning the source html, is good when a window blocks you from right clicking or seeing the source ;-)

With a small modification, you could return the WebBrowser object, instead of just the document, and also have access to the URL and anything else included in the Object...

this link: (originally posted by ZmrAbdulla on thread222-970270)
May also prove useful... if you want to handle the WebPages' Events in VB ;-)

If you find any of the info I posted above useful, please also visit the threads listed and thank the members I referred to that helpped me reach this solution :)
And thanks again guys!!!

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thank you all for your answers,

CubeE101 as i wrote in a previous message i don't want to use the webrowser control.

In your second post, you got what i'm trying to do.
I've already tried the SPY tool, but as i remember it had the same handle for ALL textboxes and textareas.
What i really need is to find a method to fill in a text box as i could use via Javascript - but from VB, for example:

document.form-name.field-name.value='my text'

this means that i must get the form's name and all form fields names via code .... and also find a way to PASS the data to the specific form fields.

I'm sure it can be done without using the webrowser control, perhapse by using some API calls... but this is what i need and searching for.

Regards.

 
The second post is about the first one ;-)

Read it again, or better yet, Follow the instructions and place the code in a project and try it out ;-)

The webBrowser control is not Visible in VB, VB uses it to control external IE windows...

Trust me, just give it a try...

*Note: it uses the WebBrowser REFERENCE, not the Component...

The only thing visible on the form is:
CommandButton (Command1)
CommandButton (Command2)
TextBox (Text1) [Multiline w/ scrollbars]
ListBox (List1)
PictureBox (Picture1) [Drag From this box onto an explorer window to select]
*Note: Picture Box turns blue when window is selected...

Then you go to Project>References... and add the following references...
Microsoft Shell Controls and Automation
Microsoft Internet Controls
Microsoft HTML Object Library



Then as StrongM suggested on this thread: thread222-972053
You can use this to execute javascript...
strongm said:
Ah - this is javascript, isn't it. probably requires a semicolon:

WebBrowser1.document.parentWindow.execScript "makeSelection(5);"

Code:
TargetDoc.parentWindow.execScript "document.form-name.field-name.value='my text';"

Or you can use the DOM in VB to get and set values for the Tags...
Code:
TargetDoc.form-name.field-name.value="my text"
or
Code:
TargetDoc.Forms("form-name").Item("field-name").Value = "my text"

Here is the code to Insert the text into the google search string and click submit... (after you select the Google Window by draging from the picture box to the Explorer win...)

Code:
'Inject search for 'VB Tutorial' into google window and submit
Private Sub Command3_Click()
  Dim elem As IHTMLElement
  If Not TargetDoc Is Nothing Then
    TargetDoc.Forms("f").Item("q").Value = "vb tutorial"
    TargetDoc.Forms("f").Item("btnG").Click
  End If
End Sub

Hope this helps ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
When you are working with html Forms, and need to know the names and types of their controls... here is some code that you can replace the Command1_Click() code above with...

Code:
'Get Form Control Data
'Requires:
'  CommandButton (Command1)
'  TextBox (Text1) [Multiline w/ scrollbars]
Private Sub Command1_Click()
  Dim elem As IHTMLElement
  Dim elem2 As IHTMLElement
  Dim frm As IHTMLFormElement
  If Not TargetDoc Is Nothing Then
    Text1 = ""
    For Each frm In TargetDoc.Forms
      Text1 = Text1 & frm.Name & " <FORM>" & vbCrLf
      For Each elem In frm
        Text1 = Text1 & "   " & elem.getAttribute("Name") & " <" & elem.tagName & "> = " & elem.getAttribute("Value") & vbCrLf
        If LCase(elem.tagName) = "select" Then
          For Each elem2 In elem.children
            Text1 = Text1 & "      " & elem2.innerText & " <" & elem2.tagName & ">" & vbCrLf
          Next
        End If
      Next
    Next
  End If
End Sub

here are a few examples of the output...
Google window selected...
Code:
f <FORM>
   hl <INPUT> = en
   q <INPUT> = search string goes here
   btnG <INPUT> = Google Search
   btnI <INPUT> = I'm Feeling Lucky

Tek-Tips window selected...
Code:
 <FORM>
   Type <SELECT> = Forum
      Find A Forum <OPTION>
      Search Posts (Keyword) <OPTION>
      Thread Number <OPTION>
      Threads by Handle <OPTION>
      Search FAQs (Keyword) <OPTION>
      Search Links (Keyword) <OPTION>
      Find An Expert <OPTION>
   SearchString <INPUT> = 
getquestion <FORM>
   QID <INPUT> = 934916
   Userid <INPUT> = 350850
   Topic <INPUT> = Can i fill the fields of a web form from VB code?
   pid <INPUT> = 222
   page <INPUT> = 1
   post <TEXTAREA> = When you are working with html Forms, and need to know the names and types of their controls... here is some code that you can replace the Command1_Click() code above with... [b](the rest of the text I was typing here)[/b]
   NotifyMe <INPUT> = 1
   ProcessEmot <INPUT> = 1
   ProcessTGML <INPUT> = 1
   showSig <INPUT> = 1
   SubReply <INPUT> = Preview Post
   SubReply <INPUT> = Submit Post

the format is:
name <type> = value

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
I may be missing something here, but why wait for the page to load and then send the data? I am assuming that the page is the same and that you know the fields that require filling in.
If that is the case, then I agree with Homersim2600.
You could do a "post" and send the data to the relevant page, and then have the page load up with the fields filled in.
See Hope this helps
[bigcheeks]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top