string parameter too long error

dean.rogers

New Member
Joined
Apr 6, 2012
Messages
31
Hey all,

I am running into this error which is making things difficult when trying to find and replace.

The error is:
Run-time error '5854':
String parameter too long

I take it there is a character limit when using the find and replace and if there is a way around it, it would be GREAT!!

here is the code that i currently use to find/replace from excel to word:
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:Templates\Template1.docx")
Set wrdDocSelection = wrdApp.Selection

With wrdDocSelection.Find
.Text = "[company_name]"
.MatchWholeWord = False
.Replacement.Text = ActiveWorkbook.Sheets("Sheet2").Range("D3")
.Execute , , , , , , , , , , wdReplaceAll

Thanks for the help
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
That is setting wrdDoc as the word.document as an object
Same goes for wrdApp. The .selection is allowing me to use the .find feature in the word app so I can execute a find and replace.
 
Upvote 0
The issue is when the .text = "[string_parameter_that_is_too_long]"
and the .Range("D4") is a 3-4 sentence paragraph, it returns the error 5854: String Parameter too long. I have researched online before and have read that there is a limit to the amount of characters when doing find/replace but there is a way around it. I just havent been able to find it yet. Not sure if anyone knows of a way.
 
Upvote 0
Through testing, it would appear that the maximum length of the replacement text is 255 characters. If you are exceeding this, then you may need to run multiple replaces. You could do it like this (this replaces your section starting "With wrdDocSelection.Find", I have also inserted a couple of new variable declarations (include these at the top of your module):


Code:
Dim strReplacement As String, strFragment As String
Dim cnt As Long

'rest of code
'...

'then:


With wrdDocSelection.Find
    .Text = "[company_name]"
    .MatchWholeWord = False
    
    strReplacement = ActiveWorkbook.Sheets("Sheet1").Range("D3").Value
    If Len(strReplacement) > 255 Then
        strFragment = Mid(strReplacement, cnt + 1, 230)
        strFragment = strFragment & "@@@@@@@@@@"
        cnt = cnt + 230
        .Replacement.Text = strFragment
        .Execute , , , , , , , , , , wdReplaceAll
        .Text = "@@@@@@@@@@"
        Do
           strFragment = Mid(strReplacement, cnt + 1, 230)
           cnt = cnt + 230
           If Len(strFragment) > 0 Then strFragment = strFragment & "@@@@@@@@@@"
           .Replacement.Text = strFragment
           .Execute , , , , , , , , , , wdReplaceAll
        Loop While Len(strFragment) > 0
    Else
        .Replacement.Text = strReplacement
        .Execute , , , , , , , , , , wdReplaceAll
    End If
End With
 
Upvote 0
Try also this:
Rich (BB code):

With wrdDoc
  .Content = Replace(.Content, "[company_name]", Sheets("Sheet2").Range("D3"))
End With
 
Upvote 0
So I tried your code ZVI and it worked however, when using it, it completely wiped out every bit of formatting in the template which completely kills it. Do you know any way around that?

Firefly2012.. Your code worked flawlessly!! thank you. The only reason i would consider the other code is for the fact that the code is much shorter.

Thank you so much for your help!!
 
Upvote 0
Through testing, it would appear that the maximum length of the replacement text is 255 characters. If you are exceeding this, then you may need to run multiple replaces. You could do it like this (this replaces your section starting "With wrdDocSelection.Find", I have also inserted a couple of new variable declarations (include these at the top of your module):


Code:
Dim strReplacement As String, strFragment As String
Dim cnt As Long

'rest of code
'...

'then:


With wrdDocSelection.Find
    .Text = "[company_name]"
    .MatchWholeWord = False
    
    strReplacement = ActiveWorkbook.Sheets("Sheet1").Range("D3").Value
    If Len(strReplacement) > 255 Then
        strFragment = Mid(strReplacement, cnt + 1, 230)
        strFragment = strFragment & "@@@@@@@@@@"
        cnt = cnt + 230
        .Replacement.Text = strFragment
        .Execute , , , , , , , , , , wdReplaceAll
        .Text = "@@@@@@@@@@"
        Do
           strFragment = Mid(strReplacement, cnt + 1, 230)
           cnt = cnt + 230
           If Len(strFragment) > 0 Then strFragment = strFragment & "@@@@@@@@@@"
           .Replacement.Text = strFragment
           .Execute , , , , , , , , , , wdReplaceAll
        Loop While Len(strFragment) > 0
    Else
        .Replacement.Text = strReplacement
        .Execute , , , , , , , , , , wdReplaceAll
    End If
End With


Firefly if you could help me out one more time. This code seems to have an issue when there are more than one cell in excel that is greater than 255 characters. When I perform this, It will work for one variable is Word that is > 255 characters but the next variable it will not. Is there a way around this?
 
Upvote 0

Forum statistics

Threads
1,215,457
Messages
6,124,941
Members
449,197
Latest member
k_bs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top