将32位转换为64位 [英] Convert code for 32 bit to 64 bit

查看:256
本文介绍了将32位转换为64位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多图像,我需要在我的文件夹下载并重命名。我已经尝试了下面的宏,但是它不能在32位excel上工作,请帮助我64位工作。

  Option Explicit 

私有声明函数URLDownloadToFile Liburlmon_
别名URLDownloadToFileA(ByVal pCaller As Long,_
ByVal szURL As String,ByVal szFileName As String,_
ByVal长久以来,长期以来,ByVal lpfnCB长)

Dim Ret As Long

'~~>这是保存图像的地方。根据需要更改
Const FolderName As String =C:\Temp\

子样本()
Dim ws As Worksheet
Dim LastRow As Long,我As Long
Dim strPath As String

'~~>列表的名称
设置ws = Sheets(Sheet1)

LastRow = ws.Range(A& Rows.Count).End(xlUp)。行

对于i = 2 To LastRow'<〜〜2,因为行1有标题
strPath = FolderName& ws.Range(A& i).Value& .jpg

Ret = URLDownloadToFile(0,ws.Range(B& i).Value,strPath,0,0)

如果Ret = 0然后
ws.Range(C& i).Value =文件已成功下载
Else
ws.Range(C& i).Value =无法下载文件
End If
Next i
End Sub


解决方案

如果要导入的功能同时兼容32位和64位,则需要在声明中使用编译器指令。

 选项显式

#如果VBA7和Win64然后
私有声明PtrSafe函数URLDownloadToFile Liburlmon_
别名URLDownloadToFileA (_
ByVal pCaller As LongPtr,_
ByVal szURL As String,_
ByVal szFileName As String,_
ByVal dwReserved As LongPtr,_
ByVal lpfnCB As LongPtr _
)As Long
私人声明Ptr安全功能DeleteUrlCacheEntry LibWininet.dll_
别名DeleteUrlCacheEntryA(_
ByVal lpszUrlName As String _
)As Long
#Else
私有声明函数URLDownloadToFile Liburlmon_
别名URLDownloadToFileA(_
ByVal pCaller As Long,_
ByVal szURL As String,_
ByVal szFileName As String,_
ByVal dwReserved As Long,_
ByVal lpfnCB As Long _
)As Long
私有声明函数DeleteUrlCacheEntry LibWininet.dll_
别名DeleteUrlCacheEntryA(_
ByVal lpszUrlName As String _
)As Long
#End如果

Public Const ERROR_SUCCESS As Long = 0
Public Const BINDF_GETNEWESTVERSION As Long =& H10
public Const INTERNET_FLAG_RELOAD As Long =& H80000000
Public Const folderName As String =c:\temp\

Sub downloadImages()
Dim i As Long, ret As Long,s WAN As String,sLAN As String

与工作表(Sheet1)
对于i = 2 To .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&)

如果ret = 0然后
.Cells(i,3)=文件已成功下载
Else
.Cells(i,3)=无法下载文件
End If
Next i
结束

结束子

#如果VBA7和Win64然后告诉VBA如何编译导入的函数。 64位版本使用 PtrSafe 。以上是在32位和64位上进行了测试。


I have a lot of images which I need to download on my folder and renamed. I have tried the macro below but it is not working on 32bit excel please help me work on 64 bit.

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

'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\Temp\"

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim strPath As String

    '~~> Name of the sheet which has the list
    Set ws = Sheets("Sheet1")

    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To 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 Then
            ws.Range("C" & i).Value = "File successfully downloaded"
        Else
            ws.Range("C" & i).Value = "Unable to download the file"
        End If
    Next i
End Sub

解决方案

If you want an imported function that works with both 32-bit and 64-bit, you need to use compiler directives in the declaration.

Option Explicit

#If VBA7 And Win64 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 Long
    Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "Wininet.dll" _
      Alias "DeleteUrlCacheEntryA" ( _
        ByVal lpszUrlName As String _
      ) 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
    Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
      Alias "DeleteUrlCacheEntryA" ( _
        ByVal lpszUrlName As String _
      ) As Long
#End If

Public Const ERROR_SUCCESS As Long = 0
Public Const BINDF_GETNEWESTVERSION As Long = &H10
Public Const INTERNET_FLAG_RELOAD As Long = &H80000000
Public Const folderName As String = "c:\temp\"

Sub downloadImages()
    Dim i As Long, ret As Long, sWAN As String, sLAN As String

    With Worksheets("Sheet1")
        For i = 2 To .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 Then
                .Cells(i, 3) = "File successfully downloaded"
            Else
                .Cells(i, 3) = "Unable to download the file"
            End If
        Next i
    End With

End Sub

The #If VBA7 And Win64 Then tells VBA how to compile the imported function(s). 64-bit versions use PtrSafe. The above was tested on both 32-bit and 64-bit.

这篇关于将32位转换为64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆