【问题标题】:Find/Replace character limit workaround in VBA for Word在 VBA for Word 中查找/替换字符限制解决方法
【发布时间】:2019-01-07 23:12:05
【问题描述】:

我有一个基本的 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   

【问题讨论】:

  • 查找前 256 个字符,然后创建一个延伸到“真实”查找项的长度的 Range,将该范围的 Text 与您的查找项进行比较,然后设置Text 创建的Range 用你的匹配替换它(而不是Find.Replace)。底层Range 对象将没有字符限制。

标签: vba ms-word


【解决方案1】:

要查找过长的文本,请搜索字符串的前 255 个字符,然后将找到的 Range 扩展到其他字符。或将字符串分解为 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

【讨论】:

  • 为此,请使用@Comintern 的建议。如果有帮助,我会将它的基础知识纳入我的答案中......
  • @CindyMeister 根据 Find 文本是什么,它可能适合 通配符 Find,因此不需要过长的 Find 表达式。例如:.Text = "前几个词*后几个词",其中 .MatchWildcards = True - 在这种情况下,您还可以省略 MatchCase、MatchWholeWord、MatchSoundsLike 和 MatchAllWordForms。
  • macropod,使用通配符效果很好,我将把它合并到我的脚本中。至于@Cindy Meister 脚本几乎可以工作,由于某种原因,当它替换它时,除了前 255 个字符之外,它不会循环遍历其余的 findText,然后将其替换为指定的替换文本。对此有任何修复吗?我不知道该怎么做 Cominten 的建议。我对 VB 很陌生,因此非常感谢任何帮助!
  • @MichaelAguilera 我的代码确实使用了共产国际的建议...在If bTooLong 处设置一个断点,然后使用 F8 单步执行,看看它是否“命中”If 内的行第二次及以后?
  • @CindyMeister 抱歉搞混了。使用 F8 查看脚本时,它不会触及 if 语句。这有什么原因吗?
【解决方案2】:

Find().Text 没有限制。检查这个:

Option Explicit

Sub TestMe()

    Dim myRange As Range
    Set myRange = ActiveDocument.Content

    Dim lookFor As String
    lookFor = "The text to be searched for. Use an empty string () to search for formatting only. You can search for special characters by specifying appropriate character codes. For example, corresponds to a paragraph mark and corresponds to a tab character."
    myRange.Find.Execute FindText:=lookFor, ReplaceWith:="hello", Replace:=wdReplaceAll

End Sub

可能限制部分是a行,在要查找的字符串中没有正确解析,因此没有找到。

【讨论】:

    【解决方案3】:

    我花了很长时间寻找一个简单的解决方案;为了那些也拖网的人的利益,请考虑我的解决方案。

    我的假设是要替换的文本出现一次。

    1. 查找要替换的文本
    2. 在之后插入新文本
    3. 使用查找和替换,删除找到的文本

      With WordDoc.Content
      .Find.Execute findText:="text", Forward:=True, Wrap:=wdFindStop
      .InsertAfter Text:="newtext"
      .Find.Execute findText:="text", ReplaceWith:=""
      End With
      

    【讨论】:

    • @Chris_F。不想在其上放置一个太细的点,您的“解决方案”并没有解决 OP 的需求,即查找超过 255 个字符的字符串。此外,你对你所做的事情的态度非常低效。
    猜你喜欢
    • 2012-03-08
    • 1970-01-01
    • 2013-11-09
    • 2014-10-12
    • 2014-04-06
    • 2018-01-18
    • 2018-03-24
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多