Download Excel file from Sharepoint, Save to Desktop - excel

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.

Related

Save webpage concluded instead HTML only

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?

Download fail using VBA. Any suggestion?

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)

Downloading PDF document in Chrome using VBA Selenium

I currently need to download multiple PDF files from a specific website.
Once I reach to this point the usual action is to click on the save button or to type "CTRL + S"
Click on Save button
I retrieved this on this post of IE Automation
But I'm trying to excecute the download action with the following code:
bot.SendKeys Keys.Control, "s"
And is not working.
How can I make this work on Chrome?
Thanks,
You could use this to download the pdfs.
Dim PDF_URL as string
#If VBA7 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr) As LongPtr
#Else
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
#End If
Private Sub File_Download()
Dim FileURL As String
Dim DestinationFile As String
FileURL = PDF_URL
DestinationFile = "C:\PDFs\PDFName.pdf"
If URLDownloadToFile(0, FileURL, DestinationFile, 0, 0) = 0 Then
Debug.Print "File downloaded started"
Else
Debug.Print "File downloaded not started"
End If
End Sub
Private Sub GetURL
PDF_URL = Chrome_Driver.FindElementById("EleID").Attribute("src")
File_Download
end sub
There are a ton of websites that may not allow bots. You should go to the website's /robots.txt to see if there are any restrictions on bots pulling the content you are trying to grab. These sections will show "disallow" anything bots are forbidden to access.
example: google/robots.txt
Yes, websites can even tell if you are using Selenium.

printing specific pages form pdf file using print scope string

I need to print selected pages of *.pdf file using excel VBA.
I need to do this not by providing scope "from - to" but giving scoope of pages like "1-3,4,8, 17-25"
I can only print whole file using below code:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function
Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "C:\Users\any\Desktop"
strFile = "somefile.pdf"
printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub ```
Judging from this post, it is not possible using ShellExecute alone. What you would need is to write a script which will open the file in whatever PDF reader you have installed, find a "Print" button there and provide all the options. This will take a huge amount of work and will depend on the version of Windows and the PDF reader, so basically it will work only for you, and not for other users.
If you want to go this path though, take a look at FindWindow and SendMessage WinAPI functions.

Obtaining Image File Dimensions From URL

I have this link:
https://s23527.pcdn.co/wp-content/uploads/2017/04/wine_speedlights_kit_lens.jpg.optimal.jpg
It is on Cell A2:
I want to get on Cell B2 the dimensions of the URL of this JPG
(I don't mind how to get it, it can be 1920 on cell B2 and 1080 on cell C2)
You will need to make an API call to URLDownloadToFile to download your image. In the below example, we will download to the temp folder C:\Temp\.
Once your image is downloaded, you will create a new Shell object, and ultimately use the .ExtendedProperty() property to grab your file dimensions
After you have finished downloading your file, you can go ahead and delete the temporary file using Kill().
The below method uses Early Binding. You will need to set a reference to
Microsoft Shell Controls And Automation
By going to Tools -> References in the VBE menu
Option Explicit
#If VBA7 Then
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
#Else
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
#End If
Sub test()
Const tmpDir$ = "C:\Temp\"
Const tmpFile$ = "tmpPicFile.jpg"
Debug.Print URLDownloadToFile(0, ActiveSheet.Range("A2").Value, tmpDir & tmpFile, 0, 0)
ActiveSheet.Range("B2").Value = getFileDimensions(tmpDir, tmpFile)
Kill tmpDir & tmpFile
End Sub
Private Function getFileDimensions(filePath$, fileName$) As String
With New Shell32.Shell
With .Namespace(filePath).ParseName(fileName)
getFileDimensions = .ExtendedProperty("Dimensions")
End With
End With
End Function

Resources