Userform txtbox - String too long.... replace text in a .docx

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hello

Does anyone know how I can solve the issue where I have a userform with a txtbox where I can write optional information regarding a customer.

When I hit OK-button this text is put in a cell in Excel.

Later I run another code that pull this text and some other simple words into a replacement macro that opens a .docx file and replace the words one by one.

But when I come to this optional text, which can be 30 words or more, pluss lineshift etc. I get error message that the ...."string is too long"

I have read about the 255 word-limit, but I cannot see that I'm even close to that one?


Code:
Option Explicit
 'the document
Dim Inv_doc As Object
 'the application
Dim WD As Object
Dim FName As String
Dim DesktopB As String
Option Base 1
Sub AutoNameEdit()
    Const wdReplaceAll = 2
    Dim objSelection
    Dim WDarray As Variant
    Dim WDcnt As Long, myCnt As Long, i As Long
    i = 1
    WDarray = Array("txtCompName", "txtCompNo", "txtCompRef", "txtRefTitle", _
    "txtCompName", "txtStorage", "txtSpecial", "txtCompRef", "txtSafeRef", "txtStreet", "txtStreetNo", "txtPostNo", "txtCity")
    FName = ActiveWorkbook.Sheets("Ark3").RAnge("B1").Value
    DesktopB = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    
    Dim which_document As String
    which_document = DesktopB & "\SalesTools\Intro.docx"
    
             'need an instance of word
    Set WD = CreateObject("Word.Application") 'Set objWord = CreateObject("Word.Application")
    WD.Visible = True
    Set Inv_doc = WD.Documents.Open(which_document) 'Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
    Set objSelection = WD.Selection
    For myCnt = 1 To UBound(WDarray)
        objSelection.Find.Text = WDarray(myCnt)
        objSelection.Find.Forward = True
        objSelection.Find.MatchWholeWord = True
        If objSelection.Find.Execute Then
            objSelection.Find.Replacement.Text = Sheets("Ark3").Cells(i, 2).Text
            objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
        End If
        i = i + 1
    Next

    WD.Activate
    Inv_doc.SaveAs DesktopB & "\SalesTools\" & FName & " - " & Date & ".docx"
    Inv_doc.Close
    WD.Quit
    Set Inv_doc = Nothing
    Set WD = Nothing
End Sub

This is not the Userform Code, but it is this code that produces the Error-message.

Regards
Espen
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,224,884
Messages
6,181,568
Members
453,053
Latest member
Kiranm13

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