将dynamic数据传递给Internet Explorer – 创build多个login名

我想用Excel中的macros自动化Internet Explorer。
我能用硬编码的数据做到这一点。 我想从另一张表中复制数据。

例如: – 我想在example.com网站中复制用户名在Sheet中创build多个login。

我在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 | +------+---------------+ 

使UserPassword作为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 

这可能工作。