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 strongm 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
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.
 
Hi,
I was wondering how I can write to MS Word from VB.NET. Previously I wrote an app. that writes to Excel. The program wrote soem text into specific cells within Excel because I had a template that needed to be filled up, but now I need to do the same thing in Word. So I need to write to a specific location in Word.
For now I have this:
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRange As Word.Range
Dim Lines
Dim strText As String

wdApp = New Word.Application
wdApp.Visible = false
Set wdDoc = wdApp.Documents.Open("")
Set wdRange = wdDoc.Range
this is within a module. The wdApp = new Word.Application gives me an error stating that it need to be declared, don't know why. Also, I have no idea how to write to a specific location in word.

Thank you.
 
What version of Word are you writing to? I recall having a similar issue awhile back and it was due to a bug in Word 97. When we upgraded the machine to Office 2000, the problem disappeared.
 
What version of Word are you writing to? I seem to recall having a similar issue awhile back and it was due to a bug in Word 97. When we upgraded the machine to Office 2000, the problem disappeared.
 
Look into making a Word merge document. They allow you to create a Word document with fields inserted that get populated from a data source. You can then build a text file in your code and use the Word COM objects (as you are above) to execute the merge.

-Rick

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

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
I have a predesigned word document that I need to fill particular parts. Such as:

Part Number: "then i enter the part number from vb.net"

And also I have no idea how to do a word merge document. All I need to do is fill in parts of a word document.

Thank you again

 
Open word, make your document, look through the menus for something that looks like "merge" there's a handy little wizard. You may want to create a coma delimited text file first to store the values. Then when you select that file in the "merge" wizard, you'll be able to add all of the fields to the document.

I'm at home, so I can't look up the menu names, or the code to run the merge, give this post a bump in the morning and I'll post some stuff from work.

-Rick

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

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
Hi, i looked up merge and there is no wizard. Not sure though if this is what i need. Im not merging any documents. Also, for some reason everything after the declerations gives me an error.

oWord = New Word.Application states that oWord needs to be declared. I imported all the resorces and Word itself.

Thank you
 
I've used Word Bookmarks in the past. If the positions in the Word Document are fixed, then Insert Bookmarks at each point at which you want to insert data. Save the document as a Word Template. Then with VB you can create a new Word document based on this template. Using the Word instance, I can't remember whether you would use the Application or Document object, goto each Bookmark and .TypeText (I think) the data that you want written at that point.

Hope this helps.
 
earthandfire thats exactly what i need to do. Yea but the syntax gets me. not sure how.
thanx
 
It sounds like the reference that you have added is incorrect (or for a different version of Word) as the example I gave above works correctly. It would be easy to adapt my code to insert into the correct place (it was just designed to be a simple example).

What version of Word are you using?

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

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I'm afraid I've not done this in .NET, only through Access. So hopfully, now that we know exactly what you need, someone will be able to provide a .NET example. If not, I'll dig out my VBA for you over the weekend and post that as an example that you can work with. Obviously it will need to be .NET-ified.
 
Ahh well, here's what we do:

Step 1) Build a character delimited text file with the values you want to insert. for example:
Field1Title|Field2Title~
Value1|Value2~
Another Value1|Another Value2~

Step 2) Open Word, goto "Tools" -> "Mail Merge" -> "Main Document - Create" -> "Active Window"

Step 3) goto "Data Source - Get Data" -> "Open data source" -> browse to and select the text file you made. Enter the column and row delimiters (| and ~ in the sample above) Hit "Edit main document"

Step 4) On your toolbar there is now an "Insert Merge Field" button, click on the arror next to it and you'll see a list of field names. You can stick these anywheres on your document.

Step 5) If you manually created the text file in step 1, go back and create it dynamicly from code.

Step 6) Add a reference to the Word 9 dll, not the Office 9 dll

Step 7) Coding an automated mail merge:
Code:
Dim m_wrdApp As New Word.ApplicationClass
Dim m_wrdDoc As New Word.Document

InternalWordApp.Visible = False
InternalWordDoc = InternalWordApp.Documents.Add(strFile)
With InternalWordDoc.MailMerge
  .Destination = Word.WdMailMergeDestination.wdSendToNewDocument
  .SuppressBlankLines = True
  .Execute(False)
End With
InternalWordApp.Visible = True

That should get you going, there's a bunch of other refinements you can do. But that will print out a document for each row of values in your text file.

-Rick

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

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
Rick, I'm interested to know why you seem to prefer Merge over Bookmarks. They impression I get from saphiroth is that it is a single document to be printed which I think Bookmarks would handle more efficently. At least that was the case when I did this through Access.
 
I prefer Merge over bookmarks because that's what I'm familiar with. Truth be told, if you're doing a single document, they'll both come out with the same end product. Bookmarks may be a hair faster, I don't know. But if you want to make more then one document, I think Merge would quickly become the prefered solution.

-Rick

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

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
You peole are all great and I should buy all of you ice cream. Thanx for the help. Also, Rick, I dont know the values that will be entered. The values will be entered from VB. The user will input some stuff that I need to use to fill in some blanks in my word template that i made, kind of like a certificate form.
 
For some reason the InternalWordApp has an error that says it needs to be declared. Also the With statement has an error stating that it can not appear outside the method body. Wonder if anyone knows why.

Thank you
 
Whoops, I'm assuming you're refering to my code. This is what I get for copying/triming code from the library here. InternalWordDoc is a property that points to the wrdDoc object. You can just replace InternalWordDoc with wrdDoc.

Also, that block of code needs to be put into a sub or function.

-Rick

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

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

Part and Inventory Search

Sponsor

Back
Top