Excel VB代码:
Option Explicit 私有声明函数WideCharToMultiByte Libkernel32.dll(ByVal CodePage As Long,ByVal dwFlags As Long,ByVal lpWideCharStr As Long,ByVal cchWideChar As Long,ByRef lpMultiByteStr As Byte,ByVal cchMultiByte As Long,ByVal lpDefaultChar As String,ByRef lpUsedDefaultChar As Long)As Long Private Const CP_UTF8 As Long = 65001 Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122& 公共函数ToUTF8(s as String)As Byte() 如果Len(s)= 0,则退出函数 Dim ccb As Long ccb = WideCharToMultiByte(CP_UTF8,0,StrPtr(s),Len(s),ByVal 0& 0,vbNullString,ByVal 0&) 如果ccb = 0然后 Err.Raise 5,内部错误。 End If Dim b()As Byte ReDim b(1 to ccb) 如果WideCharToMultiByte(CP_UTF8,0,StrPtr(s) ,Len(s),b(LBound(b)),ccb,vbNullString,ByVal 0&)= 0然后 Err.Raise 5,内部错误。 Else ToUTF8 = b 如果 结束函数我试图添加条件#如果VBA7 和 PtrSave 到任何地方,但工作表仍然不工作。
这是我在Office 64位中尝试的代码
Option Explicit #如果VBA7然后私有声明PtrSafe函数WideCharToMultiByte Libkernel32(ByVal CodePage As Integer,ByVal dwFlags As Long,ByVal lpWideCharStr As LongPtr,ByVal cchWideChar As Long ,ByVal lpMultiByteStr As Long,ByVal cchMultiByte As LongPtr,ByVal lpDefaultChar As Long,ByVal lpUsedDefaultChar As Long)As LongPtr #Else 私有声明函数WideCharToMultiByte Libkernel32.dll(ByVal CodePage As Long,ByVal dwFlags As Long,ByVal lpWideCharStr As Long,ByVal cchWideChar As Long,ByRef lpMultiByteStr As Byte,ByVal cchMultiByte As Long,ByVal lpDefaultChar As String,ByRef lpUsedDefaultChar As Lon g)As Long #EndIf Private Const CP_UTF8 As Long = 65001 Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122& 公共函数ToUTF8(s as String)As Byte() 如果Len(s)= 0,则退出函数 Dim ccb As LongPtr ccb = WideCharToMultiByte(CP_UTF8,0,StrPtr(s),Len(s),ByVal 0& 0,vbNullString,ByVal 0&) 如果ccb = 0然后 Err.Raise 5,内部错误。 End If Dim b()As Byte ReDim b(1 to ccb)// ERROR TYPE MISMATCH on ccb 如果WideCharToMultiByte(CP_UTF8 ,0,StrPtr(s),Len(s),b(LBound(b)),ccb,vbNullString,ByVal 0&)= 0然后 Err.Raise 5,内部错误。 Else ToUTF8 = b 如果 结束函数感谢您的帮助。
解决方案(未验证)
更改
此
私有声明PtrSafe功能WideCharToMultiByte Libkernel32_ (ByVal CodePage As Integer,ByVal dwFlags As Long,ByVal lpWideCharStr _ As LongPtr,ByVal cchWideChar As Long,ByVal lpMultiByteStr As Long ,_ ByVal cchMultiByte As LongPtr,ByVal lpDefaultChar As Long,_ ByVal lpUsedDefaultChar As Long)As LongPtr私有声明PtrSafe功能WideCharToMultiByte LibKernel32(_ ByVal CodePage As LongPtr,ByVal dwflags As LongPtr,_ ByVal lpWideCharStr As LongPtr,ByVal cchWideChar As LongPtr,_ ByVal lpMultiByteStr As LongPtr,ByVal cchMultiByte As LongPtr,_ ByVal lpDefaultChar As LongP tr,ByVal lpUsedDefaultChar As LongPtr)As LongPtr
Private Const CP_UTF8 As Long = 65001
到
私有Const CP_UTF8 = 65001
这个
code> Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122&至
Private Const ERROR_INSUFFICIENT_BUFFER = 122&
这个
Dim ccb As LongPtr/ p>
Dim ccb As Variant在我建议的最后三个chnages中,我们将它们声明为Variants,因为我们不知道在不同系统上的类型。它将是 Long 或 LongPtr
I should figure out problem with excel VBA code compatibility on 64bit systems. I do not use VB language and code below is not my but I have to solve that issue.
Excel VB code:
Option Explicit Private Declare Function WideCharToMultiByte Lib "kernel32.dll" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Byte, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByRef lpUsedDefaultChar As Long) As Long Private Const CP_UTF8 As Long = 65001 Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122& Public Function ToUTF8(s As String) As Byte() If Len(s) = 0 Then Exit Function Dim ccb As Long ccb = WideCharToMultiByte(CP_UTF8, 0, StrPtr(s), Len(s), ByVal 0&, 0, vbNullString, ByVal 0&) If ccb = 0 Then Err.Raise 5, , "Internal error." End If Dim b() As Byte ReDim b(1 To ccb) If WideCharToMultiByte(CP_UTF8, 0, StrPtr(s), Len(s), b(LBound(b)), ccb, vbNullString, ByVal 0&) = 0 Then Err.Raise 5, , "Internal error." Else ToUTF8 = b End If End FunctionI have tried to add conditions #If VBA7 and PtrSave to everywhere but worksheet still does not work.
This is the code that I tried in Office 64 Bit
Option Explicit #If VBA7 Then Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Integer, ByVal dwFlags As Long, ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As LongPtr, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As LongPtr #Else Private Declare Function WideCharToMultiByte Lib "kernel32.dll" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Byte, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByRef lpUsedDefaultChar As Long) As Long #EndIf Private Const CP_UTF8 As Long = 65001 Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122& Public Function ToUTF8(s As String) As Byte() If Len(s) = 0 Then Exit Function Dim ccb As LongPtr ccb = WideCharToMultiByte(CP_UTF8, 0, StrPtr(s), Len(s), ByVal 0&, 0, vbNullString, ByVal 0&) If ccb = 0 Then Err.Raise 5, , "Internal error." End If Dim b() As Byte ReDim b(1 To ccb) // ERROR TYPE MISMATCH on ccb If WideCharToMultiByte(CP_UTF8, 0, StrPtr(s), Len(s), b(LBound(b)), ccb, vbNullString, ByVal 0&) = 0 Then Err.Raise 5, , "Internal error." Else ToUTF8 = b End If End FunctionThanks for help.
解决方案(Untested)
Change
This
Private Declare PtrSafe Function WideCharToMultiByte Lib "kernel32" _ (ByVal CodePage As Integer, ByVal dwFlags As Long, ByVal lpWideCharStr _ As LongPtr, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, _ ByVal cchMultiByte As LongPtr, ByVal lpDefaultChar As Long, _ ByVal lpUsedDefaultChar As Long) As LongPtrTo
Private Declare PtrSafe Function WideCharToMultiByte Lib "Kernel32" ( _ ByVal CodePage As LongPtr, ByVal dwflags As LongPtr, _ ByVal lpWideCharStr As LongPtr, ByVal cchWideChar As LongPtr, _ ByVal lpMultiByteStr As LongPtr, ByVal cchMultiByte As LongPtr, _ ByVal lpDefaultChar As LongPtr, ByVal lpUsedDefaultChar As LongPtr) As LongPtr
This
Private Const CP_UTF8 As Long = 65001To
Private Const CP_UTF8 = 65001
This
Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122&To
Private Const ERROR_INSUFFICIENT_BUFFER = 122&
This
Dim ccb As LongPtrTo
Dim ccb As VariantIn the last three chnages that I suggested, we are declaring them as Variants because we don't know what the type will be on different systems. It will either be Long or LongPtr