+ Reply to Thread
Results 1 to 13 of 13

Thread: How to fetch the table from ASP web site and save in Excel

  1. #1
    Registered User
    Join Date
    01-01-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2010
    Posts
    6

    How to fetch the table from ASP web site and save in Excel

    We have a search tool web site (.aspx), which will give the list of policy data in a table format (Rows & Columns) upon clicking submit button. i want to fetch the entire table and have to save that in a excel. I have already wrote the code for opening the web site and clicking submit botton. I have attached the source code of the web site in this thread. Can anyone give me the sample code to fetch the entire 12th table of the web site and save it in the excel worksheet. Thanks for your help.

    Please remember that, i have to include your sample in the below query. So please provide the code which fit into this.
    Sub Button1_Click() 
    Dim obj As Object 
    Dim Scrn As String 
    Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll) 
    Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library 
    Dim htmlInput As MSHTML.HTMLInputElement 
    Dim htmlColl As MSHTML.IHTMLElementCollection 
    
    Set objIE = New SHDocVw.InternetExplorer
    
    If (objIE Is Nothing) Then 
    MsgBox "Could not create the Internet Explorer object. Stopping macro playback." 
    Stop 
    End If 
    
    With objIE 
    .Navigate "http://devts296:81/spiqatools/ui/PolicyFinder.aspx" ' Main page
    
    .Visible = 1 
    Do While .readyState <> 4: DoEvents: Loop 
    Application.Wait (Now + TimeValue("0:00:02"))
    Set htmlDoc = .document 
    objIE.document.getElementById("LOBList").Value = "A" 
    objIE.document.getElementById("DataFilterEnv").Value = "L" 
    objIE.document.getElementById("ctl04_rdbAutoTransitionSignal_1").Checked = True objIE.document.getElementById("ctl04_AutoTransitionSignal").Value = "L" 
    objIE.document.getElementById("btnsubmit").Click 
    
    While objIE.Busy 
    DoEvents 
    Wend 
    While objIE.readyState <> 4 
    DoEvents 
    Wend 
    
    End With 
    
    End Sub
    Attached Files Attached Files
    Last edited by Leith Ross; 01-01-2012 at 03:15 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to fetch the table from ASP web site and save in Excel

    Hello Rajen87,

    Welcome to the Forum!

    I have attached a file that shows the table that I believe you are referring to. Please have a look and let me know if this the table you want to import. Do want just the text or text and links copied to the worksheet?
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-01-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to fetch the table from ASP web site and save in Excel

    Thanks for looking ino my query. Yes, i need the same table which you have attached. Only Text is fine. Please provide the sample code which fits in my code.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to fetch the table from ASP web site and save in Excel

    Hello Rajen87,

    I will copy only the text. Was the picture I posted of the correct table? Where should the text be copied to on the worksheet?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    01-01-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to fetch the table from ASP web site and save in Excel

    Yes that is the correct table. It should be pasted in new worksheet like "sheet1".

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to fetch the table from ASP web site and save in Excel

    Hello Rajen87,

    Here is the modified macro. If you have any problems, let me know.
    
    ' Thread:  http://www.excelforum.com/excel-programming/807655-how-to-fetch-the-table-from-asp-web-site-and-save-in-excel.html
    ' Poster:  rajen87
    ' Written: January 02, 2012
    ' Author:  Leith Ross
    
    Sub Button1_Click()
    
        Dim C As Long
        Dim Cell As Object
        Dim Data As Variant
        Dim obj As Object
        Dim R As Long
        Dim Rng As Range
        Dim Scrn As String
        Dim TableX As Object
        Dim Text As String
        Dim Wks As Worksheet
    
        Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
        Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
        Dim htmlInput As MSHTML.HTMLInputElement
        Dim htmlColl As MSHTML.IHTMLElementCollection
    
            Set objIE = New SHDocVw.InternetExplorer
    
            If (objIE Is Nothing) Then
               MsgBox "Could not create the Internet Explorer object. Stopping macro playback."
               Stop
            End If
    
                With objIE
                    .Navigate "http://devts296:81/spiqatools/ui/PolicyFinder.aspx" ' Main page
                    .Visible = 1
                
                    Do While .ReadyState <> 4: DoEvents: Loop
                    Application.Wait (Now + TimeValue("0:00:02"))
                
                    Set htmlDoc = .document
                    objIE.document.getElementById("LOBList").Value = "A"
                    objIE.document.getElementById("DataFilterEnv").Value = "L"
                    objIE.document.getElementById("ctl04_rdbAutoTransitionSignal_1").Checked = True
                    objIE.document.getElementById("ctl04_AutoTransitionSignal").Value = "L"
                    objIE.document.getElementById("btnsubmit").Click
    
                While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
                End With
                
                Set TableX = objIE.document.getElementById("GridView1")
                
                R = TableX.Rows.Length - 2
                ReDim Data(1 To R, 1 To 15)
                
                For R = 1 To TableX.Rows.Length - 2
                    For C = 0 To 14
                        Text = ""
                        Set Cell = TableX.Rows(R).Cells(C)
                        If Not Cell Is Nothing Then
                           If Cell.HasChildNodes Then
                              Select Case Cell.ChildNodes(0).NodeName
                                 Case Is = "A"
                                     Text = Cell.ChildNodes(0).innerHTML
                                 Case Is = "#text"
                                     Text = Cell.innerHTML
                              End Select
                           End If
                           If InStr(1, Text, "<br>", vbTextCompare) Then
                              Data(R, C + 1) = Replace(Text, "<br>", vbLf, 1, -1, vbTextCompare)
                           Else
                              Data(R, C + 1) = Text
                           End If
                        End If
                    Next C
                Next R
                
            Set Wks = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            Wks.Name = "Policy Info"
            
            With Wks.Range("A1:O1").Resize(RowSize:=UBound(Data))
                .Value = Data
                .Rows(1).Font.Bold = True
                .VerticalAlignment = xlVAlignTop
                .HorizontalAlignment = xlHAlignCenter
                .Columns.AutoFit
            End With
    
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Registered User
    Join Date
    01-01-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to fetch the table from ASP web site and save in Excel

    Hi Leith,

    Thanks a lot for your sample code. I tried that in my tool. I am getting error message as "Runtime Error '91': Object variable or With block variable not set error" in the line

     R = TableX.Rows.Length - 2
    I have tried to remove the error, but couldn't able to do that. Could you please help me on that?

    Once again. Thanks for your help. Really it is helpful.
    Thanks,
    Raj

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to fetch the table from ASP web site and save in Excel

    Hello Raj,

    This problem occurs often with Internet Explorer. Excel often gets ahead of Internet Explorer in processing. I have added a 2 second delay to the macro before it begins processing the document object. Replace the old macro with the code below.
    ' Thread:  http://www.excelforum.com/excel-programming/807655-how-to-fetch-the-table-from-asp-web-site-and-save-in-excel.html
    ' Poster:  rajen87
    ' Written: January 02, 2012
    ' Updated: January 04, 2012
    ' Author:  Leith Ross
    
    Sub Button1_Click()
    
        Dim C As Long
        Dim Cell As Object
        Dim Data As Variant
        Dim obj As Object
        Dim R As Long
        Dim Rng As Range
        Dim Scrn As String
        Dim TableX As Object
        Dim Text As String
        Dim Wks As Worksheet
    
        Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
        Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
        Dim htmlInput As MSHTML.HTMLInputElement
        Dim htmlColl As MSHTML.IHTMLElementCollection
    
            Set objIE = New SHDocVw.InternetExplorer
    
            If (objIE Is Nothing) Then
               MsgBox "Could not create the Internet Explorer object. Stopping macro playback."
               Stop
            End If
    
                With objIE
                    .Navigate "http://devts296:81/spiqatools/ui/PolicyFinder.aspx" ' Main page
                    .Visible = 1
                
                  ' Wait for 2 seconds to allow the document object to be loaded.
                    Application.Wait (Now + TimeValue("0:00:02"))
                
                    Do While .ReadyState <> 4: DoEvents: Loop
                    Application.Wait (Now + TimeValue("0:00:02"))
                
                    Set htmlDoc = .document
                    objIE.document.getElementById("LOBList").Value = "A"
                    objIE.document.getElementById("DataFilterEnv").Value = "L"
                    objIE.document.getElementById("ctl04_rdbAutoTransitionSignal_1").Checked = True
                    objIE.document.getElementById("ctl04_AutoTransitionSignal").Value = "L"
                    objIE.document.getElementById("btnsubmit").Click
    
                While objIE.Busy Or objIE.ReadyState <> 4: DoEvents: Wend
                End With
                
                Set TableX = objIE.document.getElementById("GridView1")
                
              ' Wait for 2 seconds to allow the document object to be loaded.
                Application.Wait (Now + TimeValue("0:00:02"))
                
                R = TableX.Rows.Length - 2
                ReDim Data(1 To R, 1 To 15)
                
                For R = 1 To TableX.Rows.Length - 2
                    For C = 0 To 14
                        Text = ""
                        Set Cell = TableX.Rows(R).Cells(C)
                        If Not Cell Is Nothing Then
                           If Cell.HasChildNodes Then
                              Select Case Cell.ChildNodes(0).NodeName
                                 Case Is = "A"
                                     Text = Cell.ChildNodes(0).innerHTML
                                 Case Is = "#text"
                                     Text = Cell.innerHTML
                              End Select
                           End If
                           If InStr(1, Text, "<br>", vbTextCompare) Then
                              Data(R, C + 1) = Replace(Text, "<br>", vbLf, 1, -1, vbTextCompare)
                           Else
                              Data(R, C + 1) = Text
                           End If
                        End If
                    Next C
                Next R
                
            Set Wks = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            Wks.Name = "Policy Info"
            
            With Wks.Range("A1:O1").Resize(RowSize:=UBound(Data))
                .Value = Data
                .Rows(1).Font.Bold = True
                .VerticalAlignment = xlVAlignTop
                .HorizontalAlignment = xlHAlignCenter
                .Columns.AutoFit
            End With
    
    End Sub
    Last edited by Leith Ross; 01-04-2012 at 03:00 AM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Registered User
    Join Date
    01-01-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to fetch the table from ASP web site and save in Excel

    Still it is not working. Even i made 10 sec delay. I guess their is no value in TableX.Rows.length object. It is coming as null value. If i comment the line "R = TableX.Rows.length - 2", then same error is coming for the line "For R = 1 To TableX.Rows.length - 2". As per my analysis, wherever we have TableX.Rows we are getting this error.

    Please help me here. I am badly needed this for my project.

  10. #10
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: How to fetch the table from ASP web site and save in Excel

    I think you'd better use a HTTPRequest.
    BTW is the address correct ??



  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to fetch the table from ASP web site and save in Excel

    Hello Raj,

    The macro was created and tested using the HTML file you posted. I am using Internet Explorer 8. If you are using a different version that may be the cause. The code in question is standard and runs on all browsers. However, I am unable to reproduce the error on my system. Since the last update changed nothing then something else is at work. What that may be is difficult to say since this is being run on private network that I know nothing about nor have access to.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  12. #12
    Registered User
    Join Date
    01-01-2012
    Location
    Chennai
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to fetch the table from ASP web site and save in Excel

    Hi Leith,

    I also tried with HTML saved page, it is working perfect. But the actual web page is .aspx formate. Do you know whether .aspx formate will have any differant procedure or referance to add. Do you have any other idea to get the table?

    Thanks,
    Raj

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to fetch the table from ASP web site and save in Excel

    Hello Raj,

    I must not understand what you want to do. Active server pages simply create HTML documents that are sent to the browser. The server side scripts create the HTML document when the ASP file is executed. Provided that the information in the example HTML file is the same as that generated by your ASP file, the code should work.

    Do you have the necessary network permissions to run the macro and this file on your network?
    Does the table named "GridView1" exist in your ASP file code?

    If you are not certain about the last question then open the ASP file using Notepad. Use CTRL+F to search for "GridView1" in the file.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0