In my app, I have a text box that takes the text and transfers it to w Word doc for printing. It does this with the bookmark feature in Word.

On the Word doc, I only have "X" amount of space. The bookmark in Word will auto size and fill with as much text as I want, but I need to limit this, and only have (for example) up to 900 characters in that specific bookmark. The remainder of the text I need to go to a seperate sheet.

Here is the (stripped down) code I use to transfer the text to the Word doc as an illistration:

Public Sub printPC_Affidavit()

        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add(x.myPC_AffidavitDOTFile)

        oDoc.Bookmarks.Item("txtOffense1").Range.Text = frmPC_Affidavit.txtOffense1.Text
        oDoc.Bookmarks.Item("txtOffense2").Range.Text = frmPC_Affidavit.txtOffense2.Text

        oDoc.Bookmarks.Item("txtPC").Range.Text = frmPC_Affidavit.txtPC.Text

        oWord.PrintPreview = True ' print preview mode
        oWord = Nothing
        oDoc = Nothing
        GC.Collect()

    End Sub

So the bookmark in Word is called txtPC. I'll have to create another template in Word, and in it create another bookmark (that I'll call) txtPC2 or something.

So how do I get any characters over the 900 over to the second template?

Do you not need the bookmark after the first insert operation? A quick look online shows me that unless you redefine the bookmark after inserting the text, it's lost.

Can you not just set the page layout for different first page, then let Word decide when to page break?

Assuming your character limit is a "hard" stop, the following occurs to me:

  1. Item One: Re-use the same template instead of recreating number(x) templates
  2. Item Two: Make sure you put the bookmark back per previous link
  3. Item Three: Break the text up into "chunks" and send each chunk to a sub-procedure

Off the top code below:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
                                                            Handles btnOk.Click

        'Fill the textbox first for testing
        Dim myNewLongString As String = vbNullString
        For i = 0 To 9
            myNewLongString = myNewLongString + New String(CChar(CStr(i)), 900)
        Next
        Me.txtOffense1.Text = myNewLongString

        'working variable
        Dim strToChunk As String
        'adjust chunksize as needed
        Dim chunksize As Integer = 900
        'variable to hold textbox character count
        Dim txtlength As Integer = txtOffense1.TextLength
        'how many chunks we talking about?
        Dim numberChunks As Integer = CInt(txtlength / chunksize)
        'create an array to hold the text chunks
        Dim txtchunks(0) As String

        strToChunk = Me.txtOffense1.Text

        If (strToChunk.Length > chunksize) Then
            For i = 0 To numberChunks
                If UBound(txtchunks) < i Then
                    ReDim Preserve txtchunks(i)
                End If
                If strToChunk.Length > chunksize Then
                    txtchunks(i) = Strings.Left(strToChunk, chunksize)
                    strToChunk = strToChunk.Substring(chunksize - 1)
                Else
                    txtchunks(i) = strToChunk
                End If
            Next
        Else ' less than 900 characters in original chunk
            'txtchunks(i) should still be 0 at this point
            txtchunks(0) = strToChunk
        End If

        For i = 0 To UBound(txtchunks)
            SendDataToWord(CStr(txtchunks(i)))
        Next
        ' all done with txtchunks
        Erase txtchunks

    End Sub

    Sub SendDataToWord(stringIn As String)
        ' dummy procedure
    End Sub
commented: Great code- thanks! +2

Sorry, got busy with life, forgot to thank everyone for their replies and code. Mucho gracias!

john.knapp gives the best solution... try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.