《电脑爱好者》2004、13期最新文章 Sub 字符替换() '宏名称,可修改为其他字符
With ActiveDocument.Content.Find '在当前文档中进行查找
.Text = "其它" '被替换的字符
.Replacement.Text = "其他" '替换的字符
.Execute Replace:=wdReplaceAll, Forward:=True '替换全部
End With
End Sub Sub 替换引号()
Dim Countx As Integer, i As Integer, Sh As Byte '声明变量
'以下代码统计出文中的引号数目(包括""“”)
Countx = 0
On Error Resume Next
With ActiveDocument.Content.Find
Do While .Execute(FindText:="""", Forward:=True, Format:=True) = True
Countx = Countx + 1
Loop
'以下代码判断引号是否配对出现
Sh = Countx Mod 2
If Sh <> 0 Then
MsgBox "引号不配对!"
Exit Sub '如果引号不配对,则退出宏
End If
End With
For i = 1 To Countx
Sh = i Mod 2 '求i值除2的余数
If Sh <> 0 Then '如果余数不等于0(即为奇数),则将相应的引号替换为“前z”
With ActiveDocument.Content.Find
.Text = """"
.Replacement.Text = "前z"
.Execute Replace:=wdReplaceOne, Forward:=True
End With
Else
With ActiveDocument.Content.Find '反之则将相应的引号替换为“后z”
.Text = """"
.Replacement.Text = "后z"
.Execute Replace:=wdReplaceOne, Forward:=True
End With
End If
Next '进行下一对引号的替换
With ActiveDocument.Content.Find
'以下代码将所有的“前z”替换为左引号
.Text = "前z"
.Replacement.Text = "“"
.Execute Replace:=wdReplaceAll, Forward:=True
'以下代码将所有的“后z”替换为右引号
.Text = "后z"
.Replacement.Text = "”"
.Execute Replace:=wdReplaceAll, Forward:=True
End With
End Sub |