I want to be able to download images by using multiple image urls on vba. This is the code I have right now, and everytime I try download, it fails (It reads "Files not Found!"). Any help will be 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
Private Sub Image()
Dim i As Long
Dim url As String
Dim done As Long
Worksheets("Sheet1").Activate
For i = 1 To 4
url = Sheet1.Range("A" & i).Text
done = URLDownloadToFile(0, url, "C:\Users\Public\Pictures\", 0, 0)
Next
'Test.
If done = 0 Then
MsgBox "Files have been downloaded!"
Else
MsgBox "Files not Found!"
End If
End Sub
I have got this code to work by doing two simple things:
Make sure you have http:// included in your urls, without the http URLDownloadToFile fails to download the file.
The usage of the URLDownloadToFile function is expecting a filename rather than a path name for the downloaded file. A quick and dirty fix is to do the following:
done = URLDownloadToFile(0, url, "C:\Users\Public\Pictures\pic" & i & ".gif", 0, 0)
which assigns a name to each file using i.
A better solution would parse the filename from the url and save using this filename.
Other than that the code worked fine for me.
Related
I am struggling to download full html code from below page. I was using URLDownloadToFileA and MSXML2.XMLHTTP60 methods and none of them downloads full code. Part of the webpage is missing - table on the bottom with "DEBTI...." in not included in the code. My aim is to get these "DEBTI..." strings and I want to avoid selenium if possible. None of below codes work for me, but when I just ctrl+s (save) page from the browser everything is correctly mentioned in the code. Any suggestions? Thanks!
Sub Get_Data()
Dim xmlhttp As New MSXML2.XMLHTTP60, myurl As String
myurl = "https://ec.europa.eu/taxation_customs/dds2/ebti/ebti_consultation.jsp?Lang=en&Lang=en&refcountry=&reference=&valstartdate1=&valstartdate=&valstartdateto1=&valstartdateto=&valenddate1=&valenddate=&valenddateto1=&valenddateto=&suppldate1=&suppldate=&nomenc=2309&nomencto=&keywordsearch1=&keywordsearch=&specialkeyword=&keywordmatchrule=OR&excludekeywordsearch1=&excludekeywordsearch=&excludespecialkeyword=&descript=&orderby=0&Expand=true&offset=1&viewVal=Thumbnail&isVisitedRef=false&allRecords=0&showProgressBar=true" 'replace with your URL
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
Open "C:\Desktop\test.txt" For Output As #1
Print #1, (xmlhttp.responseText)
Close #1
End Sub
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
Function DownloadFile(URL As String, LocalFilename As String) As Boolean
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function
Sub test()
DownloadFile "https://ec.europa.eu/taxation_customs/dds2/ebti/ebti_consultation.jsp?Lang=en&Lang=en&refcountry=&reference=&valstartdate1=&valstartdate=&valstartdateto1=&valstartdateto=&valenddate1=&valenddate=&valenddateto1=&valenddateto=&suppldate1=&suppldate=&nomenc=8418&nomencto=&keywordsearch1=&keywordsearch=&specialkeyword=&keywordmatchrule=OR&excludekeywordsearch1=&excludekeywordsearch=&excludespecialkeyword=&descript=&orderby=0&Expand=true&offset=1&viewVal=List&isVisitedRef=false&allRecords=0&showProgressBar=true", "C:\Desktop\abc.html"
End Sub
web:
https://ec.europa.eu/taxation_customs/dds2/ebti/ebti_consultation.jsp?Lang=en&Lang=en&refcountry=&reference=&valstartdate1=&valstartdate=&valstartdateto1=&valstartdateto=&valenddate1=&valenddate=&valenddateto1=&valenddateto=&suppldate1=&suppldate=&nomenc=8418&nomencto=&keywordsearch1=&keywordsearch=&specialkeyword=&keywordmatchrule=OR&excludekeywordsearch1=&excludekeywordsearch=&excludespecialkeyword=&descript=&orderby=0&Expand=true&offset=1&viewVal=List&isVisitedRef=false&allRecords=0&showProgressBar=true
I need to use Powerpoint or VBA to save a URL’s image to a folder on my windows computer. The purpose is that i’m trying to embed a pic of a Tableau dashboard to my powerpoint, but for some reason the pic won’t show up when I insert it. Using VBA should work as i can save the image to a folder with VBA, and then insert it into the powerpoint from the folder using VBA.
Right now, this is the code I'm using in EXCEL to try and save the image from Tableau to a windows folder. Either Excel VBA or Powerpoint VBA will work for this purpose:
Public 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
Public Sub GURoL(url As String, FileName As String)
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, url, FileName, 0, 0)
If lngRetVal <> 0 Then
MsgBox "GURol godo: Can't download from " & url & " to " & FileName
End If
End Sub
Sub Download_Procedure()
'Saves an image to folder, needs to have jpg tho it looks like
'https://stackoverflow.com/questions/51858575/web-download-image-and-save-to-folder/51859060#51859060
Call GURoL("https://us-west-1.online.tableau.com/TableauaddressExample.png", _
"G:\My Drive\FolderExample.png") '<change your dest. path
End Sub
So I'm using the above code to save an image. However, it does not work with the tableau image, but it does work with other jpg images on google. Which is weird.
Once I fix that step, I can use the below to save the image from my folder with powerpoint VBA:
Sub AddSavedImage()
'Adds a picture from your folder
'https://www.techrepublic.com/blog/microsoft-office/use-vba-to-insert-a-picture-file-onto-a-powerpoint-slide/
ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
FileName:="G:\FolderExample\FileExample.png", _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, Left:=60, Top:=35, _
Width:=98, Height:=48).Select
End Sub
Can someone help me automate saving a tableau URL image? It would work if I could automatically insert the tableau URL image (which i have found), but that gives an error when I insert it into ppt as well.
This tableau website/URL image will need you to login to access it. You may be able to do this with VBA. But as the other commenter says, probably the domain is wrong
I'm able to display a google static map in Excel.
Sheets("Sheet1").Range("a1").Parent.Pictures.Insert theURL
This works fine but once my URL is longer than 1650 characters I get an error message.
It's the same when I use a shape to display the map.
Sheets("Sheet1").Shapes("gMap").Fill.UserPicture theURL
Is there another way to display a google static map in Excel?
My URL I want to display could be <= 5000 characters. It's a map with a polyline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`?v?b`?j?t?`?b?j?p?h?j?b?b?z?``?v?``?h?j`??p`??f?j?z?|?b?v?a?h?b?v?r?z?m?``?w?b?c?~?{?f?e?b`?}?``?q?h?a?d`?w?v?d?
Both URLs work when you copy them to your browser.
Here is an example using #Omegastripes idea of downloading to a folder first. My function in the comments, which converted to tinyURL also worked but can't be posted. Downloading to a folder is more robust however.
Note:
If on 32-bit remove the word PtrSafe
Code:
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
Private Sub DownloadPic()
Dim url As String
Dim fileLocation As String
url = "https://maps.googleapis.com/maps/api/staticmap?size=600x600&path="
url = url & "enc%3Amym~h?g~uz??e?a?q?a?k?c?u?e?s?i?}?m?{?c?s?a?i?a?i?a?i?a?i?a?g?a?o?a?g?c?q?a?i?g?c?a?i?m?w?a?q?q?s?c?i?s?u?e?q?m?y?e?q?o?}?q?w?a?g?o?w?c?g?e?q?a?g?k?o?o?y?m?w?q?w?a?g?q?u?a?g?i?y?m?s?a?g?k?y?a?g?i?w?a?g?e?{??q?c?y?g?w?a?e?e?u?g?g?m?}?a?g?m?{?o?y?k?a?i?a?i?c?o?s?u?q?i?y?o?}?c?y?c?k?a?o?k?{?c?o?o?w?a?g?m?w?m?y?a?g?m?g?i?y?q?{?c?o?e?u?o?m?a?g?e?c?d?u?f?c?z??b?b??b?f`?a?f??h?`?z??h`?d?|??z?c?f`?i?j??d`??d??z??p?c?d`?i?d??h`?c?h??d`?c?z?a?b??d`?c?~?i?v?k?h?c?b`?k?j?e?h??~?n?b`?c?d??d`?i?b`?u?f?u?`?o?e?u`?q?w`?q?w`?c?e?q?g`?o?y?k?k?}?y?w?g`?s?}?g?i?}?i`?e?e?u?g`?a?e?s?m`?i?c`?c?o?e?{?a?m??k?e?c`?e?a`?a?g?u?o`?a?g?a?e?m?i`?i?{???q?o`?k?w?e?m?m?m`?m?i`?u?m`?c?e?e?m?s?u`??q?i?y?y?k`?a?e?u?q`?q?w`?a?i?q?w`?m?w`?u?u`?s?s`?u?s`?c?g?s?q`?s?q`?e?k?m?m`?q?u`?c?e?o?c`?g?}?b?ca?c?{?m?c?o?n?w?|?c?b?u?r?y?d`?q?v`?s?|`?u?r`?g?n?k?v?g?l?i?n?u?m?a`?u?e?a?g`?y?e?c?e`?y?e?c?g`?u"
url = url & "?i?e?c`?{?e?c?a`?y?g?e?a`?e`?{?}?{?h?a`?v?}?~?a?d?m?f`?g?n`?i?~`?e?z`?o?t`?c?f?m?|`?a"
url = url & "?f?i?|`?k?v`?g?|`??v?a?~?c?z?`?ba??|?c?d?}?r?}?f`?q?r?e?l?m?l?a`?z?i?d?}?n?y?h?g?j?e?l?e?v?g?x`??|`?d?|`?`?l`?e?v`?d?z`?`?n?j?x`??f?h?z`??f?j?|`?b?t?f?v`?h?v`??f?p?|`?d?p?h?t`??v`?g?|`?c?p??~`?b?l?f??n??`?d?n?x?h?n?p?d`?r?l`?j?v?r?l`?t?p`?h?r?b?d?f?r?f?x?b?l?`?l?b?l?f?d`?`?f?b?h`?`?z??h?c?l`??f?c?l`?j?p`?n?r`?b?f?t?r`?d?n?f?v`?m?z?{?~?s?|?a`?|?a`?v?c?`?k?j?m?n?a`?z?c?b?a`?x?c`?|?c?b?a`?|?q?n?a`?z?e?b?y?x?{?~?`?d`?`?z?}?f`?i?d`??f??l`??r`?v?l`?t?p`?`?f?x?n`?t?~?x?d`?v?b`?j?t?`?b?j?p?h?j?b?b?z??v??h?j`??p`??f?j?z?|?b?v?a?h?b?v?r?z?m??w?b?c?~?{?f?e?b?}?``?q?h?a?d?w?v?d?"
fileLocation = "C:\Users\User\Desktop\TestFolder\Test.png"
If URLDownloadToFile(0, url, fileLocation, 0, 0) = 0 Then
Debug.Print "downloaded"
ThisWorkbook.Worksheets("Sheet1").Range("A1").Parent.Pictures.Insert fileLocation
Kill fileLocation
Else
Debug.Print "failed"
End If
End Sub
Result:
I'm trying to write a code to download a very large file that, depending on bandwidth, may take 30 minutes to download. I have a very basic script now, that typically terminates before the file is completely downloaded. Is there a way to use readystate, or something similar, to make VBA allow the entire file to download before moving on?
Here's the code:
Sub Download()
Dim strURL As String
Dim strPath As String
'~~> URL of the Path
strURL = "http://www.aeronav.faa.gov/upload_313-/terminal/DDTPPE_201612.zip"
'~~> Destination for the file
strPath = "c:\Users\username\Desktop\WebTest\database.zip"
Ret = URLDownloadToFile(0, strURL, strPath, 0, 0)
End Sub
Thanks!
You can put your URL in a cell and run the script below.
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
Sub DownloadFilefromWeb()
Dim strSavePath As String
Dim URL As String, ext As String
Dim buf, ret As Long
URL = Worksheets("Sheet1").Range("A2").Value
buf = Split(URL, ".")
ext = buf(UBound(buf))
strSavePath = "C:\Users\your_path_here\" & "DownloadedFile." & ext
ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
If ret = 0 Then
MsgBox "Download has been succeed!"
Else
MsgBox "Error"
End If
End Sub
That's if you want to loop through a range with many URLs. If you want to download just one, try it this way.
Declare Function URLDownloadToFileA Lib "urlmon" _
(ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
Sub ExampleDownload()
Dim IExpl As Object
Set IExpl = CreateObject("InternetExplorer.Application")
With IExpl
.Navigate "http://www.bom.mu/?id=80277" 'You need to change this for a variable and loop
Do Until .Readystate = 4: Loop ' Allow page to load
'Code below to find correct href link in page based on text
For Each lnk In IExpl.Application.Document.Links
If lnk.outertext = "Click Here to Open or Right Click to Download." Then Exit For
Debug.Print lnk.outertext
Next
End With
SuccessfulDownload = URLDownloadToFileA(0, lnk.href, "C:\myfilename.zip", 0, 0)
Set IExpl = Nothing
End Sub
Or, try R, which is blazing fast!! In order to get your data to download and uncompress, you need to set mode="wb"
download.file("...",temp, mode="wb")
unzip(temp, "gbr_Country_en_csv_v2.csv")
dd <- read.table("gbr_Country_en_csv_v2.csv", sep=",",skip=2, header=T)
Then, simply read the CSV from your Excel tool.
I have a table of products where there is say a pdf for a specific products user manual. I'm storing the model name and it's file path in my products table (in Access). I've created a form in Access that allows the user to search by product name and it narrows down the number of files and shows the results from the search in a list box. However my biggest problem is opening the actual PDF. It opens the file, but I have to store the file path exactly how it is and the path of the files are long. Is there a way to open the PDF hyperlinks without using the Followhyperlink command? Or is there a way that I can show only the file name of the pdf in my list box rather than the entire path name? If I change the display text in my products table it doesn't open the hyperlink, I get an error. Any help would be greatly appreciated!
Application.FollowHyperLink() has problems with security, especially when opening files on a network drive. See e.g. here: http://blogannath.blogspot.de/2011/04/microsoft-access-tips-tricks-opening.html
A better method is the ShellExecute() API function.
Essentially it looks like this (trimmed from http://access.mvps.org/access/api/api0018.htm ):
' This code was originally written by Dev Ashish.
' http://access.mvps.org/access/api/api0018.htm
Private Declare Function apiShellExecute 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 Const WIN_NORMAL = 1 'Open Normal
Private Const ERROR_SUCCESS = 32&
Public Function fHandleFile(stFile As String) As Boolean
Dim lRet As Long
lRet = apiShellExecute(hWndAccessApp(), "Open", stFile, vbNullString, vbNullString, WIN_NORMAL)
If lRet > ERROR_SUCCESS Then
' OK
fHandleFile = True
Else
Select Case lRet
' Handle various errors
End Select
fHandleFile = False
End If
End Function
Now for your listbox:
Set it to 2 columns, the first being the model name, the second the file path.
Set the column width of the second column to 0, so it will be invisible.
And in the doubleclick event, call fHandleFile with the second column (file path):
Private Sub lstManuals_DblClick(Cancel As Integer)
Call fHandleFile(Me.lstManuals.Column(1))
End Sub