2013-07-02 135 views
6

我收到以下错误。Excel 2013 VBA错误

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 

回答

9

您必须在Office的64位版本上运行此功能,而以前您使用的是32位版本。

要转换32位调用64位您通常需要添加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位版本,则需要如果语句,这样办公室知道要使用哪个函数使用预处理即:

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

非常感谢,但我也应该添加其他代码。我之前得到的错误是修复感谢,但我得到错误,'Ret'变量未定义。 – Mowgli

+1

没有probs。您还需要将'Ret'变量从'Long'转换为'LongPtr',因为这是该函数的新返回类型。 (如果它是一个全局变量,那么使用'Public'(或'Private'作为该模块)而不是'Dim') – CuberChase

+0

嗨,对不起,我刚刚有时间。但我尝试添加LongPtr,并尝试公开但它没有工作。我该怎么办?谢谢 – Mowgli

2

MSDN reference

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

添加PtrSafe关键字。