【发布时间】:2020-03-25 23:41:11
【问题描述】:
我正在尝试使此代码正常工作,但它会随机将我尝试替换“ID”的整个句子大写。我正在使用 ID00 数字替换文本。有时替换将全部大写。我无法弄清楚如何保持外壳应有的状态。 一个例子是:ID001 被替换为“这是一个例子。我想要这个”。它这样做:“这是一个例子。我想要这个。”
With Selection.Find
.ClearFormatting
chunks = Round(Len(replace) / 250, 0)
If Len(replace) Mod 250 > 0 Then chunks = chunks + 1
If chunks = 1 Then
.Execute FindText:=search, ReplaceWith:=replace, replace:=wdReplaceAll
Else
.Execute FindText:=search, ReplaceWith:="{1}", replace:=wdReplaceAll
' Replace the text in chunks of less than 255 characters
For i = 1 To chunks
chunk = Mid(replace, ((i - 1) * 250) + 1, 250)
If i < chunks Then chunk = chunk & "{" & (i + 1) & "}"
.Execute FindText:="{" & i & "}", ReplaceWith:=chunk, replace:=wdReplaceAll
Next i
End If
结束
【问题讨论】:
-
请提供一些用于测试的示例文本以及“好”和“坏”结果的示例,以便我们理解。
-
我已经编辑了原件以包含一个示例。基本上,它只是将替换文本中的每个字母大写。它似乎只发生在我拥有的某些短语上。不知道为什么。
-
如果代码使用
Selection.Find而不是Range.Find,则查找将使用上次在查找/替换对话框中使用的设置。可能,ALL CAPS 格式已用于替换并影响结果?在其中粘贴Replacement.ClearFormatting可能是个好主意。和/或修改代码以使用Find和Range对象。