运行时错误5854字符串参数太长

teb*_*fet 3 vba

我使用以下宏,有时它给出参数太长的错误.我怎么解决呢?

Sub BoldFirstLetterInSentence()
Dim s As Range
Dim doc1 As Document
Dim doc2 As Document

Set doc1 = Word.Documents("Doc1.docx")
Set doc2 = Word.Documents("Doc2.docx")

For Each s In doc1.Sentences
    If s.Characters(1).Bold = True Then
        Debug.Print s
        With doc2
            Selection.Find.ClearFormatting
            With Selection.Find
                .Text = s
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            a = Selection.Find.Execute
            If a = True Then
                Selection.Font.Bold = True
            End If
        End With
    End If
Next

End Sub
Run Code Online (Sandbox Code Playgroud)

小智 7

我想出了一个答案 - 希望它可以使用3年.

在我的应用程序中,我用替换文本替换文档中所有出现的字符串"{Text}".

方法是将替换文本分解为250个字符的"块",如果还有剩余的块,则附加一个新的替换变量(第一个块为{1},第二个块为{2}等),然后重复一遍

我的代码在Word 2010 VBA中运行:

Private Sub SearchAndReplace(search As String, replace As String)

Dim i As Integer
Dim chunks As Integer
Dim chunk As String

Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst ' Go to the start of the document

With Selection.Find
    .ClearFormatting
    .MatchCase = True
    .MatchWholeWord = True

    ' We get error 5854 if the replacement text is greater than 255 characters, so need to work around
    ' How many 250 character "chunks" are there in the replacement text?
    chunks = Round(Len(replace) / 250, 0)                   ' Use 250 to allow for {1}, etc.
    If Len(replace) Mod 250 > 0 Then chunks = chunks + 1      ' Workaround because there's no Ceiling()

    If chunks = 1 Then
        .Execute FindText:="{" & search & "}", ReplaceWith:=replace, replace:=wdReplaceAll
    Else

        ' Replace existing replacement variable (e.g. {Text}) the first chunk's replacement variable (i.e. {1})
        .Execute FindText:="{" & search & "}", ReplaceWith:="{1}", replace:=wdReplaceAll

        ' Replace the text in chunks of less than 255 characters
        For i = 1 To chunks

            ' Get the
            chunk = Mid(replace, ((i - 1) * 250) + 1, 250)

            ' Add the replacement variable for the next chunk to the end of the string
            If i < chunks Then chunk = chunk & "{" & (i + 1) & "}"

            .Execute FindText:="{" & i & "}", ReplaceWith:=chunk, replace:=wdReplaceAll
        Next i

    End If
End With

End Sub
Run Code Online (Sandbox Code Playgroud)


小智 5

答案比这里给出的要简单得多。不要使用替换。只需选择要替换的文本,然后在其上键入即可。

With ActiveDocument.ActiveWindow.Selection
    .WholeStory
    .Find.ClearFormatting
    .Find.Execute FindText:="Whatever you want to replace"
    Options.ReplaceSelection = True
    .TypeText Text:="Type away to your heart's content.  This string can be as long as it needs to be.  What you're doing here is selecting the part you want to replace, then just start typing, same as you would if you were using Word for real.  Because Selection selects the word you want to replace, when you start typing, that will be replaced with the new text as you type.  No need to worry about String length limitations."
End With
Run Code Online (Sandbox Code Playgroud)