2012-07-16 20 views
5

मैं एक प्रोजेक्ट बना रहा हूं और मैं समझता हूं कि एक्सेल 2003 बाहरी डेटापृष्ठों से डेटा के आयात का समर्थन करता है "डेटा -> बाहरी डेटा आयात करें -> नई वेब क्वेरी"।लॉगिन पृष्ठ के साथ Excel 2003 में डेटा आयात करें

यह यहाँ सूचीबद्ध कुछ ही कदम के माध्यम से किया जा सकता है: http://www.internet4classrooms.com/excel_import.htm

हालांकि, साइट मैं से डेटा आयात कर रहा हूँ किसी आंतरिक वेबसाइट (इंट्रानेट) है और यह मैं इसे का उपयोग एक लॉगिन हर की आवश्यकता है।

वेबसाइट को पासवर्ड याद नहीं है और हर बार जब मैं "आयात" बटन दबाता हूं, तो यह लॉगिन के कारण कुछ भी नहीं करता है।

मैं उपयोगकर्ता नाम + पासवर्ड के लिए कैसे संकेत दूं और एक्सेल 2003 में बाहरी वेबसाइट से डेटा आयात करते समय वेबसाइट पर लॉगिन करूं?

+0

क्या यह संभव है आपको लगता है कि इंट्रानेट सर्वर पर कोड बदलने के लिए:

नीचे मेरी मैक्रो है? – seeker

+0

यह किस प्रकार का डेटा है? क्या आप इसे एक और तरीका प्राप्त कर सकते हैं? क्या आप इसके बजाय इसे पुनर्प्राप्त करने के लिए एमएसएक्सएमएल या आईई ऑटोमेशन का उपयोग कर सकते हैं? क्या यह उत्तर मदद करता है? http://stackoverflow.com/a/11216467/190829 – JimmyPena

उत्तर

2

मैं एक साल पहले इसमें भाग गया था और जिमीपेना ने सुझाव दिया था कि आईई स्वचालन शायद जाने का रास्ता है। यह और अधिक जटिल दिखने जा रहा है, तो आप उम्मीद कर रहे थे लेकिन मेरा विश्वास करो, मैंने एक आसान तरीका खोजने की कोशिश करने में घंटों बिताए और एक नहीं मिला।

HTML और DOM ऑब्जेक्ट के बारे में जानने के लिए कुछ समय लें। ऐसा लगता है कि आप जो कर रहे हैं उसके लिए यह अधिक हो सकता है लेकिन अगर आप वेबसाइटों से डेटा प्राप्त करना चाहते हैं तो यह सड़क पर उतर जाएगा। दो बक्सें और एक बटन

  • Textbox1 उपयोगकर्ता नाम इनपुट और TextBox2 हो जाएगा के साथ एक UserForm

    1. अपना पासवर्ड होगा
    2. आप पासवर्ड मुखौटा चाहिए: यहाँ आप सही दिशा में इशारा किया पाने के लिए एक स्क्रिप्ट है गुण विंडो में पासवर्ड वर्ण का चयन करके इनपुट (वीबीए संपादक में F4 दबाएं, ड्रॉपडाउन से टेक्स्टबॉक्स 2 का चयन करें और पासवर्डकहर के बगल में एक वर्ण दर्ज करें)
    3. आपके द्वारा अभी बनाए गए बटन पर डबल क्लिक करें और निम्न कोड में पेस्ट करें:

      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 
      
    4. बदलें अपनी वेबसाइट के लिए URL और अपने वेबपेज

    किसी HTML और डोम (दस्तावेज़ ऑब्जेक्ट मॉडल) के साथ अपरिचित के लिए trickiest हिस्सा के साथ काम करने के लिए सही पाने के लिए किया जाएगा getelement तरीकों को संशोधित पेज पर तत्व।

    इंटरनेट एक्सप्लोरर के डेवलपर टूल का उपयोग करना एक अच्छी चाल है। आईई में अपना इंट्रानेट पेज खोलें और एफ 12 दबाएं। यह डेवलपर टूल खुल जाएगा। टूलबार में तीर आइकन (तीर बिंदु ऊपर और बाईं ओर) पर क्लिक करें और अपने इंट्रानेट पृष्ठ पर वापस स्विच करें। पृष्ठ पर होवर करें और आपको प्रत्येक तत्व के चारों ओर चित्रित नीले रंग के बक्से दिखाई देंगे। उपयोगकर्ता नाम लॉगिन पर होवर करें और इनपुट बॉक्स पर क्लिक करें। यह स्रोत कोड में एचटीएमएल को हाइलाइट करेगा।

    यहां से आप तत्व आईडी, नाम, टैगनाम और कक्षा की पहचान कर सकते हैं यदि उसके पास है। getelementbyID, getelementsbytagname इत्यादि पर कुछ शोध करें या उपरोक्त कोड के माध्यम से कदम उठाएं ताकि यह काम करने के लिए एक महसूस हो सके।

    एक अंतिम नोट, यदि आपके इंट्रानेट पेज में एक फॉर्म तत्व है, तो आपको फॉर्म ऑब्जेक्ट को getelement विधियों के साथ प्राप्त करना होगा और इसे .submit से सबमिट करना होगा। यदि पृष्ठ बटन ऑब्जेक्ट का उपयोग करता है, तो बटन तत्व प्राप्त करें और .click का उपयोग करें। सौभाग्य!

    1:

  • 0

    सुनिश्चित नहीं हैं कि अगर यह अभी भी प्रासंगिक है, लेकिन मैं एक मैक्रो

    निम्नलिखित के माध्यम से इस बात के लिए एक समाधान है कदम हैं नई वेब क्वेरी आयात करने में अपने मैक्रो रिकॉर्ड (कुछ भी करेंगे)

    2: अपने सभी प्रश्नों को ताज़ा करें।

    इनलाइन उपयोगकर्ता नाम/पासवर्ड शामिल करने के लिए अपनी वेब क्वेरी संपादित करें।

    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