【问题标题】:Excel 2013 VBA ErrorExcel 2013 VBA 错误
【发布时间】:2013-07-02 22:07:40
【问题描述】:

我收到以下错误。

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

VBA 代码

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

它在 Excel 2010 中运行良好。

谢谢。

编辑

我得到的错误是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

【问题讨论】:

    标签: excel vba 64-bit excel-2013


    【解决方案1】:

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

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

    所以转换后的函数是:

    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
    

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

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

    【讨论】:

    • 非常感谢,但我也应该添加其余代码。我之前遇到的错误已修复,谢谢,但我收到 Ret 变量未定义的错误。
    • 没有问题。您还需要将Ret 变量从Long 转换为LongPtr,因为这是该函数的新返回类型。 (如果它是一个全局变量,则使用Public(或仅用于该模块的Private)而不是Dim
    • 嗨,对不起,我刚刚有时间。但我尝试添加 LongPtr 并尝试了 Public 但它没有用。我应该怎么办?谢谢
    • 我会在你的模块中声明Ret,而不是全局声明。有什么理由在全球范围内声明它吗?唯一的另一件事是,您可能需要实际变量,而不是使用 0s 作为 pCallerlpfnCB 变量,因为它们是通过 ByRef 传递的。
    【解决方案2】:

    MSDN reference

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

    添加PtrSafe 关键字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多