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
 
If you are trying to run this a second time within the same piece of code then you will need to initialise the cnt variable so that it starts at zero again - this can be achieved by inserting the following line in the code:

Rich (BB 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
        cnt = 0    
    Else
        .Replacement.Text = strReplacement
        .Execute , , , , , , , , , , wdReplaceAll
    End If
End With
Relevant amendment is in red.
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,216,086
Messages
6,128,734
Members
449,466
Latest member
Peter Juhnke

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