excel - Convert code for 32 bit to 64 bit -
i have lot of images need download on folder , renamed. have tried macro below not working on 32bit excel please me work on 64 bit.
option explicit private declare function urldownloadtofile lib "urlmon" _ alias "urldownloadtofilea" (byval pcaller long, _ byval szurl string, byval szfilename string, _ byval dwreserved long, byval lpfncb long) long dim ret long '~~> images saved. change applicable const foldername string = "c:\temp\" sub sample() dim ws worksheet dim lastrow long, long dim strpath string '~~> name of sheet has list set ws = sheets("sheet1") lastrow = ws.range("a" & rows.count).end(xlup).row = 2 lastrow '<~~ 2 because row 1 has headers strpath = foldername & ws.range("a" & i).value & ".jpg" ret = urldownloadtofile(0, ws.range("b" & i).value, strpath, 0, 0) if ret = 0 ws.range("c" & i).value = "file downloaded" else ws.range("c" & i).value = "unable download file" end if next end sub
if want imported function works both 32-bit , 64-bit, need use compiler directives in declaration.
option explicit #if vba7 , win64 private declare ptrsafe function urldownloadtofile lib "urlmon" _ alias "urldownloadtofilea" ( _ byval pcaller longptr, _ byval szurl string, _ byval szfilename string, _ byval dwreserved longptr, _ byval lpfncb longptr _ ) long private declare ptrsafe function deleteurlcacheentry lib "wininet.dll" _ alias "deleteurlcacheentrya" ( _ byval lpszurlname string _ ) long #else private declare function urldownloadtofile lib "urlmon" _ alias "urldownloadtofilea" ( _ byval pcaller long, _ byval szurl string, _ byval szfilename string, _ byval dwreserved long, _ byval lpfncb long _ ) long private declare function deleteurlcacheentry lib "wininet.dll" _ alias "deleteurlcacheentrya" ( _ byval lpszurlname string _ ) long #end if public const error_success long = 0 public const bindf_getnewestversion long = &h10 public const internet_flag_reload long = &h80000000 public const foldername string = "c:\temp\" sub downloadimages() dim long, ret long, swan string, slan string worksheets("sheet1") = 2 .cells(rows.count, "a").end(xlup).row slan = foldername & .cells(i, 1).value & ".jpg" swan = .cells(i, 2).value ret = urldownloadtofile(0&, swan, slan, bindf_getnewestversion, 0&) if ret = 0 .cells(i, 3) = "file downloaded" else .cells(i, 3) = "unable download file" end if next end end sub the #if vba7 , win64 then tells vba how compile imported function(s). 64-bit versions use ptrsafe. above tested on both 32-bit , 64-bit.
Comments
Post a Comment