Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Word from VFP 2

Status
Not open for further replies.

ezpzjohn

Programmer
Jun 29, 2001
50
GB
Hi

I have spent most of the day checking postings to the forum on how to use Microsoft Word from VFP and have ended the day pretty confused.

What I want to do is to use a Word file with database fields on it saved as a template. I then want VFP to get Word to open a new document from the template, close the template, replace the fields in the document with values from a table then bring Word to the front so the user can save, print or do whatever with the file. Any help would be appreciated!

By the way, when should I use:

CreateObject('Word.Application')

and when should I use:

CreateObject('Word.Basic')

TIA

John B.
 
Hi

There is absolutely no better way to automate Word than to use the command window to process your code so that you can check to see if your code actually works. Start by opening the word object using the ubiquitous:

oWord = createobject("Word.Application")
oWord.visible = .t.

You should now see the Word object that you have just created.

Make sure you have the Word VBA help file "\Program Files\Microsoft Office\Office\1033\VBAWRD9.CHM" open. This will list all methods and procedures for Word that you can hook into. I have been informed on this forum that this file can be called a slight variation on this file, but it's still close!

To create a new file from a template enter this

Code:
oWord.documents.add("full path to your template")

And as if by magic, there, in your word window is your new document! You can see in the help file all the other parameters for the Add method.

So that's the first stage done. Part 2 is a little more involved, but here goes ... Create custom document properties in your template (I shall assume you know how to do this) and call them all relevant names that you can remember (and that match your table columns if possible!). Insert these as fields in your template where you wish them to be.

Once you have your newly created document open you can simply execute this marvelous piece of code:

Code:
oWord.ActiveDocument.CustomDocumentProperties("your field name").value = new_value_of_your_choice

and change the values of all the fields in your document accordingly! There is a small matter of updating the fields, but this can be done by using:

Code:
oWord.ActiveDocument.Fields.Update

If you have fields in the header or footer you will need to get back t ome with that code, because I can't be bothered to type it now!

Now you have a new document, with all its values in fields updated to your new values, it's time to show the document to your user and you have finished!

I hope this helps! Derren
[The only person in the world to like Word]
 
Hi Derren

Many thanks for your very full reply - I will try out your method and post a reply later.

John B.
 
Hi again Derren

Tried your suggestions with excellent results - many thanks! I struggled a bit as I don't have the VBA help file on my system (must find out my Office CD and reinstall) so a bit of trial and error was involved.

Many thanks again.

John B.
 
No problem! Don't forget to mark posts as heplful if you think they might be of interest to other people as well.

[fish] Derren
[The only person in the world to like Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top