2016-01-23 48 views
1

我想使用Excel中的宏自动化Internet Explorer。
我可以用硬编码的数据做到这一点。我想从另一张表中复制数据。将动态数据传递给Internet Explorer - 创建多个登录

示例: - 我想在example.com网站中创建多个登录,并在Sheet中复制用户名。

我在Excel代码: -

Sub Submit() 
     Set objIE = CreateObject("InternetExplorer.Application") 
     objIE.Top = 0 
     objIE.Left = 0 
     objIE.Width = 800 
     objIE.Height = 600 
     objIE.AddressBar = 0 
     objIE.StatusBar = 0 
     objIE.Toolbar = 0 
     objIE.Visible = True 'We will see the window navigation 

     objIE.Navigate ("http://example.com") 

     Do 
      DoEvents 
     Loop Until objIE.ReadyState = 4 

     pageSource = objIE.Document.body.Outerhtml 
     objIE.Document.GetElementByID("ctl00_ContentPlaceHolder1_txtUserName").Value = "xyz" 
     objIE.Document.GetElementByID("ctl00_ContentPlaceHolder1_txtPassword").Value = "123" 
     objIE.Document.GetElementByID("ctl00_ContentPlaceHolder1_btnSubmit1").Click 
    End Sub 

数据在Excel

+------+---------------+ 
| User | Password | 
+------+---------------+ 
| ABC | 123   | 
| XYZ | 456   | 
| XCZ | 777   | 
+------+---------------+ 

回答

1

UserPassword作为paremeters为Submit子。

Sub Submit(User as string, Password as string) 
    Set objIE = CreateObject("InternetExplorer.Application") 
    objIE.Top = 0 
    objIE.Left = 0 
    objIE.Width = 800 
    objIE.Height = 600 
    objIE.AddressBar = 0 
    objIE.StatusBar = 0 
    objIE.Toolbar = 0 
    objIE.Visible = True 'We will see the window navigation 

    objIE.Navigate ("http://example.com") 

    Do 
     DoEvents 
    Loop Until objIE.ReadyState = 4 

    pageSource = objIE.Document.body.Outerhtml 
    objIE.Document.GetElementByID("ctl00_ContentPlaceHolder1_txtUserName").Value = User 
    objIE.Document.GetElementByID("ctl00_ContentPlaceHolder1_txtPassword").Value = Password 
    objIE.Document.GetElementByID("ctl00_ContentPlaceHolder1_btnSubmit1").Click 
End Sub 

在另一个子项中调用该子。

Sub Login() 
    dim strUser as string 
    dim strPassword as string 
    dim intLastRow as integer 
    dim intRow as integer 

     intLastRow = Cells(Rows.Count, "A").End(xlUp).Row 

     For intRow = 2 to intLastRow 
      strUser = Worksheets("MySheet").Cells(intRow, 1).value 
      strPassword = Worksheets("MySheet").Cells(intRow, 2).value  
      call Submit(strUser, strPassword) 
     Next intRow 
    End Sub 

这可能有效。

相关问题