Excel VBA代码:在x64版本中编译错误(需要'PtrSafe'属性)

10 浏览
0 Comments

Excel VBA代码:在x64版本中编译错误(需要'PtrSafe'属性)

我正在使用Win8 x64 + Office 2013 x64。\n

问题描述:

\n我有一个Excel文件,在Office (Excel) x86中完美运行。它使用瑞士天文计算文件(swedll32.dll)进行许多天文计算。\n然而,当我尝试在我的Excel x64上运行时,一打开它,VBA编辑器就会启动,并给我以下错误:\n

编译错误:
此项目中的代码必须更新以在64位系统上使用。
请检查并更新声明语句,然后使用PtrSafe属性标记它们。

\n正如我上面所说,它只能在32位的Office (Excel)中运行,但在我的x64 Excel中无法运行。然而,似乎有一种变通方法,但我是个新手,无法理解。\n我对VBA一无所知,但在互联网上搜索后,有几个网站提到对于64位的Office (Excel),我们应该将代码修改为以下格式(对于每个声明):\n

    \n

  • 原始声明:Private Declare Function
  • \n

  • 修改后的声明:Private Declare PtrSafe Function
  • \n

\n不幸的是,尽管添加了“PtrSafe”,VBA错误停止了,但是单元格中的值并没有正确显示/计算。\n来自微软关于VBA中的64位的帖子中,似乎还需要修改一些数据类型,例如:\n

    \n

  • Long → to → LongPtr
  • \n

  • LongPtr → to → LongLong
  • \n

  • 一些“Long”应该保持不变,这取决于它们的功能。
  • \n

\n这个网站(在“哪些Long应该变成LongPtr?”下)给出了一个提示,关于如何知道哪个要修改。\n我一点也不懂,也不知道应该如何才能使这在Office (Excel) x64中工作。请那些擅长此事的人帮帮我。拜托了。\n注意:\n

    \n

  • 这里只列出了出现红色的声明,表示它们包含错误。其余部分的代码没有粘贴在这里,因为它们看起来是正确的(绿色)。
  • \n

  • 为了使Excel文件在您的计算机上正常工作,请确保在提取后,\'swedll32.dll\'和\'JCalc Ver 0.2d.xls\'两个文件在同一个文件夹中。
  • \n

\n模块名称中的问题:MainCalculations\n\'Swiss Ephemeris Release 1.60 9-jan-2000\n\'\n\' Declarations for Visual Basic 5.0\n\' The DLL file must exist in the same directory, or in a system\n\' directory where it can be found at runtime\n\'\n Private Declare Function swe_azalt Lib \"swedll32.dll\" _\n Alias \"_swe_azalt@40\" ( _\n ByVal tjd_ut As Double, _\n ByVal calc_flag As Long, _\n ByRef geopos As Double, _\n ByVal atpress As Double, _\n ByVal attemp As Double, _\n ByRef xin As Double, _\n ByRef xaz As Double _\n ) As Long \'geopos必须是三个数组元素中的第一个\n \'xin必须是两个数组元素中的第一个\n \'xaz必须是三个数组元素中的第一个\n Private Declare Function swe_azalt_rev Lib \"swedll32.dll\" _\n Alias \"_swe_azalt_rev@24\" ( _\n ByVal tjd_ut As Double, _\n ByVal calc_flag As Long, _\n ByRef geopos As Double, _\n ByRef xin As Double, _\n ByRef xout As Double _\n ) As Long \'geopos必须是三个数组元素中的第一个\n \'xin必须是两个数组元素中的第一个\n \'xout必须是三个数组元素中的第一个\nPrivate Declare Function swe_calc Lib \"swedll32.dll\" _\n Alias \"_swe_calc@24\" ( _\n ByVal tjd As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x必须是六个数组元素中的第一个\n \' serr必须能够容纳256个字节\nPrivate Declare Function swe_calc_d Lib \"swedll32.dll\" _\n Alias \"_swe_calc_d@20\" ( _\n ByRef tjd As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\nPrivate Declare Function swe_calc_ut Lib \"swedll32.dll\" _\n Alias \"_swe_calc_ut@24\" ( _\n ByVal tjd_ut As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\nPrivate Declare Function swe_calc_ut_d Lib \"swedll32.dll\" _\n Alias \"_swe_calc_ut_d@20\" ( _\n ByRef tjd_ut As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\nPrivate Declare Function swe_close Lib \"swedll32.dll\" _\n Alias \"_swe_close@0\" ( _\n ) As Long\nPrivate Declare Function swe_close_d Lib \"swedll32.dll\" _\n Alias \"_swe_close_d@4\" ( _\n ByVal ivoid As Long _\n ) As Long \' argument ivoid is ignored\nPrivate Declare Sub swe_cotrans Lib \"swedll32.dll\" _\n Alias \"_swe_cotrans@16\" ( _\n ByRef xpo As Double, _\n ByRef xpn As Double, _\n ByVal eps As Double _\n )\nPrivate Declare Function swe_cotrans_d Lib \"swedll32.dll\" _\n Alias \"_swe_cotrans_d@12\" ( _\n ByRef xpo As Double, _\n ByRef xpn As Double, _\n ByRef eps As Double _\n ) As Long\nPrivate Declare Sub swe_cotrans_sp Lib \"swedll32.dll\" _\n Alias \"_swe_cotrans_sp@16\" ( _\n ByRef xpo As Double, _\n ByRef xpn As Double, _\n ByVal eps As Double _\n )\nPrivate Declare Function swe_cotrans_sp_d Lib \"swedll32.dll\" _\n Alias \"_swe_cotrans_sp_d@12\" ( _\n ByRef xpo As Double, _\n ByRef xpn As Double, _\n ByRef eps As Double _\n ) As Long\nPrivate Declare Sub swe_cs2degstr Lib \"swedll32.dll\" _\n Alias \"_swe_cs2degstr@8\" ( _\n ByVal t As Long, _\n ByVal S As String _\n )\nPrivate Declare Function swe_cs2degstr_d Lib \"swedll32.dll\" _\n Alias \"_swe_cs2degstr_d@8\" ( _\n ByVal t As Long, _\n ByVal S As String _\n ) As Long\nPrivate Declare Sub swe_cs2lonlatstr Lib \"swedll32.dll\" _\n Alias \"_swe_cs2lonlatstr@16\" ( _\n ByVal t As Long, _\n ByVal pchar As Byte, _\n ByVal mchar As Byte, _\n ByVal S As String _\n )\nPrivate Declare Function swe_cs2lonlatstr_d Lib \"swedll32.dll\" _\n Alias \"_swe_cs2lonlatstr_d@16\" ( _\n ByVal t As Long, _\n ByRef pchar As Byte, _\n ByRef mchar As Byte, _\n ByVal S As String _\n ) As Long\nPrivate Declare Sub swe_cs2timestr Lib \"swedll32.dll\" _\n Alias \"_swe_cs2timestr@16\" ( _\n ByVal t As Long, _\n ByVal sep As Long, _\n ByVal supzero As Long, _\n ByVal S As String _\n )\nPrivate Declare Function swe_cs2timestr_d Lib \"swedll32.dll\" _\n Alias \"_swe_cs2timestr_d@16\" ( _\n ByVal t As Long, _\n ByVal sep As Long, _\n ByVal supzero As Long, _\n ByVal S As String _\n ) As Long\nPrivate Declare Function swe_csnorm Lib \"swedll32.dll\" _\n Alias \"_swe_csnorm@4\" ( _\n ByVal P As Long _\n ) As Long\nPrivate Declare Function swe_csnorm_d Lib \"swedll32.dll\" _\n Alias \"_swe_csnorm_d@4\" ( _\n ByVal P As Long _\n ) As Long\nPrivate Declare Function swe_csroundsec Lib \"swedll32.dll\" _\n Alias \"_swe_csroundsec@4\" ( _\n ByVal P As Long _\n ) As Long\nPrivate Declare Function swe_csroundsec_d Lib \"swedll32.dll\" _\n Alias \"_swe_csroundsec_d@4\" ( _\n ByVal P As Long _\n ) As Long\nPrivate Declare Function swe_d2l Lib \"swedll32.dll\" _\n Alias \"_swe_d2l@8\" ( _\n ) As Long\nPrivate Declare Function swe_d2l_d Lib \"swedll32.dll\" _\n Alias \"_swe_d2l_d@4\" ( _\n ) As Long\nPrivate Declare Function swe_date_conversion Lib \"swedll32.dll\" _\n Alias \"_swe_date_conversion@28\" ( _\n ByVal Year As Long, _\n ByVal Month As Long, _\n ByVal Day As Long, _\n ByVal utime As Double, _\n ByVal cal As Byte, _\n ByRef tjd As Double _\n ) As Long\nPrivate Declare Function swe_date_conversion_d Lib \"swedll32.dll\" _\n Alias \"_swe_date_conversion_d@24\" ( _\n ByVal Year As Long, _\n ByVal Month As Long, _\n ByVal Day As Long, _\n ByRef utime As Double, _\n ByRef cal As Byte, _\n ByRef tjd As Double _\n ) As Long\nPrivate Declare Function swe_day_of_week Lib \"swedll32.dll\" _\n Alias \"_swe_day_of_week@8\" ( _\n ByVal JD As Double _\n ) As Long\nPrivate Declare Function swe_day_of_week_d Lib \"swedll32.dll\" _\n Alias \"_swe_day_of_week_d@4\" ( _\n ByRef JD As Double _\n ) As Long\nPrivate Declare Function swe_degnorm Lib \"swedll32.dll\" _\n Alias \"_swe_degnorm@8\" ( _\n ByVal JD As Double _\n ) As Double\nPrivate Declare Function swe_degnorm_d Lib \"swedll32.dll\" _\n Alias \"_swe_degnorm_d@4\" ( _\n ByRef JD As Double _\n ) As Long\nPrivate Declare Function swe_deltat Lib \"swedll32.dll\" _\n Alias \"_swe_deltat@8\" ( _\n ByVal JD As Double _\n ) As Double\nPrivate Declare Function swe_deltat_d Lib \"swedll32.dll\" _\n Alias \"_swe_deltat_d@8\" ( _\n ByRef JD As Double, _\n ByRef deltat As Double _\n ) As Long\nPrivate Declare Function swe_difcs2n Lib \"swedll32.dll\" _\n Alias \"_swe_difcs2n@8\" ( _\n ByVal p1 As Long, _\n ByVal p2 As Long _\n ) As Long\nPrivate Declare Function swe_difcs2n_d Lib \"swedll32.dll\" _\n Alias \"_swe_difcs2n_d@8\" ( _\n ByVal p1 As Long, _\n ByVal p2 As Long _\n ) As Long\nPrivate Declare Function swe_difcsn Lib \"swedll32.dll\" _\n Alias \"_swe_difcsn@8\" ( _\n ByVal p1 As Long, _\n ByVal p2 As Long _\n ) As Long\nPrivate Declare Function swe_difcsn_d Lib \"swedll32.dll\" _\n Alias \"_swe_difcsn_d@8\" ( _\n ByVal p1 As Long, _\n ByVal p2 As Long _\n ) As Long\nPrivate Declare Function swe_difdeg2n Lib \"swedll32.dll\" _\n Alias \"_swe_difdeg2n@16\" ( _\n ByVal p1 As Double, _\n ByVal p2 As Double _\n ) As Double\nPrivate Declare Function swe_difdeg2n_d Lib \"swedll32.dll\" _\n Alias \"_swe_difdeg2n_d@12\" ( _\n ByRef p1 As Double, _\n ByRef p2 As Double, _\n ByRef Diff As Double _\n ) As Long\nPrivate Declare Function swe_difdegn Lib \"swedll32.dll\" _\n Alias \"_swe_difdegn@16\" ( _\n ByVal p1 As Double, _\n ByVal p2 As Double _\n ) As Long\nPrivate Declare Function swe_difdegn_d Lib \"swedll32.dll\" _\n Alias \"_swe_difdegn_d@12\" ( _\n ByRef p1 As Double, _\n ByRef p2 As Double, _\n ByRef Diff As Double _\n ) As Long\nPrivate Declare Function swe_fixstar Lib \"swedll32.dll\" _\n Alias \"_swe_fixstar@24\" ( _\n ByVal star As String, _\n ByVal tjd As Double, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\n \' star must be able to hold 40 bytes\nPrivate Declare Function swe_fixstar_d Lib \"swedll32.dll\" _\n Alias \"_swe_fixstar_d@20\" ( _\n ByVal star As String, _\n ByRef tjd As Double, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\n \' star must be able to hold 40 bytes\nPrivate Declare Function swe_fixstar_ut Lib \"swedll32.dll\" _\n Alias \"_swe_fixstar_ut@24\" ( _\n ByVal star As String, _\n ByVal tjd_ut As Double, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\n \' star must be able to hold 40 bytes\nPrivate Declare Function swe_fixstar_ut_d Lib \"swedll32.dll\" _\n Alias \"_swe_fixstar_ut_d@20\" ( _\n ByVal star As String, _\n ByRef tjd_ut As Double, _\n ByVal iflag As Long, _\n ByRef X As Double, _\n ByVal serr As String _\n ) As Long \' x must be first of six array elements\n \' serr must be able to hold 256 bytes\n \' star must be able to hold 40 bytes\nPrivate Declare Function swe_get_ayanamsa Lib \"swedll32.dll\" _\n Alias \"_swe_get_ayanamsa@8\" ( _\n ByVal tjd_et As Double _\n ) As Double\nPrivate Declare Function swe_get_ayanamsa_d Lib \"swedll32.dll\" _\n Alias \"_swe_get_ayanamsa_d@8\" ( _\n ByRef tjd_et As Double, _\n ByRef ayan As Double _\n ) As Long\nPrivate Declare Function swe_get_ayanamsa_ut Lib \"swedll32.dll\" _\n Alias \"_swe_get_ayanamsa_ut@8\" ( _\n ByVal tjd_ut As Double _\n ) As Double\nPrivate Declare Function swe_get_ayanamsa_ut_d Lib \"swedll32.dll\" _\n Alias \"_swe_get_ayanamsa_ut_d@8\" ( _\n ByRef tjd_ut As Double, _\n ByRef ayan As Double _\n ) As Long\nPrivate Declare Sub swe_get_planet_name Lib \"swedll32.dll\" _\n Alias \"_swe_get_planet_name@8\" ( _\n ByVal ipl As Long, _\n ByVal pname As String _\n )\nPrivate Declare Function swe_get_planet_name_d Lib \"swedll32.dll\" _\n Alias \"_swe_get_planet_name_d@8\" ( _\n ByVal ipl As Long, _\n ByVal pname As String _\n ) As Long\nPrivate Declare Function swe_get_tid_acc Lib \"swedll32.dll\" _\n Alias \"_swe_get_tid_acc@0\" ( _\n ) As Double\nPrivate Declare Function swe_get_tid_acc_d Lib \"swedll32.dll\" _\n Alias \"_swe_get_tid_acc_d@4\" ( _\n ByRef X As Double _\n ) As Long\nPrivate Declare Function swe_houses Lib \"swedll32.dll\" _\n Alias \"_swe_houses@36\" ( _\n ByVal tjd_ut As Double, _\n ByVal geolat As Double, _\n ByVal geolon As Double, _\n ByVal ihsy As Long, _\n ByRef hcusps As Double, _\n ByRef ascmc As Double _\n ) As Long \' hcusps must be first of 13 array elements\n \' ascmc must be first of 10 array elements\nPrivate Declare Function swe_houses_d Lib \"swedll32.dll\" _\n Alias \"_swe_houses_d@24\" ( _\n ByRef tjd_ut As Double, _\n ByRef geolat As Double, _\n ByRef geolon As Double, _\n ByVal ihsy As Long, _\n ByRef hcusps As Double, _\n ByRef ascmc As Double _\n ) As Long \' hcusps must be first of 13 array elements\n \' ascmc must be first of 10 array elements\nPrivate Declare Function swe_houses_ex Lib \"swedll32.dll\" _\n Alias \"_swe_houses_ex@40\" ( _\n ByVal tjd_ut As Double, _\n ByVal iflag As Long, _\n ByVal geolat As Double, _\n ByVal geolon As Double, _\n ByVal ihsy As Long, _\n ByRef hcusps As Double, _\n ByRef ascmc As Double _\n ) As Long \' hcusps must be first of 13 array elements\n \' ascmc must be first of 10 array elements\nPrivate Declare Function swe_houses_ex_d Lib \"swedll32.dll\" _\n Alias \"_swe_houses_ex_d@28\" ( _\n ByRef tjd_ut As Double, _\n ByVal iflag As Long, _\n ByRef geolat As Double, _\n ByRef geolon As Double, _\n ByVal ihsy As Long, _\n ByRef hcusps As Double, _\n ByRef ascmc As Double _\n ) As Long \' hcusps must be first of 13 array elements\n \' ascmc must be first of 10 array elements\nPrivate Declare Function swe_houses_armc Lib \"swedll32.dll\" _\n Alias \"_swe_houses_armc@36\" ( _\n ByVal armc As Double, _\n ByVal geolat As Double, _\n ByVal eps As Double, _\n ByVal ihsy As Long, _\n ByRef hcusps As Double, _\n ByRef ascmc As Double _\n ) As Long \' hcusps must be first of 13 array elements\n \' ascmc must be first of 10 array elements\nPrivate Declare Function swe_houses_armc_d Lib \"swedll32.dll\" _\n Alias \"_swe_houses_armc_d@24\" ( _\n ByRef armc As Double, _\n ByRef geolat As Double, _\n ByRef eps As Double, _\n ByVal ihsy As Long, _\n ByRef hcusps As Double, _\n ByRef ascmc As Double _\n ) As Long \' hcusps must be first of 13 array elements\n \' ascmc must be first of 10 array elements\nPrivate Declare Function swe_house_pos Lib \"swedll32.dll\" _\n Alias \"_swe_house_pos@36\" ( _\n ByVal armc As Double, _\n ByVal geolat As Double, _\n ByVal eps As Double, _\n ByVal ihsy As Long, _\n ByRef xpin As Double, _\n ByVal serr As String _\n ) As Double\n \' xpin must be first of 2 array elements\nPrivate Declare Function swe_house_pos_d Lib \"swedll32.dll\" _\n Alias \"_swe_house_pos_d@28\" ( _\n ByRef armc As Double, _\n ByRef geolat As Double, _\n ByRef eps As Double, _\n ByVal ihsy As Long, _\n ByRef xpin As Double, _\n ByRef hpos As Double, _\n ByVal serr As String _\n ) As Long\n \' xpin must be first of 2 array elements\nPrivate Declare Function swe_julday Lib \"swedll32.dll\" _\n Alias \"_swe_julday@24\" ( _\n ByVal Year As Long, _\n ByVal Month As Long, _\n ByVal Day As Long, _\n ByVal hour As Double, _\n ByVal gregflg As Long _\n ) As Double\nPrivate Declare Function swe_julday_d Lib \"swedll32.dll\" _\n Alias \"_swe_julday_d@24\" ( _\n ByVal Year As Long, _\n ByVal Month As Long, _\n ByVal Day As Long, _\n ByRef hour As Double, _\n ByVal gregflg As Long, _\n ByRef tjd As Double _\n ) As Long\nPrivate Declare Function swe_lun_eclipse_how Lib \"swedll32.dll\" _\n Alias \"_swe_lun_eclipse_how@24\" ( _\n ByVal tjd_ut As Double, _\n ByVal ifl As Long, _\n ByRef geopos As Double, _\n ByRef attr As Double, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_lun_eclipse_how_d Lib \"swedll32.dll\" _\n Alias \"_swe_lun_eclipse_how_d@20\" ( _\n ByRef tjd_ut As Double, _\n ByVal ifl As Long, _\n ByRef geopos As Double, _\n ByRef attr As Double, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_lun_eclipse_when Lib \"swedll32.dll\" _\n Alias \"_swe_lun_eclipse_when@28\" ( _\n ByVal tjd_start As Double, _\n ByVal ifl As Long, _\n ByVal ifltype As Long, _\n ByRef tret As Double, _\n ByVal backward As Long, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_lun_eclipse_when_d Lib \"swedll32.dll\" _\n Alias \"_swe_lun_eclipse_when_d@24\" ( _\n ByRef tjd_start As Double, _\n ByVal ifl As Long, _\n ByVal ifltype As Long, _\n ByRef tret As Double, _\n ByVal backward As Long, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_nod_aps Lib \"swedll32.dll\" _\n Alias \"_swe_nod_aps@40\" ( _\n ByVal tjd_et As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByVal method As Long, _\n ByRef xnasc As Double, _\n ByRef xndsc As Double, _\n ByRef xperi As Double, _\n ByRef xaphe As Double, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_nod_aps_ut Lib \"swedll32.dll\" _\n Alias \"_swe_nod_aps_ut@40\" ( _\n ByVal tjd_ut As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByVal method As Long, _\n ByRef xnasc As Double, _\n ByRef xndsc As Double, _\n ByRef xperi As Double, _\n ByRef xaphe As Double, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_pheno Lib \"swedll32.dll\" _\n Alias \"_swe_pheno@24\" ( _\n ByVal tjd As Double, _\n ByVal ipl As Long, _\n ByVal iflag As Long, _\n ByRef attr As Double, _\n ByVal serr As String _\n ) As Long\nPrivate Declare Function swe_pheno_ut Lib \"swedll32.dll\" _\n Alias \"_swe_pheno_ut@24\" ( _\n ByVal tjd As Doub

0
0 Comments

Excel VBA代码:x64版本中的编译错误(需要'PtrSafe'属性)

问题的原因是32位DLL无法在64位的Office中使用。需要作者更新DLL以与64位Office版本兼容。

你在问题中提供的代码更改用于将已经为Office 64位重写的API调用进行转换(大多数Windows API已更新)。

根据Microsoft的官方文档,32位Office的ActiveX控件和COM DLL(动态链接库)将无法在64位进程中运行。

进一步研究后,发现你提供的64位DLL实际上是32位版本。因此,在64位Office上会出现错误。你可以尝试在32位Office中访问“64位”版本。

你提到的运行时错误“文件未找到:swedll32.dll”。根据这个错误信息,似乎找不到文件。请确保swedll32.dll与*.xls文件位于同一文件夹中,并将其副本放在C:\Windows\System32目录中。

问题的根本原因是32位DLL无法在64位Office中运行。解决方法是使用64位的DLL版本,并对声明进行修改,添加PtrSafe属性并更改某些数据类型。

0
0 Comments

这个问题的原因是代码中缺少"PtrSafe"属性,而在x64版本中,VBA代码需要添加该属性来确保代码的正确运行。解决方法是在函数声明前面添加"PtrSafe"。

在上述代码中,只需在第一个函数的第一行添加"PtrSafe"属性即可,代码如下:

Private Declare PtrSafe Function swe_azalt Lib "swedll32.dll" ......

另外,根据作者的提醒,还需要更新某些数据类型。例如,将"ms"更新为"millisecs"。

这样修改后的代码就可以在x64版本中正常编译和运行了。

0