Results 1 to 10 of 10

Thread: How to overcome VBA ms.word-replace-macro 255 limitation

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    5

    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

  2. #2
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: How to overcome VBA ms.word-replace-macro 255 limitation

    where did you get this Macro From ?

    the property for
    vb Code:
    1. .replacement.Text
    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
    This is Blank

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    5

    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.

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    5

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    5

    Re: How to overcome VBA ms.word-replace-macro 255 limitation

    Quote Originally Posted by Ecniv View Post
    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?

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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
    Code:
    rng.InsertAfter
    Last edited by Ecniv; Oct 7th, 2009 at 06:08 AM. Reason: Got it wrong...

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  9. #9
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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
    Attached Files Attached Files

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    5

    Re: How to overcome VBA ms.word-replace-macro 255 limitation

    superb! Thank Ecniv

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width