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

Write to Word from VB.NET 1

Status
Not open for further replies.

ca8msm

Programmer
May 9, 2002
11,327
0
0
GB
If you are just doing a simple document then somethign like the following will work:
Code:
        ' Declarations
        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        Dim oTable As Word.Table
        Dim oPara1 As Word.Paragraph
        Dim oPara2 As Word.Paragraph

        'Start Word and open the document template.
        oWord = New Word.Application
        oWord.Visible = True

        ' Create A New Document
        oDoc = oWord.Documents.Add

        'Add a paragraph
        oPara1 = oDoc.Content.Paragraphs.Add
        oPara1.Range.Text = "Here's My First Bit Of Text"
        oPara1.Range.Font.Bold = True
        oPara1.Range.InsertParagraphAfter()

        ' Add another paragraph
        oPara2 = oDoc.Content.Paragraphs.Add
        oPara2.Range.Text = "Here's My First Bit Of Text"
        oPara2.Range.InsertParagraphAfter()
(remember to correctly close and destroy your objects though).

If you are doing something more complicated then do what Rick suggested as it will allow you to create a template file and you can do all the formatting there.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Hey Rick,
I have a few questions. First when I go to "Tools" -> "Mail Merge" -> "Main Document - Create" I only see "Form letter", "Mailing labels", "Envelopes", and "Catalog" but when i click on any of them I do get to choose "Active Window". And that is what I tried. After that I created a file that looked like this: 1234|5678% and tried to merge it using the directions you wrote. But after setting the Field delimiter to | and Record delimiter to % I get an error saying: "Word could not merge the main document with the data source because the data records were empty or no data records matched your query options.

Im not sure why that is.

Thank you again.
 
Make sure you have a header row and atleast 1 data row. You text file should look like:

Field 1 Title|Field 2 Title%
1234|5678%

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I'm generating a Word doc in VB.NET, but the problem I am having is that the instance of WINWORD.EXE is not going away once I've generated the word doc.

I'm doing the following:

Imports Word = Microsoft.Office.Interop.Word
Dim myReportWord As New Word.Application
Dim myDocument As Word.Document
''''My code for what's in the document goes here''''
myDocument.Close()
myWord.Application.Quit()
Document = Nothing
myWord = Nothing
 
First problem may be that you set Document = Nothing (it is declared as myDocument earlier on).

Second, this may help: thread796-1027496

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Everything works but I do have one question. After I merge the two documents the first inserted data field that goes into the word document from the txt gets returned to the next line. Why is that? Thank you
Example:

Gauge Num:
123
Work Num: 567
Date:7668
Name: bob
 
Is there a carrage return in the data file?

I fergot about this when I wrote those samples, your data file should actually look like this:

Field 1 Title|Field 2 Title%1234|5678%

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
After I try to merge the document I get the window that says "Choose the endcoding to use for loading this file": and it lists a bunch of encoding ways such as Unicode. I tried this through VB which worked before untill I got this window to pop up and I also tried this doing manually through MS word directly and it still gives me this. This window comes up every time I try to merge the two document. This did not appear before so I dont know why this is appearing now. This is my txt: PartNum|SerialNum|Desc|Purch|Work|Date|Purch|TestedBy%PTHA3001|11|0-100 PSI Gauge, (+-)1 FS|3456345|3456345|4/20/2005|3456345|gh%

all on one line.

Please help. Thank you very much!!!!
 
How are you writing the data file?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
This is my code for doing the merge:

Code:
Imports System.Windows
Imports Word
Module MSWORD
    Public Function wordWrite()
        'ChDir(Forms.Application.StartupPath())
        Dim InternalWordApp As New Word.ApplicationClass
        Dim InternalWordDoc As New Word.Document
        Dim InternalWordDocTemp As New Word.Document

        InternalWordApp.Visible = False
        InternalWordDoc = InternalWordApp.Documents.Add("C:\Documents and Settings\GVORTMAN\Desktop\WindowsApplication3\bin\label.doc")
        With InternalWordDoc.MailMerge
            .Destination = Word.WdMailMergeDestination.wdSendToPrinter
            .SuppressBlankLines = True
            .Execute(True)
        End With
        InternalWordDoc.Saved = True
        InternalWordDoc.Close()
        InternalWordDoc = Nothing
        InternalWordApp.Quit()
        InternalWordApp = Nothing
    End Function
End Module

This is my code for writing to the file...I have several global variables that I use to write to the txt:

Code:
infoStream = New IO.FileStream(fileName, IO.FileAccess.Write)
            infoWriter = New IO.StreamWriter(infoStream)
            infoWriter.WriteLine("PartNum|SerialNum|Desc|Purch|Work|Date|Purch|TestedBy*" + CStr(SaveData.PartNumber) + "|" + CStr(SaveData.SerialNum) + "|" + CStr(SaveData.Desc) + "|" + CStr(SaveData.purchOrder) + "|" + CStr(SaveData.WorkOrdNum) + "|" + CStr(SaveData.datenum) + "|" + CStr(SaveData.purchOrder) + "|" + CStr(SaveData.Init) + "*")
            infoWriter.Close()
            wordWrite()
 
hmm, it all looks good. Try reconnecting the word file to the data file. Not sure if that will fix it, but sometimes Word gets a bit hokey.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Ok, now it works. I have this new merged document print directly to the printer so when the user presses print a dialogue print box comes up asking what printer to choose and all that stuff. When pressing print the document prints fine but when pressing cancel it give me an error because of the .Execute(True)...I was looking for the API for this to see what .Execute(True) does but could not find it..if anyone can tell me where I can get the info about this that would be good. And once again thank you very much.
 
execute fires the word merge. Quick way arround it, put it in a try block, catch the COM error thrown ad parse the specifics.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top