Excel 2013 VBA 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17436646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:01:46  来源:igfitidea点击:

Excel 2013 VBA Error

excelvba64-bitexcel-2013

提问by Mowgli

I am getting following error.

我收到以下错误。

Compile error: The code in this project must be updated for use on 64-bit systems.

VBA CODE

代码

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\"

It works fine in Excel 2010.

它在 Excel 2010 中运行良好。

Thanks.

谢谢。

EDIT

编辑

Error I get is Ret Variable Not defined. Here's the rest of the code.

我得到的错误是Ret Variable Not defined。这是代码的其余部分。

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 & ".mp3"

        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

回答by CuberChase

You must be running this on a 64Bit version of Office whereas previously you were using a 32Bit version.

您必须在 64 位版本的 Office 上运行此程序,而之前您使用的是 32 位版本。

To convert 32Bit calls to 64Bit you generally have to add PtrSafeto the function and convert some of the data types from Longto LongPtr(which is merely a larger datatype (see: http://msdn.microsoft.com/en-us/library/office/gg251378.aspx)

要将 32Bit 调用转换为 64Bit,您通常必须添加PtrSafe到函数并将某些数据类型从Longto转换为LongPtr(这只是一个更大的数据类型(请参阅:http: //msdn.microsoft.com/en-us/library/office /gg251378.aspx)

So the converted function would be:

所以转换后的函数是:

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" _
    (ByRef pCaller As LongPtr, _
     ByVal szURL As String, _
     ByVal szFileName As String, _
     ByVal dwReserve As Long, _
     ByRef lpfnCB As LongPtr) _
As LongPtr

Edit: Note if you want to be able to use this on both 64Bit and 32Bit versions of Office you need to use a preprocessor If statement so Office knows which function to use. Ie:

编辑:请注意,如果您希望能够在 64 位和 32 位版本的 Office 上使用它,您需要使用预处理器 If 语句,以便 Office 知道要使用哪个函数。IE:

#If Win64 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon".......
#Else
    Private Declare Function URLDownloadToFile Lib "urlmon".......
#End If

回答by Andy G

MSDN reference

MSDN参考

Complete error message: 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. All Declare Statements must now include the PtrSafe keyword when running in 64-bit versions of Microsoft Office. The PtrSafe keyword indicates a Declare statement is safe to run in 64-bit versions of Microsoft Office. Adding the PtrSafe keyword to a Declare statement only signifies 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 using either LongLong for 64-bit integrals or LongPtr for pointers and handles.

完整的错误消息:必须更新此项目中的代码才能在 64 位系统上使用。请查看并更新 Declare 语句,然后使用 PtrSafe 属性标记它们。在 64 位版本的 Microsoft Office 中运行时,所有声明语句现在都必须包含 PtrSafe 关键字。PtrSafe 关键字指示 Declare 语句在 64 位版本的 Microsoft Office 中运行是安全的。将 PtrSafe 关键字添加到 Declare 语句仅表示 Declare 语句明确针对 64 位,语句中需要存储 64 位(包括返回值和参数)的所有数据类型仍必须修改为使用LongLong 用于 64 位积分或 LongPtr 用于指针和句柄。

Add the PtrSafekeyword.

添加PtrSafe关键字。