目的:通过动态链接库的制作,可以将WORD中的VBA代码进行封装,达到:一,保护代码的目的,避免他人通过简单方法(解密)就能窥知过程代码,进一步保护源作者的代码;二,将较复杂的代码,通过制作成动态链接库,还可以加快代码的运行速度(特别是各程序间的协同作业);三,简单化调用过程,使用活动文档中的代码数量大大降低,有利于初学者进行使用.
以下是在WORD的五个宏(五个过程),分别是: StandardNumber:功能,对所选数据(文本中或者是表格中的两者之一),进行自动格式化为千分位数据. CurrencyNumber: 功能,对所选数据(文本中或者是表格中的两者之一),进行自动格式化为人民币的货币格式数据 ScientificNumber: 功能,对所选数据(文本中或者是表格中的两者之一),进行自动格式化为科学记数法. CalValue:对所选数据进行简单四则混合运算等功能 InsertPercent: ,对所选数据(文本中或者是表格中的两者之一),进行自动补加百分号. 请记住以上五个过程名(宏名) VBE中代码如下: Sub StandardNumber() Dim i As Range, Acell As Cell, CR As Range, YN As String On Error Resume Next Application.ScreenUpdating = False If Selection.Type = 2 Then For Each i In Selection.Words If i Like "####*" = True Then If i.Next Like "." = True And i.Next(wdWord, 2) Like "#*" = True Then i.SetRange Start:=i.Start, End:=i.Next(wdWord, 2).End i = Format(i, "Standard") Else i = Format(i, "Standard") End If End If Next i ElseIf Selection.Type = 5 Then For Each Acell In Selection.Cells Set CR = ActiveDocument.Range(Acell.Range.Start, Acell.Range.End - 1) If CR Like "####*" = True Then If CR Like "####.#*" = True Then YN = Format(CR, "Standard") CR.Text = YN Else YN = Format(CR, "Standard") CR.Text = YN End If End If Next Acell Else MsgBox "您只能选定文本或者表格之一!", vbOK + vbInformation End If Application.ScreenUpdating = True End Sub Sub CurrencyNumber() Dim i As Range, Acell As Cell, CR As Range, YN As String On Error Resume Next Application.ScreenUpdating = False If Selection.Type = 2 Then For Each i In Selection.Words If i Like "####*" = True Then If i.Next Like "." = True And i.Next(wdWord, 2) Like "#*" = True Then i.SetRange Start:=i.Start, End:=i.Next(wdWord, 2).End i = Format(i, "Currency") Else i = Format(i, "Currency") End If End If Next i ElseIf Selection.Type = 5 Then For Each Acell In Selection.Cells Set CR = ActiveDocument.Range(Acell.Range.Start, Acell.Range.End - 1) If CR Like "####*" = True Then If CR Like "####.#*" = True Then YN = Format(CR, "Currency") CR.Text = YN Else YN = Format(CR, "Currency") CR.Text = YN End If End If Next Acell Else MsgBox "您只能选定文本或者表格之一!", vbOK + vbInformation End If Application.ScreenUpdating = True End Sub Sub ScientificNumber() Dim i As Range, Acell As Cell, CR As Range, YN As String On Error Resume Next Application.ScreenUpdating = False If Selection.Type = 2 Then For Each i In Selection.Words If i Like "####*" = True Then If i.Next Like "." = True And i.Next(wdWord, 2) Like "#*" = True Then i.SetRange Start:=i.Start, End:=i.Next(wdWord, 2).End i = Format(i, "Scientific") & " " Else i = Format(i, "Scientific") & " " End If End If Next i ElseIf Selection.Type = 5 Then For Each Acell In Selection.Cells Set CR = ActiveDocument.Range(Acell.Range.Start, Acell.Range.End - 1) If CR Like "####*" = True Then If CR Like "####.#*" = True Then YN = Format(CR, "Scientific") & " " CR.Text = YN Else YN = Format(CR, "Scientific") & "" CR.Text = YN End If End If Next Acell Else MsgBox "您只能选定文本或者表格之一!", vbOK + vbInformation End If Application.ScreenUpdating = True End Sub Sub CalValue() Dim MyValue As Single On Error Resume Next If Selection Like "*" & Chr(13) Then Selection.SetRange Start:=Selection.Start, End:=Selection.End - 1 MyValue = Selection.Calculate Selection.InsertAfter IIf(Abs(MyValue) < 1, "=" & Replace(MyValue, ".", "0."), "=" & MyValue) End Sub
Sub InsertPercent() Dim i As Range, MyRange As Range, ER As Long, CR As Range On Error Resume Next Application.ScreenUpdating = False If Selection.Type = 2 Then For Each i In Selection.Words mi = Trim(i) pi = Trim(i.Previous) ni = Trim(i.Next) If mi Like "*#" = True Then If ni <> "." Then i.InsertAfter "%" ElseIf pi = "." And ni <> "%" Then i.InsertAfter "%" End If End If Next i ElseIf Selection.Type = 5 Then For Each Acell In Selection.Cells Set CR = ActiveDocument.Range(Acell.Range.Start, Acell.Range.End - 1) If CR Like "*#" = True Then If CR Like "#*.*#" = True Then CR.InsertAfter "%" Else CR.InsertAfter "%" End If End If Next Acell Else MsgBox "您只能选定文本或者表格之一!", vbOK + vbInformation End If Application.ScreenUpdating = True End Sub
[此贴子已经被作者于2004-11-21 15:09:23编辑过] |