VBA - runtime error 5854 string parameter is too long

I'm getting the same error as this link below.  Seeing if anyone has a solution.

http://stackoverflow.com/questions/5050902/runtime-error-5854-string-parameter-is-too-long

See below for function I'm calling.  I'm calling this from Excel to search and replace values in a Word Document.  I have some values over 255 characters.


Sub searchReplace(ByVal fieldName As String, ByRef doc As Word.Document, ByVal newVal As Variant)

With doc.Application.Selection.Find
    .ClearFormatting
    .text = "[" & fieldName & "]"
    .Replacement.ClearFormatting
    
    .Replacement.text = IIf(IsNull(dic(fieldName)), "", newVal)
     .Execute Replace:=wdReplaceAll, Forward:=True, _
        Wrap:=wdFindContinue
        
End With

End Sub

Open in new window

LVL 16
carsRSTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mansooralia_yahooCommented:
try increasing your buffer size
0
aikimarkCommented:
You might assign the document text to a string variable in your Excel code and then invoke the Replace() function, assigning the changed string back to the document text.
0
carsRSTAuthor Commented:
>>try increasing your buffer size

It's a Word limitation.  



>>You might assign the document text to a string variable in your Excel code and then invoke the Replace() function, assigning the changed string back to the document text.

The Word document is pretty detailed with a lot of other fluff in it.  

It's the notes section that doesn't complete, since the data is more than 255 characters.  What I was thinking of doing is replacing in chunks in 255 characters.

I appreciate all the comments.  I can't test anything until tonight.  If any suggestions hit, let me know - I'll leave this open at least until tomorrow morning.
 
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

aikimarkCommented:
You should be able to limit your Replace() operations to the range of the document that contains your notes.
0
carsRSTAuthor Commented:
>>You should be able to limit your Replace() operations to the range of the document that contains your notes.

I do a lot of Excel VBA but only Word every now and then.

To explain further, I have a Word doc template that has place holders.  My Excel WB opens the Word doc (from VBA) and replaces all the place holders from select data in my Excel workbook.  All works fine until I get to the notes, since they're over 255 characters.

With that said, can you tell me a little more about what you mean?

I really appreciate the help!



0
regmigrantCommented:
You can't get around the 255 character limit so breaking the string into chunks is the only way if you want to use the replace functionality - and since the alternative is effectively writing your own its probably the simplest way to go.

I suggest testing the length of the repl\acement string and for those greater than 255 then doing a first pass with some identifiable series of short codes that won't appear in the text. You have to do this first or you run the risk of picking up a partial match af tjhe replacement string before the function completes.

so if the find string were 'Replace Me Now' but the replacement string is 300 characters you would wind up with <##==001><##==002> (for example). Then follow up with a second pass which loops through replacing each short code with an assigned section of the target string (either a simple split of 150 char each or a first one of 255 followed by the remainder) which you can manipulate with mid, len, etc.

If you wanted to be elegant you could make the whole thing recursive

 Reg

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
carsRSTAuthor Commented:
I like it.

I'll try tonight.
0
aikimarkCommented:
open your code window (Alt+F11) and in the immediate window (Ctrl+G), type Replace

Press your F1 key and read the help on this function.  It is a VB/VBA/VBScript function and isn't limited to 255 characters as is the Word Find object.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.