title: Excel web data from password protected site (password not required after first login)
body: <p>I am trying to write a VBA script that goes to a website I maintain with a table of data and updates a range with the data on the website. The website is password protected. I have the whole VBA script figured out except for one piece: if the password has already been input and the browser saves a cookie, the VBA script fails because it tries to input a value into a form that doesn't exist. I need to create an if statement that checks if the form that catches the password exists - and if it doesn't, skips the section of code that puts the password into the form. </p>
  
  <pre><code>Sub GetTable()
  
  Dim ieApp As InternetExplorer
  Dim ieDoc As Object
  Dim ieTable As Object
  Dim clip As DataObject
  
  'create a new instance of ie
  Set ieApp = New InternetExplorer
  
  'you don’t need this, but it’s good for debugging
  ieApp.Visible = True
  
  'assume we’re not logged in and just go directly to the login page
  ieApp.Navigate "http://google.com/"
  Do While ieApp.Busy: DoEvents: Loop
  Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
  
  Set ieDoc = ieApp.Document
  
  'fill in the login form – View Source from your browser to get the control names
      Do While ieApp.Busy: DoEvents: Loop
  Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
  MsgBox ieDoc.getElementById("pwbox-106")
  
  If ieDoc.getElementById("pwbox-106") &lt;&gt; Null Then
     With ieDoc.forms("The form that accepts and submits password")
      .post_password.Value = "PASSWORD"
      .submit
  End With
  
  End If
  
  'get the table based on the table’s id
  Set ieDoc = ieApp.Document
  Set ieTable = ieDoc.all.Item("tablepress-1")
  
  'copy the tables html to the clipboard and paste to the sheet
  If Not ieTable Is Nothing Then
      Set clip = New DataObject
      clip.SetText "&lt;html&gt;" &amp; ieTable.outerHTML &amp; "&lt;/html&gt;"
      clip.PutInClipboard
      Sheet2.Select
      Sheet2.Range("A1").Select
      Sheet2.PasteSpecial "Unicode Text"
  End If
  
  'close IE
  ieApp.Quit
  Set ieApp = Nothing
  
  End Sub
  </code></pre>