-2

I am trying to download a report from my company's site for reporting purposes.

Steps for:

  1. Open IE
  2. Go to download link
  3. Click on extract button
  4. click on open button on the IE dialog box(save/open/cancel box)
  5. Copy the data to sheet 1 of my active workbook
  6. Close IE

I have done till step 3. I am having trouble with step 4. I tried the below solutions but they did not work for me.

How to check if Open/Save/Cancel bar appeared

Automate saveas dialogue for IE9 (vba)

Code Used:

Sub ExtractGLfile()

    Set ie = New InternetExplorerMedium
    Dim DLCPortalGL As String
    DLCPortalGL = "link"
    ie.Visible = True
    ie.navigate (DLCPortalGL)

    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE

    ie.document.getElementById("ButtonID").Click

I need help with step 4 - clicking on open of the open/save/cancel button.

UPDATES: I was able to download the file using the below code

    Application.SendKeys "%{O}", True
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

SendKeys "{TAB}", True
SendKeys "{ENTER}", True

However while the file opens, I am getting error: enter image description here Any suggestions?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Impossible to answer your question as of now. You need to include a [Minimal, Complete and Verifiable Example](https://stackoverflow.com/help/mcve) – Samuel Hulla Aug 07 '18 at 13:27
  • 1
    @QHarr I Thank you for feedback. I apologize for the previous question. Thank you – Sheetal Proag Aug 07 '18 at 14:00
  • Hi, thanks for responding. It would help to see the relevant HTML using the snippet tool I described before and also for you to elaborate on what doesn't work means? Your click is working and the file is downloaded but now you can't press the saveas/open dialog? And when you say open in sheet1....you want to transfer the data to sheet1? Does the document you are downloading only have one sheet? What are you planning on doing with that data? Where will it go? – QHarr Aug 07 '18 at 14:03
  • noted. Yes, click is working and I cannot press the save/open tab in the active workbook sheet 1. Yes, I want to transfer the data in sheet 1 for my report. This data is the input for my report. – Sheetal Proag Aug 07 '18 at 14:12
  • There are numberous Q&As on SO for hitting the saveas/open dialog partic with IE. Have a look at those. If you can locate an URL associated with the download then the answer below is one way to do a direct download. You would then target the file for opening. – QHarr Aug 07 '18 at 14:41

1 Answers1

0

If you are trying to simply download a page or file, you can use this fast function DownloadFile:

Option Compare Database
Option Explicit

' API declarations.
'
Private Declare Function URLDownloadToFile Lib "Urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long

' Download a file or a page with public access from the web. 
' Returns 0 if success, error code if not. 
' 
' If parameter NoOverwrite is True, no download will be attempted 
' if an existing local file exists, thus this will not be overwritten. 
' 
' Examples: 
' 
' Download a file: 
'   Url = "https://www.codeproject.com/script/Membership/ProfileImages/%7Ba82bcf77-ba9f-4ec3-bbb3-1d9ce15cae23%7D.jpg" 
'   FileName = "C:\Test\CodeProjectProfile.jpg" 
'   Result = DownloadFile(Url, FileName) 
' 
' Download a page: 
'   Url = "https://www.codeproject.com/Tips/1022704/Rounding-Values-Up-Down-By-Or-To-Significant-Figur?display=Print" 
'   FileName = "C:\Test\CodeProject1022704.html" 
'   Result = DownloadFile(Url, FileName) 
' 
' Error codes: 
' -2146697210   "file not found". 
' -2146697211   "domain not found". 
' -1            "local file could not be created." 
' 
' 2004-12-17. Gustav Brock, Cactus Data ApS, CPH. 
' 2017-05-25. Gustav Brock, Cactus Data ApS, CPH. Added check for local file. 
' 2017-06-05. Gustav Brock, Cactus Data ApS, CPH. Added option to no overwrite the local file. 
' 
Public Function DownloadFile( _ 
    ByVal Url As String, _ 
    ByVal LocalFileName As String, _ 
    Optional ByVal NoOverwrite As Boolean) _ 
    As Long 

    Const BindFDefault  As Long = 0 
    Const ErrorNone     As Long = 0 
    Const ErrorNotFound As Long = -1

    Dim Result  As Long

    If NoOverwrite = True Then 
        ' Page or file should not be overwritten. 
        ' Check that the local file exists. 
        If Dir(LocalFileName, vbNormal) <> "" Then 
            ' File exists. Don't proceed. 
            Exit Function 
        End If 
    End If     

    ' Download file or page. 
    ' Return success or error code. 
    Result = URLDownloadToFile(0, Url & vbNullChar, LocalFileName & vbNullChar, BindFDefault, 0)   

    If Result = ErrorNone Then 
        ' Page or file was retrieved. 
        ' Check that the local file exists. 
        If Dir(LocalFileName, vbNormal) = "" Then 
            Result = ErrorNotFound 
        End If 
    End If   

    DownloadFile = Result 

End Function

The full story - and about caching or not - can be found here.

Gustav
  • 53,498
  • 7
  • 29
  • 55
  • I cannot use the url to download as i need to click on the download button . I need to click on an extract button. – Sheetal Proag Aug 07 '18 at 13:45
  • Yes, but check what code the button runs. It may be nothing but a link to the excel file. Or present the URL here for us to play with. – Gustav Aug 07 '18 at 15:42
  • So, I added the below code and it worked. Application.SendKeys "%{O}", True newHour = Hour(Now()) newMinute = Minute(Now()) newSecond = Second(Now()) + 10 waitTime = TimeSerial(newHour, newMinute, newSecond) Application.Wait waitTime SendKeys "{TAB}", True SendKeys "{ENTER}", True – Sheetal Proag Aug 08 '18 at 08:01
  • OK. But there is no `Application.Wait` in Access VBA. – Gustav Aug 08 '18 at 08:08