Replacement text is too long

C

cyberdude

Hi,

I used the following subroutine to replace some text appearing in a
document, which is the same as that held by TextBox1, by the text held
in TextBox3.

With doc_range.Find
.Text = TextBox1
.Font.Color = wdColorRed
.Replacement.Text = TextBox3
.Replacement.Font.Color = wdColorBlack
.Execute Replace:=wdReplaceAll
End With

For example, I want to replace boy_name in a document, then I'll put
boy_name in TextBox1 in a userform and put the replacement text in
Textbox3. After the replacement, all boy_name(s) in the active
document will be replaced. But, today, a error message showed up
saying that the replacement text, which is the text in TextBox3, is
too long. Can someone tell me how to overcome this problem? Thanks.

Mike
 
H

Helmut Weber

Hi Cyberdude,

quoting Jay Freedman:
"If you actually assign the replacement text to a string
and pass that to the .Replacement.Text parameter,
it is limited to 255 characters. Beyond that,
you get the error "Run-time error '5854': String too long",
and MVP Klaus Linke has written that the error sometimes
occurs with strings slightly shorter than 255."

Try:

Private Sub CommandButton1_Click()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = TextBox1
.Font.Color = wdColorRed
While .Execute
rTmp.Select' for testing using F8, remove later
rTmp.Text = TextBox2
rTmp.Font.Color = wdColorRed
rTmp.Collapse
Wend
End With
End Sub


Though 255 characters should be sufficient for a boy's name. ;-)


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top