在 VBA for Word 中查找/替换字符限制解决方法 [英] Find/Replace character limit workaround in VBA for Word

查看:114
本文介绍了在 VBA for Word 中查找/替换字符限制解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的 vbscript,可以在 microsoft word 中查找和替换,但是我无法遍历一定数量的字符(我认为是 256).我想知道是否有人知道解决此问题的方法.以下是我正在使用的脚本示例:

I have a basic vbscript which finds and replaces in microsoft word, however I am not able to go over a certain amount of characters (I think 256). I was wondering if anyone had any idea of a workaround with this problem. Below is a sample of script I'm using:

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 个字符,然后将找到的范围扩展到其他字符.OR 将字符串分解为 255 个字符的bites"并连续搜索它们(始终将第一个找到的 Range 扩展到每个后续找到的 Range 的终点).

To find text that's too long, search the first 255 characters of the string, then extend the found Range to the additional characters. OR break down the string into 255-character "bites" and search them in succession (always extending the first found Range to the end point of each succeeding found range).

可以使用太长而无法替换的文本,但不能使用Replace.相反,在循环中将字符串值分配给找到的范围.(注意Wrap需要设置为wdFindStop.) 示例

Using text that's too long for replacement can be done, but not using Replace. Instead, assign the string value to the found range, in a loop. (Note that Wrap needs to be set to wdFindStop.) Example

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

这篇关于在 VBA for Word 中查找/替换字符限制解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆