How to overcome VBA ms.word-replace-macro 255 limitation
Hi all,
I've been searching for a solution for this problem for days.
I'm not using the vba replace function instead I'm using the word
replace macro.
Problem is this macro will not accept text exceeding 255 characters.
Note: Line 6 where value inserted to "replacement.text" assigned to
"replacement_"
And error occurs in this scenario.
Is there a way to insert more than 255 characters using the ms.word-
replace macro below?
I need to use the ms.word-replace macro below, because it can insert
the data in right places in the document.
Please help me on this, thanks!
Code:
Word Replace Macro
================
Private Sub Replacer(target_, replacement_)
'Dim replacement_ As Long
With Selection.Find
.Text = target_
.replacement.Text = replacement_ <----Error Prompt - String too Long
.replacement.Font.Hidden = False
.Execute Replace:=wdReplaceOne, Forward:=True
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Call StartPointer
End Sub
Re: How to overcome VBA ms.word-replace-macro 255 limitation
where did you get this Macro From ?
the property for is the issue. i would imagine that you will need to code some sort of replace function to overcome this as it seems like a restriction with selection object.
i'm sure there will be some samples of how to find an replace data in word on here try a search.
David
Re: How to overcome VBA ms.word-replace-macro 255 limitation
One logic (untested) that you can follow is
1) Split the replacement string in two parts
2) Replace the Search string with the first part of the replacement string
3) Now search for the first part of the replacement string and add (not replace) the 2nd string to it...
Hope I make sense...
Re: How to overcome VBA ms.word-replace-macro 255 limitation
Davadvice,
hmm..i got this macro from the ctrl-f function at microsoft word. I used the macro recorder to extract the syntax.
But it seems that the "find" object members .replacement.text has this 255 character limitation.
I'did as search on the web on this, but i could find a compatible one, or i got lost in the code.
Koolaid,
You logic is noted, in fact before reading your posting I'm almost completed the actual coding. I'll post it here to share. if it works. haha
thanks guys! other avenues are also welcomed.
Re: How to overcome VBA ms.word-replace-macro 255 limitation
Hi,
If you can find and erase the text, your pointer should still be in the documents range? If it is, you can inserttext at a point in the range, which should be the >255 characters you want.
Not sure if this makes sense... hehe but might be another option?
Re: How to overcome VBA ms.word-replace-macro 255 limitation
TADA!
Code:
Private Function longReplacer(target_, replacement_)
Dim r_List As Object
Dim replacement_edit As String
Dim limit_ As Integer
Dim dynlimit_ As Integer
Dim i_ As Integer
Dim start_ As Integer
Dim loop_ As Integer
Dim parts_ As String
Dim continue_ As String
dynlimit_ = 0
i_ = 0
start_ = 1
limit_ = (255 - 15)
continue_ = "<#@187985515@#>"
'Create add dictionary
'Set r_List = CreateObject("Scripting.Dictionary") 'Adding via array.. adding via loop easier
replacement_edit = replacement_
'Special Round+Increment function for accounting extra text
loop_ = Abs(Len(replacement_) / limit_)
If (Len(replacement_) / limit_) - loop_ > 0 Then
loop_ = loop_ + 1
End If
dynlimit_ = 1
'MsgBox loop_ & "<loop" & " " & Len(replacement_) & "<len" & " " & raw_loop_ & "<raw" & " " & (Len(replacement_) / limit_) & "<?"
If Len(replacement_) >= limit_ Then
For i_ = 1 To loop_
If Len(replacement_edit) >= limit_ Then
parts_ = Mid(replacement_edit, 1, limit_)
'MsgBox Len(parts_) & vbCrLf & vbCrLf & parts_ & vbCrLf & vbCrLf & Len(replacement_edit) & vbCrLf & vbCrLf & replacement_edit & vbCrLf & vbCrLf & Len(replacement_) & vbCrLf & vbCrLf & replacement_
'remainder
replacement_edit = Right(replacement_edit, Len(replacement_edit) - limit_)
'MsgBox Len(parts_) & vbCrLf & vbCrLf & parts_ & vbCrLf & vbCrLf & Len(replacement_edit) & vbCrLf & vbCrLf & replacement_edit & vbCrLf & vbCrLf & Len(replacement_) & vbCrLf & vbCrLf & replacement_
dynlimit_ = dynlimit_ + limit_
Else
parts_ = replacement_edit
End If
If i_ = 1 Then
'MsgBox Len(parts_ & continue_)
Call Replacer(target_, parts_ & continue_)
Else
'MsgBox parts_
If i_ <> loop_ Then
Call Replacer(continue_, parts_ & continue_)
Else
Call Replacer(continue_, parts_)
End If
End If
'replace the target_ & add a continue_code
Next
Else
Call Replacer(target_, replacement_)
End If
End Function
Re: How to overcome VBA ms.word-replace-macro 255 limitation
Quote:
Originally Posted by
Ecniv
Hi,
If you can find and erase the text, your pointer should still be in the documents range? If it is, you can inserttext at a point in the range, which should be the >255 characters you want.
Not sure if this makes sense... hehe but might be another option?
But interesting enough! how do i insert text after a replacement? I'tried to select.typetext "xxx" but the "xxx" print appeared on the top of the document.
Can you provide a sample code that words with my "replacer" function?
Re: How to overcome VBA ms.word-replace-macro 255 limitation
Only vaguely remembering this but I think it is something like:
Code:
set rng=doc.range(lngStart,lngEnd) 'start and end are numbers - char position in doc
rng.inserttext("text to add")
If you type inserttext and press f1 the word help file should give further info...
Sorry, its
1 Attachment(s)
Re: How to overcome VBA ms.word-replace-macro 255 limitation
I think the attached will demonstrate...
Have a look and see if this does what you need...
There may be problems with the find tho ;) depending on your document
Re: How to overcome VBA ms.word-replace-macro 255 limitation