Fill a form from excel?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cynicon
    New Member
    • Feb 2010
    • 20

    Fill a form from excel?

    Hi everyone

    So this is my question...

    I need to log in so many times in so many computers, in a specific web page "http://webshipping.dhl .com.mx"

    I wold like to do it with a macro Im using Excel 2003

    By now, I have the next code, But doesnt seems to work, please help me...



    Code:
    Sub pag()
    
    Dim myie As New InternetExplorer 'New'
    Dim mydoc As HTMLDocument
    Dim myurl, usuario As String
    Dim ieform As Object
    
    myurl = "http://webshipping.dhl.com.mx"
    usuario = "User"
    
    myie.navigate myurl
    myie.Visible = True
    
    'esperar a que cargue la página
    Do While myie.Busy Or myie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
    Loop
    
    'MsgBox myie.LocationName
    
    
    Set mydoc = myie.Document
    mydoc.forms(0).UserName.Value = usuario ' i get an error here
    
    ' code for set the password must be here
    
    'code for submit the form is here
    
    'MsgBox "pausa p/ver si escribió"
    'mydoc.forms [0].submit
    
    Do While myie.Busy Or myie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
    Loop
    
    MsgBox myie.LocationName
    
    
    End Sub
  • cynicon
    New Member
    • Feb 2010
    • 20

    #2
    Hello, is anybody there?

    Comment

    • Guido Geurs
      Recognized Expert Contributor
      • Oct 2009
      • 767

      #3
      To open a webpage in Excel:

      Code:
      Sub launchIE()
      ActiveWorkbook.FollowHyperlink Address:="http://www.google.ca", NewWindow:=False
      ActiveWorkbook.FollowHyperlink Address:="http://www.yahoo.ca", NewWindow:=False
      End Sub
      For Your URL it will be:

      Code:
      Sub launchIE()
      ActiveWorkbook.FollowHyperlink Address"http://webshipping.dhl.com.mx", NewWindow:=False
      End Sub

      Comment

      • danp129
        Recognized Expert Contributor
        • Jul 2006
        • 323

        #4
        Checking to see if the browser is finished is always a pain especially in frames or redirects. I've left your checks but added my own, then showed how to populate the textboxes and submit the form.
        Code:
        ' References: 1) MS Internet Controls. 2) MS HTML Object Library
        Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
        
        
        Const PROCESS_TIMEOUT = 20  ' Seconds to wait for procedure to fail on errors
        
        Sub pag()
        Dim StartTime As Date
        Dim myie As New InternetExplorer 'New'
        Dim mydoc As HTMLDocument
        Dim myurl As String
        Dim ieform As IHTMLFormElement
        Dim usuario As String
        Dim Contrasena As String
        
        StartTime = Now()
        
        usuario = "User"
        Contrasena = "Password"
        
        myurl = "http://webshipping.dhl.com.mx"
         
        myie.navigate myurl
        myie.Visible = True
         
        'esperar a que cargue la página
        Do While myie.Busy Or myie.ReadyState <> READYSTATE_COMPLETE
        DoEvents
        Loop
        
        ' Wait until submit button is found or our process times out
        Dim testElement As IHTMLElementCollection
        Do
            ' Look for submit button, exit loop if we find it
            On Error Resume Next
            Set mydoc = myie.Document
            Set testElement = mydoc.getElementById("loginform").getElementsByTagName("input")
            
            If testElement Is Nothing Then
                Sleep (100) ' Wait 1/10th second
            Else
                ' We've found the form and some inputs, if the submit button is found then exit do
                If LCase(testElement.Item(testElement.Length - 1).Value) = "ingresar" Then Exit Do
            End If
            
            ' Check time process has taken, if more than user specified, close browser and exit loop
            If DateDiff("s", StartTime, Now()) >= PROCESS_TIMEOUT Then
                Set mydoc = Nothing
                Set testElement = Nothing
                myie.Quit
                Set myie = Nothing
                MsgBox "Failed to login to website, user specified timeout of " & PROCESS_TIMEOUT & " seconds has been exceeded."
                Exit Sub
            End If
        Loop
        
        On Error GoTo 0
        
        Dim formInputs As IHTMLElementCollection
        Set ieform = mydoc.getElementById("loginform")
        
        ieform("j_username", 0).Value = usuario
        ieform("j_password", 0).Value = Contrasena
        ieform.submit
        
         
        Do While myie.Busy Or myie.ReadyState <> READYSTATE_COMPLETE
        DoEvents
        Loop
         
        MsgBox myie.LocationName
         
         
        End Sub

        Comment

        • cynicon
          New Member
          • Feb 2010
          • 20

          #5
          omg

          danp129, you are my hero, it works perfectly, I really appreciate the time you dedicate to reply my question

          Thank you so much

          Cheers

          Comment

          Working...