PDA

View Full Version : Filling in IE form with values from Excel



jdawg_1989
07-17-2011, 03:46 AM
Hi All,

I have put together the following code that works great, however....
It steps through all the text box's and put the values into them but the form actually gets submitted blank!

Does anybody have any ideas why this is the case?

Here is all my code, unfortunately the webpage is on an internal intranet.

Option Explicit
Option Compare Text

Const cForm_name As Long = 1
Const cForm_Id As Long = 2
Const cElement_Name As Long = 3
Const cElement_ID As Long = 4
Const cElement_nodeName As Long = 5
Const cElement_Type As Long = 6
Const cElement_Value As Long = 7
Const cElement_SetValue As Long = 8

Sub FillOTForm()
On Error Resume Next
Dim objIE As Object
Dim objParent As Object
Dim objInputElement As Object
Dim lngRow As Long

Set objIE = GetIEApp
'Make sure an IE object was hooked
If TypeName(objIE) = "Nothing" Then
MsgBox "Could not hook Internet Explorer object", vbCritical, "GetFields() Error"
GoTo Clean_Up
End If

'''PAGE 1'''
'Text Box1
objIE.document.all("page1a").Value = Range("D18").Value
'Text Box2
objIE.document.all("page1b").Value = Range("D20").Value
'Radio Button
objIE.document.all.Item("page2")(1).Checked = True
'Click Next Button
objIE.document.forms("surveyform").submit

'''PAGE 2'''
'Wait untill IE has loaded
Do Until objIE.Busy = False And objIE.readyState = 4
DoEvents
Loop

'Text Box1
objIE.document.all("page3a").Value = Range("D11").Value
'Text Box2
objIE.document.all("page3b").Value = Range("C14").Value
'Text Box3
objIE.document.all("page3c").Value = Range("D20").Value

'Text Box4
objIE.document.all("page4a").Value = Range("F16").Value
'Text Box5
objIE.document.all("page4b").Value = Range("G16").Value
'Text Box6
objIE.document.all("page4c").Value = Range("H16").Value
'Text Box7
objIE.document.all("page4d").Value = Range("I16").Value
'Text Box8
objIE.document.all("page4i").Value = Range("J16").Value
'Text Box9
objIE.document.all("page4j").Value = Range("K16").Value
'Text Box10
objIE.document.all("page4k").Value = Range("L16").Value

'Combo Box
objIE.document.all("page5").Value = Range("N18").Value

'Text Area
objIE.document.all("Question6").Value = Range("B23").Value

'Click Next Button
objIE.document.forms("surveyform").submit

'''PAGE 3'''
'Wait untill IE has loaded
Do Until objIE.Busy = False And objIE.readyState = 4
DoEvents
Loop

'Combo Box
objIE.document.all("page7").Value = Range("N20").Value

Clean_Up:
Set objParent = Nothing
Set objIE = Nothing
End Sub

Function GetIEApp() As Object
Dim objShell As Object
Dim objWindows As Object
Dim objWindow As Object
Dim lngSingleWindow As Long
Dim intOption As Integer
Dim strMessage As String, strReturnValue As String

Set objShell = CreateObject("Shell.Application")
Set objWindows = objShell.Windows
lngSingleWindow = -1

For Each objWindow In objWindows
'Build a list of windows, make sure they are Internet Explorer
If Left(objWindow.document.Title, 16) = "Platform Cluster" Then
strMessage = strMessage & intOption & " : " & objWindow.LocationName & vbCrLf
If lngSingleWindow = -1 Then
lngSingleWindow = intOption
Else
lngSingleWindow = 0
End If
End If
intOption = intOption + 1
Next
'Check if there are any IE windows
If Len(strMessage) <> 0 Then
'Prompt to pick a window, used an InputBox for portability
If lngSingleWindow > 0 Then
Set GetIEApp = objWindows.Item(CLng(lngSingleWindow))
Else
strReturnValue = 0
'If the user cancels the input box an empty string is returned
If strReturnValue <> "" Then
'Make sure the number selected is valid
If Val(strReturnValue) >= 0 And Val(strReturnValue) <= intOption Then
Set GetIEApp = objWindows.Item(CLng(strReturnValue))
End If
End If
End If
End If
Set objWindow = Nothing
Set objWindows = Nothing
Set objShell = Nothing
End Function