Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text format problem after using MS word for spell check

Status
Not open for further replies.

Razzle00

Programmer
Sep 27, 2006
20
US
Hi,

I have a VB windows form using VS2005 that calls the MS Word object to spell check a multiline textbox on my form. The spell check works fine, however after the text is returned back to the textbox it combines all the paragraphs together and displays a carriage return character between the paragraphs. I'm not sure why it is doing that. Can anyone reproduce this behavior. And if so, why does it return the text like that?

Here is a before and after sample of what is happening here....

Before...
This is a test paragraph.

This is another test paragraph for spacing.

This is yet another test paragraph for testing the format and spacing after spell check is performed.


After Spell Check.....
This is a test paragraph.**This is another test paragraph for spacing.**This is yet another test paragraph for testing the format and spacing after spell check is performed.

where * equals a character that looks like a box that represents a carriage return.

Here is the routine I am using for spell check....

Code:
    Private Sub SpellCheck()
        If txtnotes.Text.Length > 0 Then
            ' Make a Word server object.
            Dim word_server As New Word.Application

            ' Hide the server.
            word_server.Visible = False

            ' Make a Word Document.
            Dim doc As Word.Document = _
                word_server.Documents.Add()
            Dim rng As Word.Range

            ' Make a Range to represent the Document.
            rng = doc.Range()

            ' Copy the text into the Document.
            rng.Text = txtnotes.Text

            ' Activate the Document and call its CheckSpelling
            ' method.
            doc.Activate()
            doc.CheckSpelling()

            ' Copy the results back into the TextBox,
            ' trimming off trailing CR and LF characters.
            Dim chars() As Char = {CType(vbCr, Char), CType(vbLf, Char)}
            txtnotes.Text = doc.Range().Text.Trim(chars)

            ' Close the Document, not saving changes.
            doc.Close(SaveChanges:=False)

            ' Close the Word server.
            word_server.Quit()
        End If

    End Sub

Thanks,

Razzle
 
If you build a mini-app with: a form, two textboxes, and a button. Then add this code to the button click event:
Code:
Try
   Me.TextBox2.Text = Asc(TextBox1.Text)
Catch ex As Exception
   Me.TextBox2.Text = "{error}"
End Try

Then Run the mini-app, copy the first box shaped character and paste it into the first textbox, click the button and make a note of the code. Do the same for the second box character and make a note of that.

Then with the text returned by Word (I'm guessing in a String), Add this code, replacing {FirstChar} with the first code...
Code:
TextReturned = TextReturned.Replace(Char({FirstChar}), vbCR).Replace(Char({SecondChar}), vbLF)

And that should fix it for you.


 
Thanks for the response,

I was able to fix it with this...

txtnotes.Text = txtnotes.Text.Replace(vbCr, Environment.NewLine)

However, I was curious as to why the text is being returned from MS Word this way.

Razzle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top