2016-09-20 64 views
-1

我的任務是在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如何,MH,穆斯塔法,MZ,Abdel-Moneim,TM,Yousef,HA:On Three Phase Six- 開關電壓源逆變器:150°傳導模式成員IEEE,Alexandria Univercity(2006)「

現在那朵例如宏將不得不作出17標和150標和2006年標..

請人幫我這個

回答

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 

東西然後添加一些布爾值,告訴你,如果你將其設置爲子或標

+0

'測試=真 昏暗CHR作爲範圍 對於每個CHR在ActiveDocument.Range.Characters 如果則IsNumeric(唯一chr.Text)和test = True然後chr.Font.Subscript = True並且test = False 如果IsNumeric(chr.Text)和t EST = false,那麼chr.Font.Superscript = True和測試=真 接下來chr'我試過這樣......但它不會工作,,請在此 –

+0

幫助'和'是一個邏輯運算符,而不是東西來連接線。像這樣使用'And'將導致VBA將這兩個語句評估爲布爾值,而不是設置屬性。 – arcadeprecinct

+0

您能否爲我構建這個,我已經嘗試過了,但是,這對我來說可能會有所幫助 –

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 

這將引發所有的數字變成標然後調用子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 

這將把所有的數字交替換成下標和上標...... 這段代碼可以明顯優化,,,目前執行給定工作需要很長時間,,,但是在整天搜索工作方法後,這是該工程

希望這將有助於將有人談到誰找到這個:)