|
楼主 |
发表于 2010-2-17 15:21
|
显示全部楼层
以下代码是将汉字转换为编码
Sub k_GetQuWei()
On Error GoTo ErrHandle
Dim buffer() As Byte
Dim f
Dim sPos As Single
Dim s As String
Dim i As Long
Dim L1 As Integer
Dim R1 As Integer
If Selection.Type = wdSelectionNormal Then
Set MyRange = Selection.Range
Selection.Collapse wdCollapseStart
Else
Set MyRange = ActiveDocument.Range(Selection.Start, ActiveDocument.Content.End)
End If
sPos = Selection.Start
Selection.Collapse wdCollapseStart
For Each iChar In MyRange.Characters
f = ""
iChar = Selection.Text
Selection.MoveRight unit:=wdCharacter, Count:=1
a = Hex(AscW(iChar))
If "&H" & a <> &HD Then
buffer = VBA.StrConv(iChar, vbFromUnicode, &H804) 'Simplified Chinese:&H804,Chinese Taiwan:&H404,English - United States:&H409
For i = LBound(buffer) To UBound(buffer)
f = f & VBA.Right("00" & VBA.Hex(buffer(i)), 2)
Next i
L1 = CInt("&H" + Mid(f, 1, 2))
R1 = CInt("&H" + Mid(f, 3, 2))
Selection.TypeText Text:=(L1 - 160)
If R1 - 160 < 10 Then
Selection.TypeText Text:="0" & (R1 - 160)
Else
Selection.TypeText Text:=R1 - 160
End If
End If
Next
Selection.Start = sPos
Selection.Collapse wdCollapseStart
MsgBox "Job Done!"
Exit Sub
ErrHandle:
MsgBox "Error number: " + Str$(Err) + Chr(13) + Error$(Err), 48, m_Title
End Sub
Sub k_GetASCCode()
On Error GoTo ErrHandle
Dim myText As String
If Selection.Type = wdSelectionNormal Then
Set MyRange = Selection.Range
Selection.Collapse wdCollapseStart
Else
Set MyRange = ActiveDocument.Range(Selection.Start, ActiveDocument.Content.End)
End If
sPos = Selection.Start
myText = MyRange
Selection.Collapse wdCollapseStart
For Each iChar In MyRange.Characters
If InStr(myText, iChar) > 0 Then
iChar = Selection.Text
a = "&H" & Hex(AscW(iChar))
Selection.MoveRight unit:=wdCharacter, Count:=1
If a <> &HD Then
Selection.TypeText Text:=CLng(a)
End If
End If
Next
Selection.Start = sPos
Selection.Collapse wdCollapseStart
Exit Sub
ErrHandle:
MsgBox "Error number: " + Str$(Err) + Chr(13) + Error$(Err), 48, m_Title
End Sub
Sub k_GetGBKCode()
On Error GoTo ErrHandle
Dim buffer() As Byte
Dim GBKCode As String
Dim s As String
Dim i As Long
Dim sPos As Single
If Selection.Type = wdSelectionNormal Then
Set MyRange = Selection.Range
Selection.Collapse wdCollapseStart
Else
Set MyRange = ActiveDocument.Range(Selection.Start, ActiveDocument.Content.End)
End If
sPos = Selection.Start
Selection.Collapse wdCollapseStart
For Each iChar In MyRange.Characters
GBKCode = ""
iChar = Selection.Text
Selection.MoveRight unit:=wdCharacter, Count:=1
a = Hex(AscW(iChar))
If "&H" & a <> &HD Then
buffer = VBA.StrConv(iChar, vbFromUnicode, &H804) 'Simplified Chinese:&H804,Chinese Taiwan:&H404,English - United States:&H409
For i = LBound(buffer) To UBound(buffer)
GBKCode = GBKCode & VBA.Right("00" & VBA.Hex(buffer(i)), 2)
Next i
Selection.TypeText Text:=GBKCode
End If
Next
Selection.Start = sPos
Selection.Collapse wdCollapseStart
MsgBox "Job Done!"
Exit Sub
ErrHandle:
MsgBox "Error number: " + Str$(Err) + Chr(13) + Error$(Err), 48, m_Title
End Sub
Sub k_GetUnicode()
On Error GoTo ErrHandle
Dim myText As String
Dim sPos As Single
If Selection.Type = wdSelectionNormal Then
Set MyRange = Selection.Range
Selection.Collapse wdCollapseStart
Else
Set MyRange = ActiveDocument.Range(Selection.Start, ActiveDocument.Content.End)
End If
sPos = Selection.Start
myText = MyRange
Selection.Collapse wdCollapseStart
For Each iChar In MyRange.Characters
If InStr(myText, iChar) > 0 Then
iChar = Selection.Text
Selection.MoveRight unit:=wdCharacter, Count:=1
a = Hex(AscW(iChar))
If "&H" & a <> &HD Then
Selection.TypeText Text:=a
End If
End If
Next
Selection.Start = sPos
Selection.Collapse wdCollapseStart
MsgBox "Job Done!"
Exit Sub
ErrHandle:
MsgBox "Error number: " + Str$(Err) + Chr(13) + Error$(Err), 48, m_Title
End Sub |
|