Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Oct 2013
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post

    Unable to determine download link

    Hello Guys,

    I download a file containing stock market data from a website. Basically I open the webpage and there is a link View in Excel (top most right) and when I click on it the browser asks me to where to save the file.

    After the file is saved I can view it. It is a csv file and is generated every day. So the filename is specific to a date.

    I want to write a program that can download the file automatically but I am unable to determine the download link. I tried to check this using IE and DAP but the download link appears as the webpage name. Plus its unable to download through DAP.

    Can you guys determine download link or tell me how can I do it otherwise I have to manually do it everyday.

    The site address is : http://www.mcxindia.com/SitePages/BhavCopyDateWise.aspx

    Here click on view in excel (topmost right) allows us to save file to our system.

    Thank you,

    Cheers,
    GR

  2. #2
    Bronze Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    1,353
    Thanks
    49
    Thanked 275 Times in 253 Posts
    GreatChap,

    In the posted workbook, click the "Get File" button and you will be prompted to download the file. Give it a second or two depending on your internet speed.

    HTH,
    Maud

    web1.png

    Code:
    Private Sub CommandButton1_Click()
    '-----------------------------------------------
    'MAKE BROWSER WINDOW VISIBLE AND NAVIGATE TO SITE
    WebBrowser1.Visible = True
    WebBrowser1.Navigate2 "http://www.mcxindia.com/SitePages/BhavCopyDateWise.aspx"
    Do
        DoEvents
    Loop Until WebBrowser1.ReadyState = 4
    '-----------------------------------------------
    'DOWNLOAD FILE
    WebBrowser1.Document.getelementbyid("btnLink_Excel").Click
    Do
        DoEvents
    Loop Until WebBrowser1.ReadyState = 4
    End Sub
    
    Private Sub Worksheet_Activate()
    'START WITH HIDDEN BLANK BROWSER WINDOW
    WebBrowser1.Visible = False
    WebBrowser1.Navigate2 "about:blank"
    End Sub
    Attached Files Attached Files
    Last edited by Maudibe; 2014-03-03 at 18:26.

  3. #3
    New Lounger
    Join Date
    Oct 2013
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks a lot buddy. It worked and I used vb.net web browser control to do it. Is there a way to automate the save file dialog box or process. In other words file is saved at a specific location automatically. I am using vb.net browser to do this.

  4. #4
    Bronze Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    1,353
    Thanks
    49
    Thanked 275 Times in 253 Posts
    greatchap,

    If there was a URL link to the csv file this would be fairly simple to accomplish. The web site, however, uses javascript instead. After much research, apparently this can only be done using Windows API by selecting the child File download Window by determining its handle. Each of the buttons (Open/Save/Cancel) are separate child windows themselves each with their own handles as well. Once the handles are known, they can be selected and clicked. I got as far as positioning the cursor and clicking the open button but it does not open the file. I tried this with the save button as well but the MOUSEEVENTF_LEFTDOWN and MOUSEEVENTF_LEFTUP Does not seem to function as it should. Using SendKeys will not work nor the SendMessage API. I have to play with this further. In the meantime, I am open for suggestions.

    Maud

  5. #5
    New Lounger
    Join Date
    Oct 2013
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    I did try send keys and it worked. The dialog box opens and I am able to save the file. Though the send keys is a bit dicey because if the window is changed or we shift to other app then it won't work otherwise it seems to be ok for now. Is there any alternative or can a net.webclient class to do something.

  6. #6
    Bronze Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    1,353
    Thanks
    49
    Thanked 275 Times in 253 Posts
    Please post the code you used for SendKeys.

    By default, the Cancel button in the file download dialogue box is selected. It tried:
    SendKeys "{TAB}{TAB}~" as well as just enter SendKeys "~" and Sendkeys "{ENTER}". I even put them in their own routine and called it with Application.OnTime after 15 seconds to no avail. You are correct in that SendKeys is risky because the active window may change and could do cause some unwanted actions. However, if you were to add it to the code I wrote, that risk would be minimized because the active window is controlled. I agree that if there is an alternative to use it and the method I had described above should have worked but it doesn't.

    Take a look at the code I was attempting to implement. It is explained very well here and may answer some of your questions.
    http://www.siddharthrout.com/2011/10...wnload-window/

    Maud
    Last edited by Maudibe; 2014-03-04 at 07:15.

  7. #7
    New Lounger
    Join Date
    Oct 2013
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Basically for send keys I send the key "S" which is short for save. Then the dialog box opens and I am able to pass the filename and then I sendkey {Enter}. The whole process is working fine for the moment.

    Thanks for your help, I really appreciate it.

  8. #8
    Bronze Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    1,353
    Thanks
    49
    Thanked 275 Times in 253 Posts
    Gretachap,

    Have you inserted the commands into the procedure I wrote or is this from a separate procedure? Either way, please post the code.

    Thanks,
    Maud

  9. #9
    New Lounger
    Join Date
    Oct 2013
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    The code is a vb.net code -

    CODE IN BUTTON CLICK:

    WebBrowser1.Navigate("http://www.mcxindia.com/SitePages/BhavCopyDateWise.aspx")

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEv entArgs) Handles WebBrowser1.DocumentCompleted
    WebBrowser1.Document.InvokeScript("__doPostBack", New Object() {"btnLink_Excel", ""})
    DocComplete = True
    Timer1.Interval = 2000
    Timer1.Start
    End Sub

    In the Timer Tick

    SendKeys.Send("s")

    SendKeys.Send("C:\abc.csv")
    SendKeys.Send("{Enter}")

    I hope it helps.

  10. The Following User Says Thank You to greatchap For This Useful Post:

    Maudibe (2014-03-06)

  11. #10
    Bronze Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    1,353
    Thanks
    49
    Thanked 275 Times in 253 Posts
    Thanks

    Maud

Posting Permissions

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