Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB and Word

Posted on 1998-07-21
5
Medium Priority
?
221 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 150 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…
Suggested Courses

782 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