(转)WORD中小写金额转换成大写

转自:http://www.officefans.net/cdb/viewthread.php?tid=52631

'* +++++++++++++++++++++++++++++
'* Created By SHOUROU@OfficeFans 2007-2-14 17:44:46
'仅测试于System: Windows NT Word: 11.0 Language: 2052
'№ 0155^The Code CopyIn [ThisDocument-ThisDocument]^'
'* -----------------------------Option Explicit
Sub GetChineseNum2()
'修正了原数据中含有千分位分隔符,并加入了空格容错,允许数字中带有空格
    Dim Numeric As Currency, IntPart As Long, DecimalPart As Byte, MyField As Field, Label As String
    Dim Jiao As Byte, Fen As Byte, Oddment As String, Odd As String, MyChinese As String
    Dim strNumber As String
    Const ZWDX As String = "壹贰叁肆伍陆柒捌玖零"    '定义一个中文大写汉字常量
    On Error Resume Next    '错误忽略
    With Selection
        strNumber = VBA.Replace(.Text, " ", "")
        Numeric = VBA.Round(VBA.CCur(strNumber), 2)    '四舍五入保留小数点后两位
        '判断是否在表格中
        If .Information(wdWithInTable) Then _
           .MoveRight unit:=wdCell Else .MoveRight unit:=wdCharacter
        '对数据进行判断,是否在指定的范围内
        If VBA.Abs(Numeric) > 2147483647 Then MsgBox "数值超过范围!", _
           vbOKOnly + vbExclamation, "Warning": Exit Sub
        IntPart = Int(VBA.Abs(Numeric))    '定义一个正整数
        Odd = VBA.IIf(IntPart = 0, "", "圆")    '定义一个STRING变量
        '插入中文大写前的标签
        Label = VBA.IIf(Numeric = VBA.Abs(Numeric), "", " 负")
        '对小数点后面二位数进行择定
        DecimalPart = (VBA.Abs(Numeric) - IntPart) * 100
        Select Case DecimalPart
        Case Is = 0    '如果是0,即是选定的数据为整数
            Oddment = VBA.IIf(Odd = "", "", Odd & "整")
        Case Is < 10    '<10,即是零头是分
            Oddment = VBA.IIf(Odd <> "", "圆零" & VBA.Mid(ZWDX, DecimalPart, 1) & "分", _
                              VBA.Mid(ZWDX, DecimalPart, 1) & "分")
        Case 10, 20, 30, 40, 50, 60, 70, 80, 90    '如果是角整
            Oddment = "圆" & VBA.Mid(ZWDX, DecimalPart / 10, 1) & "角整"
        Case Else    '既有角,又有分的情况
            Jiao = VBA.Left(CStr(DecimalPart), 1)    '取得角面值
            Fen = VBA.Right(CStr(DecimalPart), 1)    '取得分面值
            Oddment = Odd & VBA.Mid(ZWDX, Jiao, 1) & "角"    '转换为角的中文大写
            Oddment = Oddment & VBA.Mid(ZWDX, Fen, 1) & "分"    '转换为分的中文大写
        End Select
        '指定区域插入中文大写格式的域
        Set MyField = .Fields.Add(Range:=.Range, Text:="= " & IntPart & " /*CHINESENUM2")
        MyField.Select    '选定域(最后是用指定文本覆盖选定区域)
        '如果仅有角分情况下,Mychinese为""
        MyChinese = VBA.IIf(MyField.Result <> "零", MyField.Result, "")
        .Text = Label & MyChinese & Oddment
    End With
End Sub

展开阅读全文

没有更多推荐了,返回首页