Solved

VB and Word

Posted on 1998-07-21
5
214 Views
Last Modified: 2010-05-03
Hi,
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,
vmano
0
Comment
Question by:vmano
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
tomook earned 50 total points
ID: 1466367
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
'oWord.Documents.Open(...)

' 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"
oDoc.Close

Set oWord = Nothing

0
 
LVL 3

Author Comment

by:vmano
ID: 1466368
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,
vmano
0
 
LVL 4

Expert Comment

by:tomook
ID: 1466369
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

oWord.Quit

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!
0
 
LVL 3

Author Comment

by:vmano
ID: 1466370
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,
vmano
0
 
LVL 4

Expert Comment

by:tomook
ID: 1466371
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal reports - Formula Field code need assistance with code 17 52
VBA Shell can't Find Word document 11 76
clicking a shape in a frame array vb6 3 40
Problem to With line 4 41
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now