Excel中2013 VBA错误 [英] Excel 2013 VBA Error
问题描述
我收到以下错误。
编译错误:code在该项目必须更新,在64位系统中使用。
VBA code
显式的选项
私人声明函数URLDownloadToFile库URLMON_
别名URLDownloadToFileA(BYVAL pCaller长,_
BYVAL szURL作为字符串,BYVAL szFileName作为字符串_
BYVAL dwReserved长,BYVAL lpfnCB长)只要
昏暗惩戒只要
~~>这就是图像将被保存。变化适用
const的文件夹名称的String =C:\ TEMP \
它工作正常,在Excel 2010中。
感谢。
修改
错误我得到的是惩戒未定义变量
。下面是的code中的其余部分。
子样品()
昏暗的WS作为工作表
昏暗LASTROW长,我只要
strPath的暗淡作为字符串
~~>具有列表中的片材的名称
设置WS =表(工作表Sheet1)
LASTROW = ws.Range(A&放大器; Rows.Count).END(xlUp).Row
对于i = 2到LASTROW'< ~~ 2,因为第1行有头
strPath的=文件夹名称和放大器; ws.Range(A&放大器;我)。价值和放大器; .MP3
的Ret = URLDownloadToFile(0,ws.Range(B&安培;ⅰ)。价值,strPath的,0,0)
如果惩戒= 0则
ws.Range(C和放大器;我)。价值=文件下载成功
其他
ws.Range(C和放大器;我)。价值=无法下载文件
结束如果
接下来我
结束小组
您必须在64位版本的Office,而previously你使用的是32位版本上运行这一点。
要转换成32位的调用64位您通常需要 PTRSAFE
添加的功能和转换某些数据类型从龙
到 LongPtr
(这仅仅是一个较大的数据类型(参见:的 http://msdn.microsoft.com/en-us/library/office/gg251378.aspx )
所以,转换功能将是:
私人声明PTRSAFE功能URLDownloadToFile库URLMON_
别名URLDownloadToFileA_
(为ByRef pCaller作为LongPtr,_
BYVAL szURL作为字符串_
BYVAL szFileName作为字符串_
BYVAL dwReserve长,_
为ByRef lpfnCB作为LongPtr)_
作为LongPtr
编辑:请注意,如果你希望能够使用该两个64位和32位版本的Office,你需要使用preprocessor如果语句,这样办公室知道要使用哪个函数。即:
#如果Win64的再
私人声明PTRSAFE功能URLDownloadToFile库URLMON.......
#其他
私人声明函数URLDownloadToFile库URLMON.......
#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屋!