dcsimg
Results 1 to 3 of 3

Thread: Automate loading an online workbook

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    2

    Automate loading an online workbook

    I'm fairly new to VBA, so this may seem like a noob level question. I pull a report via a php download URL, which I then parse for data that I need to review. What I want to automate, which has stumped me to no end, is creating a process that will automatically obtain the file (through IE) and open the file for me. My "best effort" process thus far is using some content I found online from years ago and using SendKeys to send the O to tell it to open.

    I'd like a cleaner solution that is more "end user proof" so that when I'm not at work, my peer can take my macro and run it to get the data with little to no effort or potential for error on their part. This entire process kicks off from within Excel.

    Excel version 14 (32bit)
    VBA version 7.0

    Here's my dirty:
    Code:
    Sub Automate_IE_Load_FOA(ID As String)
            
        Application.Interactive = False
    'This will load a webpage in IE
        Dim z As Long
        Dim URL As String
        Dim ie As Object
        Dim objElement As Object
        Dim objCollection As Object
        Dim HWNDSrc As Long
        
     
        'Create InternetExplorer Object
        Set ie = CreateObject("InternetExplorer.Application")
     
        While ie.Busy
            DoEvents
        Wend
     
        'Set IE.Visible = True to make IE visible, or False for IE to run in the background
        ie.Visible = False
        ie.Visible = True
        
        'Define URL
        URL = "https://the.base.url/downloader.php?id=" & ID & ""
     
        'Navigate to URL
        ie.Navigate URL
        
        ' Statusbar let's user know website is loading
        Application.StatusBar = URL & " is loading. Please wait..."
        
        ' --> Commands removed due to causing issues with processing
        ' Wait while IE loading...
        'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
        'Do While IE.ReadyState = 4: DoEvents: Loop
        'Do Until IE.ReadyState = 4: DoEvents: Loop
     
        ' We wait Excel to wait 5 seconds to give us enough time for the FOA report to load
        Application.Wait (Now + TimeValue("0:00:05"))
        ' We're sending the letter O through the keyboard.  As the focus should be on an "open" dialogue box,
        ' this will cause IE to open the FOA report
    
        For z = 1 To 1
            Application.SendKeys "{o}", True
            DoEvents
        Next
        ' Show Excel again
        
        ' Now we need to sit and spin our wheels for a bit while the report is being loaded in Excel
        WasteTime
        ' Fixes a bug in VBA.  This will turn the NUMLOCK feature back on
        For z = 1 To 1
            Application.SendKeys "{NUMLOCK}", True
            DoEvents
        Next
        'Webpage Loaded
        Application.StatusBar = URL & " Loaded | Proceeding with script.  Please wait..."
        Application.Interactive = True
    End Sub
    Any help would be greatly appreciated.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,929

    Re: Automate loading an online workbook

    I'd like a cleaner solution that is more "end user proof" so that when I'm not at work, my peer can take my macro and run it to get the data with little to no effort or potential for error on their part. This entire process kicks off from within Excel.
    what problems are you having now?

    possibly appactivate to make sure that ie is the topmost window when the keystroke is being sent
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    2

    Re: Automate loading an online workbook

    Quote Originally Posted by westconn1 View Post
    what problems are you having now?

    possibly appactivate to make sure that ie is the topmost window when the keystroke is being sent
    I was able to find several various items that, when added together, did the trick in testing. Being a holiday and I don't have that computer handy, I don't have a sample of the code to show at this time.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width