Solved

VB and Word

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 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