使用login页面将数据导入Excel 2003

我正在构build一个项目,并且我明白Excel 2003支持通过“数据 – >导入外部数据 – >新buildWeb查询”从外部网页导入数据。

这可以通过这里列出的几个步骤完成: http : //www.internet4classrooms.com/excel_import.htm

但是,我从中导入数据的站点是内部网站(Intranet),每次访问时都需要login。

该网站不记得密码,每次我点击“导入”button,它不会做任何事情,由于login。

如何在Excel 2003中从外部网站导入数据时提示input用户名和密码并login到网站?

大约一年前,我遇到了这个问题,正如JimmyPena所说,IE自动化可能是一个很好的select。 这样看起来要复杂得多,但是相信我,我花了几个小时试图find一个更简单的方法,找不到一个。

花一些时间来了解HTML和DOM对象。 如果你想从网站获取数据,对于你正在做的事情看起来似乎有些过火,但它会派上用场。 这里有一个脚本让你指出正确的方向:

  1. 用两个文本框和一个button创build一个用户窗体
  2. Textbox1将是用户名input,textbox2将是您的密码
  3. 您应该通过在属性窗口中select密码字符来屏蔽密码input(在VBA编辑器中按F4键,从下拉列表中selecttextbox2并在PasswordChar旁边input一个字符)
  4. 双击刚刚创build的button,并粘贴以下代码:

    Option Explicit Private Sub CommandButton1_Click() Const READYSTATE_COMPLETE = 4 Const tempDir As String = "C:\Windows\Temp\" Dim userName$, passWord$, URL$, s_outerhtml$ ''These are strings Dim IE As Object, IE_Element As Object, IE_HTMLCollection As Object Dim i_file% ''This is an integer Dim blnUsernameEntered As Boolean, blnPasswordEntered As Boolean, blnSheetFnd As Boolean Dim ws As Excel.Worksheet ''Test for missing username or password If Me.TextBox1 = vbNullString Then MsgBox "Enter a User Name", vbOKOnly, "User Name Missing": Exit Sub If Me.TextBox2 = vbNullString Then MsgBox "Enter a Password", vbOKOnly, "Password Missing": Exit Sub ''Set the username and password based on the userform inputs userName = Me.TextBox1.Value passWord = Me.TextBox2.Value ''Hide the form Me.Hide ''Enter your address to navigate to here URL = "http://theofficialjbfansite.webs.com/apps/auth/login" ''Create an Internet Explorer object if it doesn't exist If IE Is Nothing Then Set IE = CreateObject("InternetExplorer.Application") ''Make the window visible with true, hidden with false IE.Visible = True ''navigate to the website IE.Navigate URL '' use this loop to make wait until the webpage has loaded Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE DoEvents Loop ''This is where it will get tricky - see my notes on DOM at the end of this post ''build a collection of input elements Set IE_HTMLCollection = IE.document.getElementsByTagName("input") ''for each html element in the "input" collection For Each IE_Element In IE_HTMLCollection If IE_Element.Name = "email" Then IE_Element.innerText = userName: blnUsernameEntered = True If IE_Element.Name = "password" Then IE_Element.innerText = passWord: blnPasswordEntered = True If blnUsernameEntered = True And blnPasswordEntered = True Then Exit For ''Unblock line below if you are having trouble finding the element name, ''view the output in the Immediate Window (Ctrl + G in the VBA Editor) ''Debug.Print IE_Element.Name Next ''Find the form and submit it Set IE_HTMLCollection = IE.document.getElementsByTagName("form") For Each IE_Element In IE_HTMLCollection If IE_Element.Name = "loginForm" Then IE_Element.submit Next Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE DoEvents Loop ''The next line helps ensure that the html has been fully loaded Application.Wait Now() + TimeValue("0:00:02") s_outerhtml = IE.document.body.OuterHtml i_file = FreeFile ''This is a modification of some code I found at www.tek-tips.com <--great resource ''the code saves a temporary copy of the webpage to your temp file Open tempDir & "\tempFile.htm" For Output As #i_file Print #i_file, s_outerhtml Close #i_file ''Creating a "Data" sheet if it doesn't exist For Each ws In ThisWorkbook.Worksheets If ws.Name = "Data" Then blnSheetFnd = True: Exit For Next If blnSheetFnd = False Then Sheets.Add: ActiveSheet.Name = "Data" Sheets("Data").Cells.Clear ''Here is your webquery, using the temporary file as its source ''this is untested in 2003, if it errors out, record a macro ''and replace the property that throws the error with your recorded property With Sheets("Data").QueryTables.Add(Connection:= _ "URL;" & tempDir & "tempFile.htm" _ , Destination:=Range("$A$1")) .Name = "Data" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingAll .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With ''delete the temporary file Kill tempDir & "\tempFile.htm" ''clean up after yourself, foo!! IE.Quit Set IE = Nothing Set IE_HTMLCollection = Nothing Unload UserForm1 End Sub 
  5. 将URL更改为您的网站,并修改getelement方法以使用您的网页

不熟悉HTML和DOM(文档对象模型)的人最棘手的部分是在页面上find正确的元素。

一个好的技巧是使用Internet Explorer的开发工具。 在IE中打开您的Intranet页面,然后按F12。 这将打开开发工具。 点击工具栏上的箭头图标(箭头指向左侧)并切换回您的Intranet页面。 将鼠标hover在页面上,即可在每个元素周围看到蓝色框。 将鼠标hover在用户名login上,然后点击input框。 这将突出显示源代码中的HTML。

从这里你可以识别元素ID,名称,标记名和类,如果它有。 对getelementbyIDgetelementsbytagname等进行一些研究,或者通过上面的代码来了解它是如何工作的。

最后一点,如果你的Intranet页面有一个表单元素,你将不得不使用上面的getelement方法获得表单对象,并用.submit提交。 如果页面使用button对象,则获取button元素并使用.click 。 祝你好运!

不知道这是否仍然相关,但我有一个通过macros的解决scheme

以下是步骤:

1:logging您的macros导入新的networking查询(任何事都可以)

2:刷新所有查询。

编辑您的Web查询以包含内联用户名/密码。

以下是我的macros:

  Sub xmlUpdate() 'This will enable the import of our XML data. The first part is a dummy import, to authenticate the Excel file with the iJento servers. The second part (Web_Query_1 is the actual import) 'The sheet is initially inserted as "Dummy" and then promptly deleted. Sheets.Add.Name = "Dummy" ActiveWorkbook.XmlImport URL:= _ "https://USERNAME:PASSWORD@SERVER.com/query/app?service=file=201" _ , ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$1") Sheets("Dummy").Select Application.DisplayAlerts = False ActiveWindow.SelectedSheets.Delete ActiveWorkbook.XmlMaps("Web_Query_1").DataBinding.Refresh End Sub