Complete IE Form using DOM and VBA

joolz

New Member
Joined
Jan 27, 2008
Messages
24
Hi there,

I am looking to create a small utility which 'scrapes' data from one IE session, and pastes that to another form in a seperate IE session. The difficulty I am having is that the administrator has locked access to the source and as such I am unable to inspect the HTML code directly in order most effectively build the solution.

I am aware that the use of the DOM would prove helpful to locate the nth input field, and then sets its values. Having researched the topic somewhat using the search I am struggling to come up with a working model on best to achieve this.

I am currently concentrating on the section of code which will paste the values into the IE page containing the form. This page appears to utilise frames; a top nav bar, a left menu section and the main frame which contains the fields which need to be completed. As I do not have access to the code directly, I guess I need to be able to reference the frames by number (i.e, the third frame) and then 'guess' the input field number within the DOM tree. This is where I am really struggling.

The examples I have found online make the assumption that I know the frame name and corresponding input names. I have neither.

This code looked promising,

Code:
Dim HTMLDoc2 As MSHTML.HTMLDocument
Set HTMLDoc2 = Browser.document.frames(2).document
' Fill in values
HTMLDoc2.forms("NavPage").CNum.Value = "12345" 'will change this to become a variable
HTMLDoc2.forms("Navpage").GetCase.Click


However, I don't have the frame or form input names.

I would be grateful for any help.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I am working my way through this, having now also enabled the MSHTML and MS Internet Control libraries. I am now wondering whether I can assign an existing window to an object using the AppActivate method, like so:


Code:
Dim HTMLDoc2 As MSHTML.HTMLDocument
Set HTMLDoc2 = AppActivate "Search Request"

I do not want to open a new IE session and load the same URL, and would prefer the macro to switch to the existing IE window using the AppActivate method.

The above code currently returns an 'Compile Error: Syntax error'. however I would be grateful if anyone could help me set the object using AppActivate.
 
Upvote 0
All existing IE windows are in the Shell.Windows collection, so loop through that looking your IE window (example code on this forum). Once you have a reference to the IE object you can say:
Code:
AppActivate IE.LocationName
Set HTMLDoc2 = IE.document
 
Upvote 0
Thank you, I am a little bit closer to getting this working now.

The code which I have managed to get so far is:

Code:
Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer
    Dim objShellWindows As New SHDocVw.ShellWindows
    Dim win As Object, rv As SHDocVw.InternetExplorer
    For Each win In objShellWindows
        If TypeName(win.document) = "HTMLDocument" Then
            If UCase(win.document.Title) = UCase(sTitle) Then
                Set rv = win
                Exit For
            End If
        End If
    Next
    Set IEWindowFromTitle = rv
End Function



Private Sub CopyToSearch_Click()
Dim w As SHDocVw.InternetExplorer
Set w = IEWindowFromTitle("Google")

Dim HTMLDoc2 As MSHTML.HTMLDocument
Set HTMLDoc2 = w.document



If Not w Is Nothing Then
    Debug.Print w.document.Title
    
    ' Fill in the form
    HTMLDoc2.document.Frame(2).document.all.tags("INPUT").Item(18).Value = "TEST"

    ' Submit the form
    'HTMLDoc2.Document.forms(0).submit.click
    
    
Else
    'Log an error in the console
    Debug.Print "Not found"
    MsgBox "Please ensure Google is open."
End If


When running this, I get a 'Run time error 438. Object doesn't support this property or method'.

I thought that with HTMLDoc2 set, I could then use the code above to set the value of the text fields. I am unsure what else I need to do to try and get this working.

This is the first time I have needed to create something like this so I am grateful for your assistance.
 
Upvote 0
Use intellisense to help as you type in "HTMLDoc2." and discover that HTMLDocument doesn't have a property called "document" or "frame".

Try instead:

Code:
   ' Fill in the form     
    HTMLDoc2.frames(2).document.all.tags("INPUT").Item(18).Value = "TEST"
      ' Submit the form
     HTMLDoc2.frames(2).document.forms(0).submit
But without the URL that is only a guess.

You say that you can't view the HTML source, but there is usually a way around that. Try a different browser (Firefox or Chrome) or write it to a text file using:
Code:
Open "C:\HTML.txt" For Output As #1
Print #1, HTMLDoc2.body.outerHTML
Close #1
 
Upvote 0

Forum statistics

Threads
1,215,884
Messages
6,127,562
Members
449,385
Latest member
KMGLarson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top