更新时间:2023-02-06 13:02:29
(未验证)
更改
此
私有声明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 Function
I 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 Function
Thanks 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 LongPtr
To
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 = 65001
To
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 LongPtr
To
Dim ccb As Variant
In 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