+ Reply to Thread
Results 1 to 5 of 5

Activating IE to copy and Paste

Hybrid View

  1. #1
    Registered User
    Join Date
    07-28-2009
    Location
    Chicago, Il
    MS-Off Ver
    Excel 2007
    Posts
    67

    Activating IE to copy and Paste

    I have a macro set up and what it should do is go to a website copy the web page and paste into cell A1.
    The problem I am having is that it will open the IE, open the Page but will not do the copy paste function can someone tell me what i am doing wrong

    Sub Macro4()
    
       Dim IE As Object
         
         Sheets("Sheet2").Select
         Cells.Select
        Selection.Delete Shift:=xlUp
        Range("A1:A1000") = "" ' erase previous data
        Range("A1").Select
         
        Set IE = CreateObject("InternetExplorer.Application")
        With IE
            .Visible = True
            .Navigate "http://www.google.com" ' shou
            Do Until .ReadyState = 4: DoEvents:  Loop
            End With
             
            
            IE.ExecWB 17, 0 '// SelectAll
            IE.ExecWB 12, 2 '// Copy selection
            ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
            Range("A1").Select
            IE.Quit
            
        
        
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Activating IE to copy and Paste

    Try this. Put the code in a sheet module or the ThisWorkbook module and make sure you follow the comment at the top of the code.
    Option Explicit
    
    'Needs reference to Microsoft Internet Controls (Tools -> References in VB Editor)
    
    Private WithEvents IE As InternetExplorer
    Private Copied As Boolean
           
    Public Sub CopyWebPage()
    
        'Delete all data on active sheet
        
        Cells.Delete Shift:=xlUp
    
        Set IE = CreateObject("InternetExplorer.Application")
        
        Copied = False
        
        With IE
            .Visible = True
            .Navigate "http://www.google.com"
            While .busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
        
            'Wait until the page has been copied by the DocumentComplete event
            
            While Not Copied
                DoEvents
            Wend
    
            'Now paste it to A1 on the active sheet and quit IE
            
            Range("A1").Select
            ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
            Range("A1").Select
    
            .Quit
        End With
        
        Set IE = Nothing
        
    End Sub
    
    Private Sub IE_DocumentComplete(ByVal pDisp As Object, URL As Variant)
        
        'Copy the page to the clipboard
        
        If pDisp = IE Then
            IE.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DONTPROMPTUSER
            IE.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
            Copied = True
        End If
        
    End Sub

  3. #3
    Registered User
    Join Date
    07-28-2009
    Location
    Chicago, Il
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Activating IE to copy and Paste

    Chippy,
    thank you very much but I must admit that you are alot further advanced than I am. I have no idea with you have here so I tried to just copy and paste and i keep getting errored out it keeps giving me "User-defined type not defined" and then it goes to 'Private WithEvents IE as Internet explorere"
    I have put this code into Thisworkbook and in Sheet2
    I do appreciate you help with this

  4. #4
    Registered User
    Join Date
    07-28-2009
    Location
    Chicago, Il
    MS-Off Ver
    Excel 2007
    Posts
    67

    Re: Activating IE to copy and Paste

    Ok, sorry about that finally got the references correct. now the problem I am having is that it is going to quick on not letting the page fully load and the page i am actually trying to copy has tables on it and it is only giving me text

    Sorry to be such a newbie pain

  5. #5
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Activating IE to copy and Paste

    Try adding this line after the While .busy .... : Wend loop:
            While .Document.ReadyState <> "complete": DoEvents: Wend
    However, that shouldn't be necessary because the DocumentComplete event handler is fired asynchronously only when the page is completely loaded and initialised, and the main CopyWebPage() routine is waiting for a flag to be set so that it knows that the page has been loaded and copied. DocumentComplete is fired only once, unless the page has frames, in which case it can fire multiple times, which could be what's happening for your page.

    If the extra line above has no effect, post the URL of the page.

+ 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.6.0 RC 1