第一,使用word內(nèi)置的全半角轉(zhuǎn)換 word中全角的標(biāo)點(diǎn)符號(hào)占兩個(gè)字節(jié),半角的標(biāo)點(diǎn)符號(hào)占一個(gè)字節(jié)。 在word2010版提供了包括標(biāo)點(diǎn)符號(hào)、字母、數(shù)字綜合性的全角半角轉(zhuǎn)換。單擊開始——更改大小寫命令下面的“半角和全角”,如下圖所示: 但此內(nèi)置功能有一個(gè)局限是,如果一個(gè)文檔同時(shí)有數(shù)字、字母和標(biāo)點(diǎn)符號(hào)混合,估計(jì)用起來,就沒有預(yù)期想要的效果。 比如筆者經(jīng)常需要處理網(wǎng)上的一些資料,往往是夾雜參合一些全角半角的標(biāo)點(diǎn)符號(hào),為了讓整篇文檔的標(biāo)點(diǎn)符號(hào)統(tǒng)一為全角或者是統(tǒng)一為半角,就需要進(jìn)行轉(zhuǎn)換。 很多朋友的第一想法就是使用word內(nèi)置的功能,查找替換完成。其實(shí)這個(gè)方法也是很好的,只是相對(duì)繁瑣一點(diǎn)。所有的標(biāo)點(diǎn)符號(hào)差不多有27種左右,如果每種標(biāo)點(diǎn)符號(hào)都需要處理一次,就需要進(jìn)行27次查找替換。 本文的最終目的是想介紹一個(gè)批量轉(zhuǎn)換標(biāo)點(diǎn)符號(hào)的宏來實(shí)現(xiàn)全半角轉(zhuǎn)換。 第二,全角標(biāo)點(diǎn)符號(hào)批量轉(zhuǎn)換為半角標(biāo)點(diǎn)符號(hào) 操作方法如下: 1.在word中,按ALT+F11組合鍵,打開VBE編輯器,單擊插入——模塊,在右邊的代碼編輯窗口復(fù)制下面的代碼,并關(guān)閉VBE。 2.回到word編輯界面,選中需要轉(zhuǎn)換的區(qū)域,然后單擊開發(fā)工具——宏(或者按ALT+F8鍵),打開“宏”對(duì)話框,選中“全角轉(zhuǎn)換為半角”宏,單擊“運(yùn)行”命令,即可一次性將選中區(qū)域的全角標(biāo)點(diǎn)符號(hào)批量轉(zhuǎn)換為半角標(biāo)點(diǎn)符號(hào)。 下面是具體的VBA代碼,直接復(fù)制粘貼sub 至end sub結(jié)束的所有代碼。 Sub 全角轉(zhuǎn)換為半角() '使用前需先選中要替換的區(qū)域 Dim fullshape, halfshape As String, i As Integer '定義fullshape(全角)、halfshape(半角)為字符串型,i為整數(shù)型 fullshape = ",。?“”‘’!:;" halfshape = ",.?""''!:;" For i = 1 To 10 '循環(huán)10次 With Selection.Find .Text = Mid(fullshape, i, 1) 'mid函數(shù):返回文本字符串中從指定位置開始的特定數(shù)目的字符,每次取一個(gè)標(biāo)點(diǎn)符號(hào) .Replacement.Text = Mid(halfshape, i, 1) '將用于替換的相應(yīng)位置的半角標(biāo)點(diǎn)符號(hào) .Format = False '保留替換前的字符格式 .Execute Replace:=wdReplaceAll '用半角標(biāo)點(diǎn)替換全角標(biāo)點(diǎn) End With Next i End Sub 第三,半角標(biāo)點(diǎn)符號(hào)批量轉(zhuǎn)換為全角標(biāo)點(diǎn)符號(hào) 操作方法如下: 1.在word中,按ALT+F11組合鍵,打開VBE編輯器,單擊插入——模塊,在右邊的代碼編輯窗口復(fù)制下面的代碼,并關(guān)閉VBE。 2.回到word,按ALT+F8鍵,打開“宏”對(duì)話框,選中“半角標(biāo)點(diǎn)符號(hào)轉(zhuǎn)換為全角標(biāo)點(diǎn)符號(hào)”宏,單擊“運(yùn)行”命令,即可一次性將所有的半角標(biāo)點(diǎn)符號(hào)轉(zhuǎn)換為全角標(biāo)點(diǎn)符號(hào)。 Sub 半角標(biāo)點(diǎn)符號(hào)轉(zhuǎn)換為全角標(biāo)點(diǎn)符號(hào)() '中英互譯文檔中將中文段落中的英文標(biāo)點(diǎn)符號(hào)替換為中文標(biāo)點(diǎn)符號(hào) Dim i As Paragraph, ChineseInterpunction() As Variant, EnglishInterpunction() As Variant Dim MyRange As Range, N As Byte '定義一個(gè)中文標(biāo)點(diǎn)的數(shù)組對(duì)象 ChineseInterpunction = Array("。", ",", ";", ":", "?", "!", "……", "—", "~", "〔", "〕", "《", "》", "‘", "’", "“", "”") '定義一個(gè)英文標(biāo)點(diǎn)的數(shù)組對(duì)象 EnglishInterpunction = Array(".", ",", ";", ":", "?", "!", "…", "-", "~", "(", ")", "<", ">", "'", "'", """", """") On Error Resume Next Application.ScreenUpdating = False '關(guān)閉屏幕更新 For Each i In ThisDocument.Paragraphs '遍歷文檔每個(gè)段落 If Asc(i.Range) < 0 Then '如果段落首個(gè)字符為漢字(漢字字符的ASC<0) '定義一個(gè)RANGE對(duì)象 For N = 0 To 13 '進(jìn)行14次循環(huán) Set MyRange = i.Range '定義一個(gè)RANGE對(duì)象 With MyRange.Find '查找 .ClearFormatting '清除查找格式 '查找相應(yīng)的英文標(biāo)點(diǎn),替換為對(duì)應(yīng)的中文標(biāo)點(diǎn) .Execute findtext:=EnglishInterpunction(N), replacewith:=ChineseInterpunction(N), Replace:=wdReplaceAll End With Next End If Next Selection.HomeKey wdStory With Selection.Find .ClearFormatting '清除查找格式 .Text = """" '查找" '如果查找成功并且在中文段落中,分別將其替換為“/” While .Execute If Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "“" If .Execute And Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "”" Wend End With Selection.HomeKey wdStory With Selection.Find .ClearFormatting '清除查找格式 .Text = "'" '查找' While .Execute '如果查找成功并且在中文段落中,分別將其替換為‘/’ If Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "‘" If .Execute And Asc(Selection.Paragraphs(1).Range) < 0 Then Selection.Text = "’" Wend End With '恢復(fù)屏幕更新 Application.ScreenUpdating = True End Sub |