Excel 2013 VBA错误 [英] Excel 2013 VBA Error

查看:785
本文介绍了Excel 2013 VBA错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误。

 编译错误:此项目中的代码必须更新才能用于64位系统。 

VBA代码



私人声明函数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

Dim Ret As Long

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

在Excel 2010中可以正常工作。



谢谢。



编辑 >

我得到的错误是 Ret变量未定义。这是其余的代码。

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

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

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

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

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


解决方案

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



转换32位拨号到64位你通常必须向函数添加 PtrSafe ,并将一些数据类型从 Long 转换为 LongPtr (这只是一个较大的数据类型(请参阅: http://msdn.microsoft.com/en-us/library/office/gg251378.aspx



所以转换的函数会是:

  Pr ivate声明PtrSafe函数URLDownloadToFile Liburlmon_ 
别名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知道要使用哪个功能。即:

 #如果Win64然后
私有声明PtrSafe函数URLDownloadToFile Liburlmon.......
#Else
私有声明函数URLDownloadToFile Liburlmon.......
#End如果


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.

Thanks.

EDIT

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

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

解决方案

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

To convert 32Bit calls to 64Bit you generally have to add PtrSafe to the function and convert some of the data types from Long to LongPtr (which is merely a larger datatype (see: 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:

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

这篇关于Excel 2013 VBA错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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