2012-07-16 6 views
5

私はプロジェクトを構築していますが、Excel 2003は「データ - >外部データのインポート - >新しいWebクエリ」を介して外部Webページからのデータのインポートをサポートしています。ログインページを使用してExcel 2003にデータをインポート

これは、ここに記載されているいくつかの手順を介して行うことができます:http://www.internet4classrooms.com/excel_import.htm

はしかし、私はからデータをインポートしていますサイトは、社内Webサイト(イントラネット)であり、それは私がそれにアクセス、ログイン毎回必要です。

ウェブサイトではパスワードが記憶されておらず、「インポート」ボタンを押すたびにログインするため何もしません。

Excel 2003で外部Webサイトからデータをインポートする際に、ユーザー名+パスワードを要求してWebサイトにログインするにはどうすればよいですか?

+0

それは可能ですか? – seeker

+0

どのような種類のデータですか?あなたは別の方法でそれを得ることができますか?代わりにMSXMLまたはIEオートメーションを使用して取得できますか?この回答は役に立ちますか? http://stackoverflow.com/a/11216467/190829 – JimmyPena

答えて

2

私はこれを約1年前に実行しました.JimmyPenaが示唆しているように、IEの自動化はおそらく道のりです。これははるかに複雑に見えますが、あなたが期待していたものの、私を信じて、もっと簡単な方法を見つけようと数時間を費やしました。

時間をかけてHTMLとDOMオブジェクトについて学んでください。あなたがやっていることが過度のように思えるかもしれませんが、ウェブサイトからデータを取得したい場合は、道に便利です。ここでは、正しい方向に指摘を取得するスクリプトは次のとおりです。

  1. のTextBox1は、ユーザー名の入力とTextBox2をあなたがパスワードをマスクする必要があるパスワード
  2. になりますされる2つのテキストボックスとボタン
  3. でユーザーフォームを作成します。プロパティウィンドウでパスワード文字を選択して入力します(VBAエディタでF4キーを押し、ドロップダウンからtextbox2を選択してPasswordCharの隣に文字を入力します)
  4. 作成したボタンをダブルクリックして次のコードを貼り付けます。

    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とは、正しいを見つけることになりますあなたのウェブページ

HTMLとDOM(ドキュメントオブジェクトモデル)に慣れていない人にとってはトリッキーな部分で動作するようにgetelement方法を変更しますページ上の要素。

Internet Explorerの開発者用ツールを使用することをお勧めします。 IEでイントラネットページを開き、F12を押します。開発者用ツールが開きます。ツールバーの矢印アイコン(矢印が左上を指す)をクリックし、イントラネットページに戻ります。ページの上に移動すると、各要素の周りに青色のボックスが表示されます。ユーザー名のログインにカーソルを置き、入力ボックスをクリックします。これにより、ソースコード内のHTMLが強調表示されます。

要素ID、名前、タグ名、およびクラスがある場合はそれを識別できます。 getelementbyIDgetelementsbytagnameなどに関するいくつかの研究を行うか、それがどのように動作するかを知るために上記のコードをステップ実行してください。

最後に、イントラネットページにフォーム要素がある場合は、上記のgetelementメソッドを使用してフォームオブジェクトを取得し、.submitで送信する必要があります。ページがボタンオブジェクトを使用している場合は、ボタン要素を取得し、.clickを使用します。がんばろう!

1:それはまだ関連だが、私は次のマクロ

を経由して、このためのソリューションを持っている場合

0

わからないステップである新しいWebクエリをインポートするには、あなたのマクロを記録(何も行います)

2:すべてのクエリを更新します。

インラインユーザー名/パスワードを含めるようにWebクエリを編集します。

以下

は私のマクロです:あなたはそのイントラネット・サーバー上のコードを変更するため

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:[email protected]/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 

関連する問題