Solved

VB and Word

Posted on 1998-07-21
5
213 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

757 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

19 Experts available now in Live!

Get 1:1 Help Now