2017-01-10 8 views
0

このスクリプトを使用すると、2番目のIEセッションが開始された理由を確認できますが、パスワードとユーザーの入力データは入力されません。複数のページにVBAでログインする

Sub MyLogin() 
Dim Url As String 
Dim ie As Object 

    Set ie = CreateObject("InternetExplorer.Application") 
    With ie 
     .Visible = True 
     .navigate "https://website" 

     Do Until .readystate = 4 
      DoEvents 
     Loop 

     .document.all.Item("username").Value = "userid" 
     .document.all.Item("password").Value = "password//" 
     .document.forms(0).submit 

     Url = "https://website" 
     Set ie = CreateObject("InternetExplorer.Application") 
     ie.Visible = True 
     ie.navigate Url 
     ie = Nothing 

     Do 
     DoEvents 
     Loop Until ie.readystate = 4 

     .document.all.Item("Enter user name").Value = "userid" 
     .document.all.Item("passwd").Value = "password" 
     .document.forms(0).submit 

    End With 
End Sub 

答えて

1

あなたはWithブロック内の参照を再割り当てするときは、新しいオブジェクトを得ることはありません。 With ieは、作成するの最初のieオブジェクトの参照カウントをインクリメントします。ブロック内の基準から始まるものはすべてそのブロックを参照しています。だから、あなたがこれを行う...

Set ie = CreateObject("InternetExplorer.Application") 

...

.document.all.Item("Enter user name").Value = "userid" 
    .document.all.Item("passwd").Value = "password" 
    .document.forms(0).submit 
... Withブロック、次のコード行内側 から

...はすべて、作成した最初のInternetExplorer.Applicationオブジェクトを参照しています(ref With ieによって誘導される)。

このコードは、(説明のためにScripting.Dictionaryを使用して)、ここで何が起こっているかを示しています。実際のURLずに伝えること

Sub FooedUp() 
    Dim foo As Scripting.Dictionary 
    Set foo = New Scripting.Dictionary 
    With foo      'This now holds a reference to the 'foo' above. 
     Debug.Print ObjPtr(foo) 'This is the pointer to that instance. 
     .Add 1, 1     'Add an item. 
     Debug.Print .Count  'Count is now 1`. 
     Set foo = New Scripting.Dictionary 'Re-use the 'foo' variable. 
     Debug.Print ObjPtr(foo) 'The pointer is now different. 
     Debug.Print .Count  '...but this is still bound to the first 'foo', prints 1. 
    End With 
End Sub 

ハード、しかし、あなたはおそらく、このようなより多くの何かを探して(マイナスまで掃除しています最初の呼び出しの後):

Sub MyLogin() 
    Dim Url As String 
    Dim ie As Object 

    Set ie = CreateObject("InternetExplorer.Application") 
    With ie 
     .Visible = True 
     .navigate "https://website" 
     Do Until .readystate = 4 
      DoEvents 
     Loop 
     .document.all.Item("username").Value = "userid" 
     .document.all.Item("password").Value = "password//" 
     .document.forms(0).submit 
    End With '<--Un-bind the first IE object. 

    Url = "https://website" 
    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = True 
    ie.navigate Url 
    With ie '<--Bind the new IE object. 
     Do 
      DoEvents 
     Loop Until ie.readystate = 4 
     .document.all.Item("Enter user name").Value = "userid" 
     .document.all.Item("passwd").Value = "password" 
     .document.forms(0).submit 
    End With 
End Sub 
+0

ありがとうComintern –

+0

それを解決しました。ブレークダウンのためのおかげです。 –

関連する問題