ActiveDocument.Range.Replace What:=punc(i), Replacement:=IIf(punc(i) = "'", "‘", IIf(punc(i) = """", "“", IIf(punc(i) = "/", "/", IIf(punc(i) = "\", "\", IIf(punc(i) = "-", "—", IIf(punc(i) = "--", "——", IIf(punc(i) = "[", "[", IIf(punc(i) = "]", "]", punc(i)))))))), MatchWholeWord:=True, ReplaceAll:=True 上述代码报错语法错误
时间: 2023-07-06 07:36:29 浏览: 75
很抱歉,有一些中文标点符号在VBA中需要用全角字符来表示,否则会出现语法错误。以下是修改后的代码:
```
Sub ReplacePunctuation()
Dim punc As Variant
Dim i As Long
Dim beforeNum As Boolean, afterNum As Boolean
'定义需要替换的标点符号
punc = Array(".", ":", ";", ",", "?", "!", "-", "--", "(", ")", "[", "]", "{", "}", "/", "\", "'", """")
'循环遍历所有标点符号
For i = LBound(punc) To UBound(punc)
'判断当前标点符号是否为冒号或句号
If punc(i) = ":" Or punc(i) = "." Then
'循环遍历文档中的所有单词
For Each w In ActiveDocument.Words
beforeNum = False
afterNum = False
'判断当前单词前后是否为数字
If IsNumeric(Left(w, 1)) Then beforeNum = True
If IsNumeric(Right(w, 1)) Then afterNum = True
'如果冒号前后都是数字或者句号前后都是数字,则不进行替换
If (punc(i) = ":" And beforeNum And afterNum) Or (punc(i) = "." And beforeNum And afterNum) Then
'不进行替换操作
Else
'将英文标点符号替换为中文标点符号
w = Replace(w, punc(i), IIf(punc(i) = ":", ":", "。"))
End If
Next w
Else
'将英文标点符号替换为中文标点符号
ActiveDocument.Range.Replace What:=punc(i), Replacement:=IIf(punc(i) = "'", "‘", IIf(punc(i) = """", "“", IIf(punc(i) = "/", "/", IIf(punc(i) = "\", "\", IIf(punc(i) = "-", "—", IIf(punc(i) = "--", "——", IIf(punc(i) = "[", "[", IIf(punc(i) = "]", "]", IIf(punc(i) = "(", "(", IIf(punc(i) = ")", ")", IIf(punc(i) = "{", "{", IIf(punc(i) = "}", "}", punc(i))))))))))), MatchWholeWord:=True, ReplaceAll:=True
End If
Next i
End Sub
```
请注意,这个代码中只替换了部分中文标点符号,如果你需要替换其他的中文标点符号,可以将代码中的`punc`数组和`ActiveDocument.Range.Replace`语句进行相应的修改。
阅读全文