We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,277 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Send text to Word

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?

3
Contributors
4
Replies
1 Week
Discussion Span
5 Months Ago
Last Updated
5
Views
Question
Answered
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
Skill Endorsements: 0

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?

john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 3

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
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25
Solved Threads: 22
Skill Endorsements: 3

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

mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 39
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 5 Months Ago by john.knapp

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

ponkhiraj
Newbie Poster
14 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1135 seconds using 2.69MB