How to Copy Data From Website to Excel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • epifinygirl
    New Member
    • Aug 2010
    • 19

    How to Copy Data From Website to Excel

    Hi,

    I'm relatively new to VBA but I am trying to copy data from a website (this website is currently running and will be the same) and paste to defined sheet in Excel. Below is the code that I have thus far, however, I am receiving an error "Method 'ExecWB' of object 'IWebBrowser2' failed".

    Code:
    Public Sub csi()
    Dim appIE As InternetExplorer
    Dim xlApp As Excel.Application
    Dim wsData As Worksheet
    Set xlApp = Application
    Set wsData = xlApp.Worksheets("CSIInq")
    Set appIE = New InternetExplorer
    AppActivate ("Exact Website Title Name Here")
    appIE.ExecWB 17, 2  <<--- This line is where the error comes up
    appIE.ExecWB 12, 0
    AppActivate ("CSI")
    Workbooks("CSI.XLS").Activate
    Range("A1").Select
    ActiveSheet.Paste
    Set appIE = Nothing
    
    End Sub
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Have you set a reference to the library "Microsoft Internet Controls"?

    Comment

    • epifinygirl
      New Member
      • Aug 2010
      • 19

      #3
      Yes, that reference has been set

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        OK change this line
        Code:
        AppActivate ("Exact Website Title Name Here")
        to
        Code:
        objIE.Navigate "Exact Website Title Name Here"
        Does that help?

        Comment

        • epifinygirl
          New Member
          • Aug 2010
          • 19

          #5
          No, I get the same error as above on the same line (#9).

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Are you sure the error is happening at line #9 and not line #10. Check out the remarks section on this method on the msdn website.

            Comment

            • epifinygirl
              New Member
              • Aug 2010
              • 19

              #7
              Yes, the error is on #9. I just went searching and saw the same thing, but I was having trouble changing the registry

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                I'm going to see if I can get someone with more experience using this library to check this out.

                Mary

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Try the below as a Template:
                  Code:
                  'First, you must set a Reference to the Microsoft Internet Controls
                  Dim appIE As InternetExplorer
                  Const conURL As String = "http://www.msn.com"
                  
                  DoCmd.Hourglass True
                  
                  Set appIE = New InternetExplorer
                  
                  appIE.Navigate conURL
                  
                  'You must wait until the Page is fully loaded
                  Do
                  Loop Until appIE.ReadyState = READYSTATE_COMPLETE
                  
                  'Copy and Paste the Web Page
                  appIE.ExecWB 17, 2
                  appIE.ExecWB 12, 0
                  
                  'Web Page should now be in the Clipboard
                  
                  DoCmd.Hourglass False
                  
                  'Clean Up, being careful of Scope
                  'appIE.Quit
                  'Set appIE = Nothing

                  Comment

                  • epifinygirl
                    New Member
                    • Aug 2010
                    • 19

                    #10
                    Great!! Is it possible to change the constant URL to activating the open window with a specific title? I ask because the website is based off of several logins and if I try to retrieve it another way, the session will time out.

                    Comment

                    • ADezii
                      Recognized Expert Expert
                      • Apr 2006
                      • 8834

                      #11
                      You can change the URL to anything you like, but to the best of my knowledge, a New Instance of IE will be opened when Navigating to this URL.

                      Comment

                      • epifinygirl
                        New Member
                        • Aug 2010
                        • 19

                        #12
                        Well, the website will already be open since I'll be logged in and if I put the URL in the code then it will give me a "session expired" message. There is no way to switch windows to copy the data and then come back to paste it?

                        Comment

                        • ADezii
                          Recognized Expert Expert
                          • Apr 2006
                          • 8834

                          #13
                          Is it an absolute requirement that the Window already be Open?

                          Comment

                          • epifinygirl
                            New Member
                            • Aug 2010
                            • 19

                            #14
                            Yes because it's a secure website so I wouldn't be able to access it from just entering the URL

                            Comment

                            • ADezii
                              Recognized Expert Expert
                              • Apr 2006
                              • 8834

                              #15
                              Back to the Drawing Board, you may have a problem...

                              Comment

                              Working...