VB and Word

I would like to open a word document and send some data from my VB application at run time. can anybody tell me how to do this? any sample code will be higly appreciated.
thanks in advance,
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

tomookConnect With a Mentor Commented:
To open Word, (assuming version 95 or 97), dd a reference in your project to Microsoft Word Object Library, then try

Dim oWord As Word.Application
Dim oDoc As Word.Document

Set oWord = New Word.Application

' Open an existing file this way

' Open a new document
Set oDoc = oWord.Documents.Add

oDoc.Content.InsertAfter "Mary had a little lamb," & vbCrLf & "Her fleece was white as snow"

oDoc.SaveAs "C:\Junk.Doc"

Set oWord = Nothing

vmanoAuthor Commented:
hi tomook,
it would be great if you can show me how i can customize the caption of the word document instead of "Microsoft Word - Document1". and also it seems to me that when i exit the application it is not unloading the Word application object.
thanks for your time and help,
To se a caption, try inserting

oDoc.Windows(1).Caption = "My Custom Caption"

once the document is open. Note that this only changes the caption on the first (default) window. If a user has multiple windows open on a document, you will have to iterate the whole Windows collection for that document.

As far as Word not exiting, this typically has one of two causes. Either a document is left open, or Word is visible and you have clicked on it. If you have looked at the Word application you are OLE automating, the Desktop has a reference to the Word.Application object. To force Word to quit, use


The object browser has the whole run down on the Word interface. It would be worth some of your time to just explore. Just as an example, we have not touched on StoryRanges, Styles, and SubDocuments. To do serious work with Word, you will need to master these, but they are for another question!
vmanoAuthor Commented:
thanks for your answer.here is another question. i want to show the word document using - oWord.Visible = True
When the user closes the document form Word itself, i am getting an error at the line - Set oWord = Nothing
how can i control the word document from my application?
And Is there a way to show the word document as Modal window?
if you want i can post all this as another question or increase the points here itself.
thanks inadvance,
oWord.Visible controls the visibility of the Word application itself, showing a document requires manipulating documents and windows. You might want to post the last bit as a new question. I am going to be a little short of time in the next few days and someone may be able to answer you before I can get to it.
All Courses

From novice to tech pro — start learning today.