我有一个基本的 vbscript,可以在 microsoft word 中查找和替换,但是我无法超过一定数量的字符(我认为是 256 个)。我想知道是否有人知道解决这个问题的方法。以下是我正在使用的脚本示例:
Sub FixedReplacements()
Dim Rng As Range
Dim SearchString As String
Dim EndString As String
Dim Id As String
Dim Link As String
Rng.Find.ClearFormatting
Rng.Find.Replacement.ClearFormatting
With Rng.Find
.Text = "" 'find text won't exceed character limitation'
.Replacement.Text = "" 'replace text won't exceed character limitation'
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Rng.Find.Execute Replace:=wdReplaceAll
要查找太长的文本,请搜索字符串的前 255 个字符,然后将找到的范围扩展到其他字符。或者将字符串分解为 255 个字符的“位”并连续搜索它们(始终将第一个找到的范围扩展到每个后续找到的范围的终点)。
可以使用太长的文本进行替换,但不能使用Replace
。相反,在循环中将字符串值分配给找到的范围。 (注意Wrap
需要设置为wdFindStop
。) 例子
Dim bFound As Boolean
Dim replacementText as String
Dim findText as String, excessText as String
Dim excessChars as Long, bTooLong as Boolean
findText = "Text to be found..."
If Len(findText) > 255 Then
bToolLong = True
excessText = Mid(findText, 256)
excessChars = Len(excessText)
findText = Left(findText, 255)
End If
replacementText = "some long string greater than 256 characters"
With Rng.Find
.Text = findText
'.Replacement.Text = "" 'replace text won't exceed character limitation'
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
bFound = Rng.Find.Execute
Do While bFound
If bTooLong Then
Rng.End = Rng.End + excessChars
'Possible to check findText against the range
'If Rng.Text <> findText Then 'do something
End If
Rng.Text = replacementText
Rng.Collapse wdCollapseEnd
bFound = Rng.Find.Execute
Loop
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)