In my Excel workbook I use VBA code that calls URLDownloadToFile to download a different Excel file stored in SharePoint to the current folder of the workbook.
The downloaded workbook is not the latest / canonical revision. The link used as a parameter for URLDownloadToFile is the one that links directly to the file, NOT to a specific revision with "_vti_history".
I tried updating the file on SharePoint multiple times to test this but the same old revision was downloaded every time.
Edit: To clarify, the issue.
I call the function as
URLDownloadToFile(0, "http://blahblah/file.ext", "C:\blah\file.ext", 0, 0)
The local copy saved is not the latest revision of "file.ext" but an older one.
If the URL used is the same to download the newest version, it may be that a cached version is being downloaded by URLDownloadToFile.
Try to clear the cache for the link before downloading by using DeleteUrlCacheEntry:
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
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
Sub downloadFile()
Call DeleteUrlCacheEntry("http://blahblah/file.ext")
Call URLDownloadToFile(0, "http://blahblah/file.ext", "C:\blah\file.ext", 0, 0)
End Sub
I have been trying to figure this out. The function URLDownloadToFile was not working for Sharepoint Online as it couldn't navigate the authentication part. Finally, I found this link which saved my day. The solution that worked for me Version 1 as I didn't have access to the folder view of the file, I only had direct access to the file alone. I created the following function and added the reference to Windows Script Host Object Model library in VBA.
Public Sub VBA_FileCopy(ByVal sSourceFile As String, ByVal sDestinationFile As String)
Dim fs As FileSystemObject
Set fs = New FileSystemObject 'CreateObject("Scripting.FileSystemObject")
sSourceFile = Replace(sSourceFile, "/", "\")
sSourceFile = Replace(sSourceFile, "http:", "")
sSourceFile = Replace(sSourceFile, "https:", "")
sSourceFile = Replace(sSourceFile, Split(sSourceFile, "\")(2), Split(sSourceFile, "\")(2) & "#SSL\DavWWWRoot")
sSourceFile = Replace(sSourceFile, " ", "%20")
fs.CopyFile sSourceFile, sDestinationFile, True
End Sub
Related
I am trying to download an excel file stored in sharepoint (no unc path available) to my desktop.
This code below seems to work and create "CST.xlsx" but I get an error msg:
Notes:
Url for excel file is taken directly from the web address bar and everthing after ".xlsx" is removed.
Code:
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
Function DownloadFileFromWeb(strURL As String, strSavePath As String) As Long
' strSavePath includes filename
DownloadFileFromWeb = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
End Function
Sub download()
Call DownloadFileFromWeb("url.xlsx", "Desktop\download\CST.xlsx")
End Sub
See my answer in this thread about the same problem for a workaround.
I'm currently using a VBA code in a spreadsheet to download a webpage, the issue is that this is downloading only the html page, without the folder that is downloaded when we go to the browser and select "Save As".
Below I'm showing the explanation with images - I would like to know if is possible to, directly from VBA, download the concluded page (with folder that comes with images, etc.) instead only the HTML.
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
Sub datafeed()
src = "https://google.com"
dlpath = "C:\Users\User\Downloads\Test\"
URLDownloadToFile 0, src, dlpath & "test.html", 0, 0
End Sub
How is currently downloading:
How I would like to download (like when I select Save As in the browser):
Does someone know how to configure VBA to download the webpage and the folder with the files instead only the webpage?
Hi I'm trying to download a zip in a local server using VBA. My code works great in my PC but at the server doesn't work. Heres's the code:
PS. Cell A7 is the link of the download.
Dim downloadStatus As Variant
Dim url As String
Dim destinationFile_local As String
url = [A7]
destinationFile_local = "C:\Users\omayorga\Downloads\" & fileName([A7])
downloadStatus = URLDownloadToFile(0, url, destinationFile_local, 0, 0)
If downloadStatus = 0 Then
MsgBox "Downloaded Succcessfully!"
Else
MsgBox "Download failed"
End If
End Sub
Function fileName(file_fullname) As String
fileName = Mid(file_fullname, InStrRev(file_fullname, "/") + 1)
End Function
Any suggestion? Thanks so much
You have this in your code, you need:
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
Check your download link, the same said Raymond Wu your destinationFile_local, tested here your code works.
refer link
How do I download a file using VBA (without Internet Explorer)
I've had a look around and I think I need some very specific help. I need to write a macro to download excel files online. Until now I have been able to use the URLdownloadtofile function by adding the get request parameters to the URL. The website I need to download from this time however doesn't seem to work that way. (It ends in aspx, if that makes a difference). I use http fox addin on Firefox to see the request line and all it gives is the website and no parameters after it. How can I use xmlhttp to download this file if I can't use URLdownloadtofile? Also one of the dropdown menus only becomes active when the previous menu value is selected.
The URL of the file is: http://cmg.cdec-sic.cl/Modulos/CMg/CDEC_CMgBarras.aspx
If I want the excel file for January 2014, tension 220 and barra alto jahuel_220 for instance, what would I need to place after the send function of the xmlhttp function? As in what parameters? I tried using the names of each parameter = to the value I want. And I changed all the $ to the hex value. But it just seemed to return the same page.
And once that works, how do I get the returned excel file to download to a specific directory?
Any help would be greatly appreciated.
Option Explicit
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
Dim Ret As Long
Sub Download_Files_from_web_and_save_to_file()
Dim strURL As String
Dim strPath As String
Ret = URLDownloadToFile(0, " http://cmg.cdec-sic.cl/Modulos/CMg/CDEC_CMgBarras.aspx", "d:\my_file.xls", 0, 0)
If Ret = 0 Then
' MsgBox "File successfully downloaded"
Else
MsgBox "Unable to download the file"
End If
End Sub
Firstly, I'm a newer to Excel Macro.
I have read some articles and done some work these days.
But now I'm so hesitant.Can .txt or .xml file be placed into VBAProject?If yes,how place and how to get the path of file or just read the file? If no, is there a transit way to do this work? Just think Macro,not AddIn project.
Thanks very much!
You can save a txt with ini extension and save your settings in it as key value pairs. With below code you can retrieve those settings providing the key and getting the value.
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Function GetINIString(ByVal sApp As String, ByVal sKey As String, ByVal filepath As String) As String
Dim sBuf As String * 256
Dim lBuf As Long
lBuf = GetPrivateProfileString(sApp, sKey, "", sBuf, Len(sBuf), filepath)
GetINIString = Left$(sBuf, lBuf)
End Function
Sub sample()
Dim Path As String
Path = ThisWorkbook.Path & "\" & "Path.ini"
Link = GetINIString("Path", "Link", Path)
End Sub
Please save the .ini file in the same folder where the workbook resides or you may change the Path variable accordingly.
You may also refer this link for more details.