0

Is there any way I can download the csv file from below link for example. "https://www.nseindia.com/corporates-pit?index=equities&symbol=WABCOINDIA&csv=true"

Tried the below code but no luck.

Private Declare PtrSafe 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

Public Sub Valuebuy()

myfile = "https://www.nseindia.com/corporates-pit?index=equities&symbol=WABCOINDIA&csv=true"

  Dim targetFile As String, targetUrl As String, returnVal As Double
    Target = "https://www.nseindia.com/api/corporates-pit?index=equities&symbol=WABCOINDIA&csv=true"
    strSavePath = "C:\testdownload.txt"
    returnVal = URLDownloadToFile(0, Target, strSavePath, 0, 0)
    If returnVal = 0 Then
        Debug.Print "Download ok!"
    Else
        Debug.Print "Error"
    End If
    
End Sub
Kiran
  • 167
  • 1
  • 9
  • 1
    "no luck" is not a useful description of exactly what happens when you run your code – Tim Williams Oct 16 '20 at 16:42
  • `Set wb = WorkBooks.open(myFile)` should work – Tim Williams Oct 16 '20 at 16:44
  • @TimWilliams, thanks for the reply, I am not able to download the file, can u try from ur end. Its saying server is busy – Kiran Oct 16 '20 at 17:20
  • I get a message to check the address – Tim Williams Oct 16 '20 at 17:23
  • my bad, apologies @TimWilliams the correct URL is below one https://www.nseindia.com/api/corporates-pit?index=equities&symbol=WABCOINDIA&csr=true .Also can u try for incognito window inchrome – Kiran Oct 16 '20 at 17:34
  • Resource not found – Tim Williams Oct 16 '20 at 17:37
  • can u refresh again twice, it came to me. This is a url which i got when inspecting a webpage. I can access through powerquery but not with vba – Kiran Oct 16 '20 at 17:38
  • I tried it also, with same results as @TimWilliams "Resource Not Found" – Ron Rosenfeld Oct 16 '20 at 17:41
  • probably this site uses cookies and cannot work the given URL. any idea how can we get through vba code by providing cookie information. – Kiran Oct 16 '20 at 17:44
  • Just tried it again and the response is a `JSON` document – Ron Rosenfeld Oct 16 '20 at 17:47
  • @RonRosenfeld, yes, but I am not able to get the data from the code provided, is there any alternative way I can download to excel sheet – Kiran Oct 16 '20 at 17:49
  • I'd probably use the `GET` method but since the response here is so slow (using any method), when I tried it a few times it timed out before anything loaded. – Ron Rosenfeld Oct 16 '20 at 18:01
  • I also get "Resource Not Found" error. Where did you obtain that URL? Also why you don;t use an API? https://www1.nseindia.com/technology/content/nnf/WEB_CBRICS_PROTOCOL_1.2.pdf – astentx Oct 16 '20 at 18:11

0 Answers0