Downloading PDF document in Chrome using VBA Selenium - excel

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.

Related

Download Excel file from Sharepoint, Save to Desktop

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.

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)

Sending text to another application

I am looking to send some text fields from Excel to another application using the SendMessage method. Here is my code below:
Public Declare PtrSafe Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Public Declare PtrSafe Function SendMessageByString Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As String) As Long
Public Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare PtrSafe Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Public Const WM_SETTEXT As Long = &HC
Public Const BM_CLICK As Long = &HF5&
Sub RunApplication()
Dim Vendor As String
Dim qty As String
Dim PartId As String
hwnd = FindWindow(vbNullString, "Purchase Order Entry - Infor ERP VISUAL Enterprise - LIVE")
gupta_form = FindWindowEx(hwnd, 0, "Gupta:Form", vbNullString)
Order_Date = FindWindowEx(gupta_form, 0, "Edit", vbNullString)
Our_OrderID = FindWindowEx(gupta_form, Order_Date, "Edit", vbNullString)
VendorID = FindWindowEx(gupta_form, Our_OrderID, "Edit", vbNullString)
gupta_dialog = FindWindowEx(hwnd, 0, "Gupta:Dialog", "Table Toolbar")
gupta_child_table = FindWindowEx(gupta_form, 0, "Gupta:ChildTable", vbNullString)
list_clip = FindWindowEx(gupta_child_table, 0, "Gupta:ChildTable:ListClip", vbNullString)
Open_line = FindWindowEx(gupta_dialog, 0, "Button", vbNullString)
qty_edit = FindWindowEx(list_clip, 0, "Edit", vbNullString)
Vendor = Sheets("Daily Stock").Range("H16")
qty = Sheets("Daily Stock").Range("I16")
PartId = Sheets("Daily Stock").Range("A16")
Call SendMessageByString(VendorID, WM_SETTEXT, ByVal CLng(0), ByVal Vendor) 'Enter Vendor ID into Field
Call SendMessageByString(Open_line, BM_CLICK, 0, ByVal 0&) 'Click Button to open Quantity field
Timeout (2) 'Wait until Quantity field is open
Call SendMessageByString(qty_edit, WM_SETTEXT, ByVal CLng(0), ByVal qty) 'Enter Quantity from Daily Stock sheet
End Sub
I am having 2 problems. For some reason the "Vendor" string is being correctly sent to the external application however the "qty_edit" window is not receiving the data. When running this program I can see that the cursor has moved to the correct window but no data was sent. The only difference between these 2 fields in the application is that the "VendorID" window is expecting text but the "qty_edit" window is expecting a number. Is this the reason why the number is not being received?
My second problem relates to the next window that I am trying to write to. In order to enter text into the next window "PartID" I need to send a Tab keystroke to move across from the "qty_edit" window to the "PartID" window. What is the best way for me to move across to this window? I cannot see the location of this window in Spy++. It needs to be activated by a Tab keystroke.
I think you've made it a lot more complicated than necessary.
This should work (or something similar)
AppActivate ("Purchase Order Entry - Infor ERP VISUAL Enterprise - LIVE")
SendKeys Vendor & "{TAB}" & qty
AppActivate (Application.Caption)

Troubled with VBA version difference: version 6 and version 7

I'm new to VBA and use excel 2010 64bit VBA v6.0 compatible. I pasted the code, trying to download files through VBA.
Option Explicit
'Tutorial link: https://youtu.be/H4-w6ULc_qs
#If VBA7 Then
Private Declare 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
Sub download_file()
'-----------------------------
'Thanks for downloading the code.
'Please visit our channel for a quick explainer on how to use this code.
'Feel free to update the code as per your need and also share with your friends.
'Download free codes from http://vbaa2z.blogspot.com
'Support our channel: youtube.com/vbaa2z
'Author: L Pamai (vbaa2z.team#gmail.com)
'-----------------------------
Dim downloadStatus As Variant
Dim url As String
Dim destinationFile_local As String
url = [D3]
destinationFile_local = "C:\Users\myUserName\Downloads\" & fileName([D3])
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
However, a pop-up window says it can only run on 64-bit systems as follow:
Compile error:
The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute.
My questions are:
I do use window and office 64-bit system. Why the window keeps popping up?
Is there any way to solve this problem?
Thanks in advance.
As the error tells you, add the PtrSafe keyword to the VBA7 branch
#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 Long, ByVal lpfnCB As LongPtr) As Long
#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
You need to add this keyword anywhere you are using LongPtr, or LongLong.
Here is the MS Documentation on PtrSafe
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ptrsafe-keyword
Adding the PtrSafe keyword to a Declare statement only signifies that the Declare statement explicitly targets 64-bits. All data types within the statement that need to store 64-bits (including return values and parameters) must still be modified to hold 64-bit quantities by using either LongLong for 64-bit integrals or LongPtr for pointers and handles.

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