VBA Excel WideCharToMultiByte Compile error on 64-bit System

本文解决了一个在64位系统上运行的32位VBA程序出现的编译错误问题。通过在函数声明处添加PtrSafe属性,程序能够在64位系统上正常编译和运行。文章详细解释了原问题原因,并提供了修改后的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Compile Error:

The code in this project must be updated for use on64-bit systems.

Please review and update Declare statements and then mark them with the PtrSafe attribute.

1.问题:

32-bit的VBA程序,在64-bit系统上运行时,出现该编译错误。

环境:Office2010 Excel 64-bit, 64-bit Win7 OS

 

2.原因

原程序中,函数声明如下。该函数在32-bit系统中编译、运行没有问题。但是在64-bit系统中会出现编译错误。

Private Declare Function WideCharToMultiByte Lib "kernel32" _

(ByVal CodePage As Long, ByVal dwFlags As Long, lpWideCharStr As Integer, ByVal cchWideChar As Long, _

lpMultiByteStr As Byte, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long

 

3.修改

其实,提示信息中,已经说明的很清楚了。

只要在函数的声明处添加"PtrSafe"属性(无需修改其他地方),程序就正常了。

修改后程序:

Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" _

(ByVal CodePage As Long, ByVal dwFlags As Long, lpWideCharStr As Integer, ByVal cchWideChar As Long, _

lpMultiByteStr As Byte, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long

 

4.扩展

①PtrSafe<Keyword>

https://msdn.microsoft.com/en-us/library/office/gg278832.aspx?f=255&MSPPError=-2147217396

Declare statements with the PtrSafe keyword is the recommended syntax. Declare statements that include PtrSafe work correctly in the VBA7 development environment on both 32-bit and 64-bit platforms only after all data types in the Declare statement (parameters and return values) that need to store 64-bit quantities are updated to use LongLong for 64-bit integrals or LongPtr for pointers and handles. To ensure backwards compatibility with VBA version 6 and earlier use the following construct:

 

②LongPtr Data Type

https://msdn.microsoft.com/en-us/library/office/gg251378.aspx?f=255&MSPPError=-2147217396

LongPtr (Long integer on 32-bit systems, LongLong integer on 64-bit systems) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647 on 32-bit systems; and signed 64-bit (8-byte) numbers ranging in value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 on 64-bit systems.

转载于:https://www.cnblogs.com/iluzhiyong/p/4303113.html

### VBA 中将 UTF-8 编码字符串转换为 ANSI 编码 为了在 VBA 中实现从 UTF-8 到 ANSI 的编码转换,可以利用 Windows API 函数 `MultiByteToWideChar` 和 `WideCharToMultiByte` 来完成这一过程。具体来说,先通过 `MultiByteToWideChar` 把 UTF-8 字符串转化为宽字符 (Unicode),再借助 `WideCharToMultiByte` 将其转变为指定的 ANSI 编码格式。 下面是一个完整的 VBA 子程序示例,用于执行此操作: ```vb Private Declare PtrSafe Function MultiByteToWideChar Lib "kernel32" _ (ByVal CodePage As Long, ByVal dwFlags As Long, lpMultiByteStr As Any, _ ByVal cchMultiByte As Long, ByVal lpWideCharStr As Any, ByVal cchWideChar As Long) As Long Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" _ (ByVal CodePage As Long, ByVal dwFlags As Long, lpWideCharStr As Any, _ ByVal cchWideChar As Long, ByVal lpMultiByteStr As Any, ByVal cbMultiByte As Long, _ ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long Function Utf8ToAnsi(Utf8String As String) As String Dim UnicodeBuffer() As Byte Dim AnsiBuffer() As Byte Dim BufferSize As Long Dim ResultLen As Long ' Convert from UTF-8 to Unicode first. BufferSize = MultiByteToWideChar(CP_UTF8, 0&, Utf8String, -1, ByVal 0&, 0&) ReDim UnicodeBuffer(BufferSize * 2 - 1) Call MultiByteToWideChar(CP_UTF8, 0&, Utf8String, -1, UnicodeBuffer(0), BufferSize) ' Then convert from Unicode to ANSI code page. BufferSize = WideCharToMultiByte(CP_ACP, 0&, UnicodeBuffer(0), BufferSize, ByVal 0&, 0, vbNullString, False) ReDim AnsiBuffer(BufferSize - 1) Call WideCharToMultiByte(CP_ACP, 0&, UnicodeBuffer(0), BufferSize, AnsiBuffer(0), BufferSize, vbNullString, False) Utf8ToAnsi = Mid$(StrConv(AnsiBuffer, vbUnicode), 1, BufferSize - 1) End Function ``` 上述代码定义了一个名为 `Utf8ToAnsi` 的函数,该函数接收一个 UTF-8 编码的字符串作为输入参数,并返回经过转换后的 ANSI 编码字符串[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值