|
本帖最后由 Nonenever 于 2019-8-11 08:36 编辑
希望能支持 64 Excel
自己改了一下,运行不了,不知道这个问题咋解决,类模块的内容没有看懂,网上搜索着改了一下,但一运行就死了
- Option Explicit
- Private Type GUID '自定义的类型
- Data1 As Long
- Data2 As Integer
- Data3 As Integer
- Data4(0 To 7) As Byte
- End Type
- Private Type VB_MORRSLT '自定义的类型
- dwSize As Long '4
- pwchOutput As Long '4
- cchOutput As Integer '2+(2)
- Block1 As Long '4
- pchInputPos As Long '4
- pchOutputIdxWDD As Long '4
- pchReadIdxWDD As Long '4
- paMonoRubyPos As Long '4
- pWDD As Long '4
- cWDD As Integer '2
- pPrivate As Long '4
- BLKBuff As Long '4
- End Type
- #If VBA7 Then
- Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
- (Destination As Any, Source As Any, ByVal Length As Long)
- Private Declare PtrSafe Function CLSIDFromString Lib "ole32.dll" _
- (ByVal lpszProgID As LongLong, pCLSID As GUID) As LongLong
- 'CLSIDFromString: 将字符串方式表达的GUID转换为CLSID结构
- Private Declare PtrSafe Function CoCreateInstance Lib "ole32" ( _
- rclsid As GUID, ByVal pUnkOuter As Long, _
- ByVal dwClsContext As Long, riid As GUID, _
- ByRef ppv As Long) As Long
- 'CoCreateInstance,函数名。用指定的类标识符创建一个Com对象,用指定的类标识符创建一个未初始化的对象
- Private Declare PtrSafe Function DispCallFunc Lib "oleaut32" _
- (ByVal pvInstance As Long, ByVal oVft As Long, _
- ByVal cc As Long, ByVal vtReturn As Integer, _
- ByVal cActuals As Long, prgvt As Integer, _
- prgpvarg As Long, pvargResult As Long) As Long
- Private Declare PtrSafe Sub CoTaskMemFree Lib "ole32" (pv As Long)
- 'CoTaskMemFree是一种函数,此函数用于释放被分配的内存块。
- #Else
- Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
- (Destination As Any, Source As Any, ByVal Length As Long)
- Private Declare Function CLSIDFromString Lib "ole32.dll" _
- (ByVal lpszProgID As Long, pCLSID As GUID) As Long
- Private Declare Function CoCreateInstance Lib "ole32" ( _
- rclsid As GUID, ByVal pUnkOuter As Long, _
- ByVal dwClsContext As Long, riid As GUID, _
- ByRef ppv As Long) As Long
- Private Declare Function DispCallFunc Lib "oleaut32" _
- (ByVal pvInstance As Long, ByVal oVft As Long, _
- ByVal cc As Long, ByVal vtReturn As Integer, _
- ByVal cActuals As Long, prgvt As Integer, _
- prgpvarg As Long, pvargResult As Long) As Long
- Private Declare Sub CoTaskMemFree Lib "ole32" (pv As Long)
- #End If
- Dim MSIME_GUID As GUID 'MSIME's GUID
- Dim IFELanguage_GUID As GUID 'IFELanguage's GUID
- Dim IFELanguage As Long 'Pointer to IFELanguage interface IFELanguage接口指针
- Dim PinYinArray() As String
- Dim HzLen As Integer
- Dim pvSeperator As String
- Dim pvUseSeperator As Boolean
- Dim pvInitialOnly As Boolean
- Dim pvOnlyOneChar As Boolean
- Dim pvNonChnUseSep As Boolean
- Public Function GetPinYin(HzStr As String) As String
- Dim i As Integer
- Dim Py As String
- Dim IsPy As Boolean
- GetPinYin = ""
- If IFELanguage = 0 Then
- GetPinYin = "未发现运行环境,请安装微软拼音2.0以上版本!"
- Exit Function
- End If
- If HzStr = "" Then Exit Function
- HzLen = Len(HzStr)
- Call IFELanguage_GetMorphResult(HzStr)
- For i = 1 To HzLen
- Py = PinYinArray(i)
- IsPy = Py <> ""
- If Not IsPy Then Py = Mid(HzStr, i, 1)
- GetPinYin = GetPinYin & Py & IIf(IsPy, pvSeperator, "")
- Next i
- If IsPy And pvSeperator <> "" Then GetPinYin = Left(GetPinYin, Len(GetPinYin) - 1)
- End Function
- Private Function IFELanguage_GetMorphResult(HzStr As String) As String
- Dim ret As Long
- Dim pArgs(0 To 5) As Long
- Dim vt(0 To 5) As Integer
- Dim Args(0 To 5) As Long
- Dim ResultPtr As Long
- Dim TinyM As VB_MORRSLT
- Dim Py() As Byte
- Dim i As Integer
- Dim j As Integer
- Dim PinyinIndexArray() As Integer
- IFELanguage_GetMorphResult = ""
- If IFELanguage = 0 Then Exit Function
- Args(0) = &H30000
- Args(1) = &H40000100
- Args(2) = Len(HzStr)
- Args(3) = StrPtr(HzStr)
- Args(4) = 0
- Args(5) = VarPtr(ResultPtr)
- For i = 0 To 5
- vt(i) = vbLong
- pArgs(i) = VarPtr(Args(i)) - 8
- Next
- Call DispCallFunc(IFELanguage, 20, 4, vbLong, 6, vt(0), pArgs(0), ret)
- Call MoveMemory(TinyM, ByVal ResultPtr, Len(TinyM))
- ReDim PinyinIndexArray(0 To HzLen - 1)
- ReDim PinYinArray(1 To HzLen)
- If TinyM.cchOutput > 0 Then
- ReDim Py(0 To TinyM.cchOutput * 2 - 1)
- Call MoveMemory(Py(0), ByVal TinyM.pwchOutput, TinyM.cchOutput * 2)
- IFELanguage_GetMorphResult = Py
- Call MoveMemory(PinyinIndexArray(0), ByVal TinyM.paMonoRubyPos + 2, HzLen * 2)
- j = 0
- For i = 0 To HzLen - 1
- PinYinArray(i + 1) = VBA.Mid(IFELanguage_GetMorphResult, j + 1, PinyinIndexArray(i) - j)
- j = PinyinIndexArray(i)
- Next i
- End If
- Call CoTaskMemFree(ByVal ResultPtr)
- End Function
- Private Sub IFELanguage_Open()
- Dim ret As Long
- Debug.Print " IFELanguage_Open 0:" & IFELanguage 'Excel 已停止工作 的位置
- Call DispCallFunc(IFELanguage, 4, 4, vbLong, 0, 0, 0, ret)
- Debug.Print " IFELanguage_Open 1 :" & IFELanguage
- Call DispCallFunc(IFELanguage, 12, 4, vbLong, 0, 0, 0, ret)
- Debug.Print " IFELanguage_Open 2:" & IFELanguage
- End Sub
- Private Sub IFELanguage_Close()
- Dim ret As Long
- If IFELanguage = 0 Then Exit Sub
- Call DispCallFunc(IFELanguage, 8, 4, vbLong, 0, 0, 0, ret)
- Call DispCallFunc(IFELanguage, 16, 4, vbLong, 0, 0, 0, ret)
- End Sub
- Private Function GenerateGUID()
- Dim Rlt As LongLong
- ' Debug.Print "Rlt :" & Rlt
- Rlt = CLSIDFromString(StrPtr("MSIME.China"), MSIME_GUID)
- ' StrPtr:返回真正的UNICODE字符串缓冲区的地址
- 'IFELanguage GUID = "{019F7152-E6DB-11d0-83C3-00C04FDDB82E}"
- With IFELanguage_GUID
- .Data1 = &H19F7152
- .Data2 = &HE6DB
- .Data3 = &H11D0
- .Data4(0) = &H83
- .Data4(1) = &HC3
- .Data4(2) = &H0
- .Data4(3) = &HC0
- .Data4(4) = &H4F
- .Data4(5) = &HDD
- .Data4(6) = &HB8
- .Data4(7) = &H2E
- End With
- GenerateGUID = Rlt = 0
- End Function
- Private Sub Class_Initialize() '类初始化
- IFELanguage = 0
- pvSeperator = " "
- GenerateGUID
- If CoCreateInstance(MSIME_GUID, 0, 1, IFELanguage_GUID, IFELanguage) = 0 Then Call IFELanguage_Open
- End Sub
- Private Sub Class_Terminate() '类终止
- If IFELanguage <> 0 Then Call IFELanguage_Close
- End Sub
复制代码
|
|