Excel VBA string limit

lostweekend

New Member
Joined
Aug 25, 2016
Messages
7
Hi,

I'm trying to run a relatively simple excel to word text replace but several of my strings are over 255 characters. So far, none of the work arounds Ive attempted have panned out but I'm I dont work with VBA very frequently and am pretty clueless. Can a work around be accomplished here? My code is copied below.

Code:
'Sub TextReplace()
   
    Dim path As String
    Dim oCell  As Integer
    Dim from_text As String, to_text As String
    Dim WA As Object
   
'Set path to template
    path = "C:\Users\100156\Desktop\Tester.docx"
 
    Set WA = CreateObject("Word.Application")
    WA.Documents.Open (path)
    WA.Visible = True
 
    For oCell = 1 To 10
        from_text = Sheet1.Range("C" & oCell).Value
        to_text = Sheet1.Range("D" & oCell).Value
        With WA
            .Activate
            With .Selection.Find
              .ClearFormatting
              .Replacement.ClearFormatting
 
              .Text = from_text
              .Replacement.Text = to_text
              .Execute Replace:=wdReplaceAll
            End With
        End With
    Next
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What version of Excel are you using? While the column width maximum is 255 the "Total number of characters that a cell can contain" has been 32,767 from at least Excel 2007 forward.
 
Upvote 0
lostweekend, posting solution if others are searching for a similar solution.

I found another MrExcel Forum question that provided the solution I have used which is to set up Bookmarks in the Word document where you want to replace text. Click here to see that question/solution.

Code:
Sub TestCode()


    Dim path As String
    Dim oCell  As Integer
    Dim BkMkName As String, to_text As String
    Dim WA As Object
    
'Set path to template- MUST BE CHANGED
    path = "C:\Users\100156\Desktop\Tester2.docx"
    Set WA = CreateObject("Word.Application")
    WA.Documents.Open (path)
    WA.Visible = True


    For oCell = 2 To 5
        BkMkName = Sheet1.Range("C" & CStr(oCell)).Value
        to_text = Sheet1.Range("D" & CStr(oCell)).Value & " "
        With WA
            .Activate
            If BkMkName = "Synopsis" Then
                .ActiveDocument.Bookmarks(BkMkName).Range = to_text & vbCrLf
            Else
                .ActiveDocument.Bookmarks(BkMkName).Range = to_text
            End If  '  BkMkName = Synopsis
        End With
    Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,794
Members
449,468
Latest member
AGreen17

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