1,105,375 Community Members

Send text to Word

Member Avatar
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
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?

Member Avatar
john.knapp
Posting Whiz in Training
221 posts since Oct 2012
Reputation Points: 25 [?]
Q&As Helped to Solve: 22 [?]
Skill Endorsements: 4 [?]
 
1
 

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
Member Avatar
mrbungle
Junior Poster in Training
82 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 1 Year Ago by john.knapp
Member Avatar
ponkhiraj
Newbie Poster
14 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: