单词宏中的数字

时间:2016-09-20 09:34:02

标签: vba ms-word

我的任务是在word文档中查找数字,或者上标并下标它们。

我用过这个:

test = True

Dim chr As Range
For Each chr In ActiveDocument.Range.Characters

If IsNumeric(chr.Text) And test = True Then chr.Font.Subscript = True
test = False
If IsNumeric(chr.Text) And test = False Then chr.Font.Superscript = True
test = True

Next chr

这只是制作所有数字的上标,而不是在超级和子脚本之间交替

示例文字 -  “[17] Saied,M.H.,Mostafa,M.Z.,Abdel-Moneim,T.M.,Yousef,H.A。:On Three Phase Six- 开关电压源逆变器:150°导通模式。成员IEEE,Alexandria Univercity(2006)“

现在在这个例子中,宏必须制作17个上标和150个下标,2006年作为上标。

请有人帮我这个

2 个答案:

答案 0 :(得分:1)

您可以使用Range.Characters循环播放字符。像

这样的东西
Dim chr As Range
For Each chr In ActiveDocument.Range.Characters
    If IsNumeric(chr.Text) Then chr.Font.SubScript = True
Next chr

然后添加一些布尔值,告诉您是否必须将其设置为子标题或上标

答案 1 :(得分:0)

如果有人需要这个答案..这就是我所做的,

  Selection.MoveUp Unit:=wdParagraph, Count:=2000
  Dim vFindText As Variant
  Dim vReplText As Variant
  Dim i As Long
  vFindText = Array("\[", "\]  ", " \*")
  vReplText = Array("", "", "")
  With Selection
   With .Find
     .ClearFormatting
     .Replacement.ClearFormatting
     .Forward = True
     .Wrap = wdFindStop
     .MatchWholeWord = True
     .MatchSoundsLike = False
     .MatchAllWordForms = False
     .MatchWildcards = True
     .Format = True
     .MatchCase = True
     For i = 0 To UBound(vFindText)
         .Text = vFindText(i)
         .Replacement.Text = vReplText(i)
         .Execute Replace:=wdReplaceAll
     Next i
     .Text = "[0-9]{1,}"
     .Replacement.Text = "^&"
     .Replacement.Font.Superscript = True
     .Execute Replace:=wdReplaceAll, MatchWildcards:=True
 End With
 End With
 MsgBox "Numbers has finshed , calling double1"
 Call Numbers1

这会将所有数字都抛入下标,然后调用Sub Numbers1

Sub Numbers1()
Dim chr As Range
Dim firstChar As Word.Range
Dim test As Integer
test = 0
Dim firstAlphabet As Range

Selection.SetRange Start:=0, End:=100000

Set firstAlphabet = Selection.Range
For i = 2 To 1600
test = test + 2
Set firstChar = Selection.Characters(test)

If IsNumeric(firstChar.Text) Then firstChar.Font.Subscript = True

On Error Resume Next

Next i

End Sub

这会将所有数字交替放入下标和上标.... 代码可以明显优化,,,目前执行给定的工作需要很长时间,,但是在整天搜索工作方法之后,这是唯一有效的方法

希望这会有助于找到这个的人:)