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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create a word document in vb.net 1

Status
Not open for further replies.

myrgir

Programmer
Feb 7, 2006
62
CA
Hi
I would like to know how create a word document and write in it.
I already added the Com Reference Microsoft Word.

Thank you in advance
 
Here's an example of creating a word document, adding a line of text, then spell checking it.., simply use the range object to set the text in the document
Code:
Public Function zSpellChecker(ByVal sText As String) As String

' Create invisible word application
Dim wrd As Word.Application

Try
    wrd = New Word.Application
    wrd.Visible = False
Catch ex As Exception
    Return sText
End Try

' Create a new document & range
Dim doc As Word.Document = wrd.Documents.Add()
Dim rng As Word.Range = doc.Range()

'Remove the line below-this is just to show how to add text
rng.Text = "This is a test of spell cheker"

' Set the range to the current Text and active the spellchecker
rng.Text = sText
doc.Activate()
doc.CheckSpelling()

' Read the Text (after spellcheck)
sText = doc.Range().Text

' Clean Up
doc.Close(SaveChanges:=False)
wrd.Quit()

While System.Runtime.InteropServices.Marshal.ReleaseComObject(wrd) <> 0
End While

wrd = Nothing

Return sText
End Function


Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
Thanks Sweep for taking time.
I have another question...How can I save my word file on the C: for example? And how to set the name of the file?
Thanks in advance
 
but where it will be saved? I would like that it saves on the C:
 
use doc.saveas("c:\myfolder\blahblah.doc")


Sweep
...if it works, you know the rest..
Always remember that Google is your friend

curse.gif
 
Ok,
Now I'm trying to implement this in my application. But the sText make me confused a little bit.
Specially this line

sText = doc.Range().Text
I put it in commentary but now it doesn't write anything in my word document.
Could you tell me what should be stext on that line? I don't want the stringdr("Message") to be change.

Code:
' Create invisible word application
        Dim wrd As Word.Application

        Try
            wrd = New Word.Application
            wrd.Visible = False
        Catch ex As Exception
            MsgBox("An error occured while printing, please try again")
        End Try

        ' Create a new document & range
        Dim doc As Word.Document = wrd.Documents.Add()
        Dim rng As Word.Range = doc.Range()

...
...
...

For Each StringDr In StringDv
            Select Case StringDr("No")
                Case 500
                    rng.Text = StringDr("Message")
                    doc.Activate()
                    doc.CheckSpelling()

                    ' Read the Text (after spellcheck)
                    'sText = doc.Range().Text
                Case 501
                    rng.Text = StringDr("Message")
                    doc.Activate()
                    doc.CheckSpelling()

                    ' Read the Text (after spellcheck)
                    'sText = doc.Range().Text
              ...
              ...
              ...
 End Select
        Next
        doc.SaveAs("c:\OperSeq.doc")

        wrd.Quit()

        While System.Runtime.InteropServices.Marshal.ReleaseComObject(wrd) <> 0
        End While

        wrd = Nothing
 
Forget it. I didn't have data in my table. ARgggg!!

But now, I put in commentary this:

Code:
'doc.CheckSpelling()

' Read the Text (after spellcheck)
'sText = doc.Range().Text

First of all because doc.CheckSpelling doesn't seems to work as it takes very long time to execute enought that I have to stop the application.

And for the sText = doc.Range().Text well I took it off because I don't want my value in my table to be changed.

Now It seems like if I have a grammar error, when I load my word document, The Grammar corrector appear. I guess this is why you put doc.CheckSpelling(), but on my side it take very long time to execute..It didn't finish to execute since I put it in my code.

Any solution for that?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top