Solved

VB-to-Word data sending.

Posted on 1998-11-24
6
215 Views
Last Modified: 2010-05-03
I need to learn how to send data from VB databases to a Word document.

The exact way I want to make it is: I navigate a VB database, I find the exact data (i.e. a person and the associated address and birth date), and I press a CommandButton "Send".

Then I want to open a pre-existing Word document (I know the exact document and its path) to insert the data. Some data will be in the middle of a phrase, and has to be at an exact place, as it will print to a pre-printed form.

Have I to put in the Word doc "marks" at the exact places to substitute them with the data? How is it made overstriking? (If I insert, the rest of the document will be displaced, I think, and I don't want it to move).

I need a little code sample, not hints. It would be enough if you tell me how to connect to Word ("Word.Application", Word.Document", both?), how to send a text from a variable or a field, how to insert this text at a specific location without displacing the rest, and how to print the document directly from VB without having to go to Word's File:Print.

A final little collateral question: If I wanted to reformat the whole document after inserting data (i.e. a personalized mailing with the name in the middle of the phrase) to re-justify it, how it would be made?

I own VB 5 Enterprise and Office 97 Pro, and I know VB for Applications is the common gateway between them.

Thank you in advance, and greetings from Spain, Europe.
0
Comment
Question by:BETTY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 1

Author Comment

by:BETTY
ID: 1446493
Edited text of question
0
 
LVL 1

Expert Comment

by:mithomas
ID: 1446494
Since you want actual code, and I don't have time to develop some, I'll just mark this a comment.  Hopefully you'll accept hints for free.

One of the best ways to learn how to interact with word is to use its macro recorder features.  Try doing the things in Word that you want to automate, with the macro recorder on.  The recorder will write the commands to a word document.  These are the commands that you will send to word from your VB app.

To attach VB to word, you basically do a CreateObject() on the ProgId for word, storing the returned reference in an object variable.  You then make the calls to word:

'This code is fake.  It's been
'a long time since I did this!
Dim Word as Object
Set Word = CreateObject("Word7") 'This is not the correct progid
Word.OpenDocument "MyTemplate.doc"
Word.Document.InsertText "Bookmark1", "TheText"
Word.Document.Print
Set Word = Nothing

We used bookmarks and word templates to create "forms" to fill in.  You can use the bookmarks to indicate places where text must be filled in.  Name the bookmarks appropriately (such as "Name", or "Country"), and your code will make a lot of sense.

You DON'T need VBA to do this.  You DO need to read the help on CreateObject, and the Word object model (available from Word).

As an alternative to using CreateObject and just an Object variable thus requiring you to guess at how to interact with Word -- perhaps by now, MS has provided a type library so that you can add a reference to Word in your VB project and see all of the methods as you're writing your code.

Good luck...
0
 
LVL 3

Accepted Solution

by:
jjmartin earned 100 total points
ID: 1446495
'This example open a new word document and write to the file, you must be using Word 95 or 97

Private Sub Command1_Click()
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

End Sub


You must select the word object library within the project references otherwise your project will not recognize the Word declarations.  This is just a quick and simple example, the hard part comes when you want to format the text.  If you go into word and look under help, there is a help file specificaly for VB to word.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:BETTY
ID: 1446496
I would want to give jjmartin 50 additional points from my account, as his/her hint is nearly as helpful as the accepted answer from the other expert.

So, if you can do it directly, you have my authorization. If not, please tell me how can I do it.
0
 
LVL 1

Author Comment

by:BETTY
ID: 1446497
Sorry, I made a mistake when I wrote the last comment on November 25, 06:46 PST. Where I wrote "give jjmartin 50 additional points", I meant "give mithomas ...".

The reason is he/she made me a comment that helped me nearly the same that the accepted answer from jjmartin. Then, the A grade (100 points) goes to jjmartin, and I want to give a "gift" of 50 points to mithomas for the comment.

Thanks, and sorry again for the mistake.
0
 
LVL 1

Expert Comment

by:mithomas
ID: 1446498
Thanks, Betty, even if I don't get the points.  I appreciate the thought!

 :-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

726 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