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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fill MS Word fields

Status
Not open for further replies.

k2a

Programmer
Jun 26, 2012
133
DE
Hi,

I have a MS Word document with some MergeFields on it and want to fill these fields using vfp.
Untill now i was only able to open the MS Word document but could not figure out how to access the MergeFields.

Can any one help me out here with some codes on how to to access those fields?

Thanks in advance
 
Thanks Olaf for your quick response.

In the meantime i found that i mistakenly used MergeFields instead of Bookmarkes. After changing that i can now populate the bookmarkes fields into my Word document.

However, now i have some problems using a .dotx template with bookmark fields to create a new Word document where the data should go and save this document.

Could you give me same hints how to create a new word document from the template and save the copy?

Regards
Klaus
 
I recorded a macro on your behalf and it reads:
Documents.Open FileName:="SomeTemplate.dotx"
ActiveDocument.SaveAs2 FileName:="FinalWordDoc.docx", FileFormat:= wdFormatXMLDocument

So, you Open the template and use SaveAs for saving.

Code:
oWord.Documents.Open("yourtemplate.dotx")
...
oWord.ActiveDocument.SaveAs2("yourdoc.docx",16)

Parameters are optional, instead of explicitly saving as DOCX with wdFormatXMLDocument=12 you may use wdFormatDocumentDefault, which is 16, to save in the default doc format. Which is what I used here.
You know how you can get at all word constant definitions via object browser?

Bye, Olaf.
 
The hard part is, if wdFormatDocumentDefault is DOC for the word version used, the file still gets a DOCX file extension by the code as is. Not specifying a file extension might help, but I fear word VBA will then save without file extension, as given by the file name parameter. Anyway, if you're fine with docx and automate your computer only, then you may use it as is or with 12.

Caution, Word constants may not be downward compatible, 12 or 16 or any other constant value may mean something else in a previous word versions.

Bye, Olaf.
 
Thank you Olaf for your great help, now it works fine for me.

>>>>> You know how you can get at all word constant definitions via object browser?
No, i dont know, please tell me.

BTW, do you know how stop a process which entered in an enless loop, other then killing VFP via the Task Manager?

Regards
Klaus

 
First of all, Macro recording in Word is hidden in a Ribbon you wouldn't expect it: In View at the rightmost toolbar button. Click on record macro, give it some name, you won't change normal.dot with it in the normal case, unless you really want that macro code permanent. Do anything you want to do programmatically. After you're done, go back to the macros menu and stop recording. Now View Macros.

On the second question: If you're stuck there are not much exists. ESC, if not set off, but otherwise I also need the task manager from time to time.

If doing something new first single stepping through it in the debugger gives you all control, but endless loops may only reveal themselves after hundreds or thousands of iterations. Restricting what to do in loops helps a bit. SKIP -1 or GO should not be needed, unless you really know you still arrive at the end. The same applies to counter variable of FOR loops. I sometimes USE a table twice or SELECT some data into a loop cursor to really have a separate record pointer instead of doing record pointer gymnastics. You can look really advanced with something like that, but more likely look stupid, if it doesn't work out.

Bye, Olaf.
 
do you know how stop a process which entered in an enless loop, other then killing VFP via the Task Manager?

I suggest you start a new thread for that question, as it's not related to Word or Mailmerge. That way, it is more likely to be seen by someone who can answer it, and it would also be helpful for anyone searching for a similar problem.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Oops, and I explained the wrong thing.

Constant with object browser:
Open Tools->Object Browser
Also open up a new empty PRG edit window

In Object Browser you browse for Office by using the leftmost toolbar button. In the open dialog switch to the COM Libraries tab. Scroll down, in this case look out for "Microsoft.Office.Interop.Word...".
Expand the root node "Word" appearing and don't expand further. Now Drag the "Constants node" to the PRG file. This step is the well known saviour of your precious time, as you don't need to google constant value definitions, nor search the VBA help or get at it with a MsgBox, you get all constants in a #define header format at once, can either jsut pick out some of them or save them to a .h file and INCLUDE that in your PRG or classes.

In the general case the name of the COM library is not straight forward, you have to look around a bit, unfortunately that doesn't follow the simple name pattern of X.Application.
On the positive side you can get constants for many COM libraries, not only Office.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top