Article ID: 175173
Article Last Modified on 2/12/2007
169565 Type Mismatch Using OLE Automation to Word 97
Field Name Type Width ---------- --------- ----- FIRST Character 15 LAST Character 15 ADDRESS Character 25 CITY Character 15 STATE Character 2 ZIPCODE Character 5 GEN General 4After building the table and adding some data to each character field, make sure that each general field has been populated by double-clicking on the general field to open it and selecting the EDIT - INSERT OBJECT menu options. With the "Create New" button selected, select "Microsoft Word Document" in the "Object Type" list box and type some data into Word. Close Word to save the data into the general field.
************* Beginning of code. ****************
PUBLIC oWord
oWord = CREATEOBJECT("Word.Basic")
WITH oWord
.AppShow && Makes Word Visible
.FileNewDefault && Opens up blank Word document
* The following Insert commands place the FoxPro fields into
* the Word document. You can change these commands to place
* any text in the document.
* Ensure that the table is already open. If only a portion
* of records are to be printed, change the EOF() to a certain
* number so that all of the records do not get processed.
* Example: DO WHILE RECNO() < 10
GO TOP
DO WHILE NOT EOF()
.Insert("Record: "+ALLTRIM(STR(RECNO())))
&& The above line shows the record number.
.InsertPara
.InsertPara
.Insert(ALLTRIM(first)+" "+last) && Field in the table.
.InsertPara
.Insert(address)
.InsertPara
.Insert(ALLTRIM(city)+", "+state+" "+zipcode)
.InsertPara
.InsertPara
.Insert("Contents of the General Field:")
.InsertPara
.InsertPara
KEYBOARD "{ctrl+c} {ctrl+w}" && Copies and closes the general
&& field to the clipboard.
MODIFY GENERAL gen && This has to be after the
&& KEYBOARD command.
.EditPaste
.WordLeft(1,1)
.EditObject("0")
.EditSelectAll
.Editcopy
.FileClose
.EditClear
.EditPaste
.InsertPara
.InsertPara
.InsertPara
SKIP && Move the record pointer to
&& the next record.
ENDDO
.FileSaveAs("c:\mydoc.doc") && Saves the Word document as
&& Mydoc.doc.
.FilePrint
.AppClose
ENDWITH
**************** End of code. ****************
If you are using Word 97, Visual FoxPro 5.0 Service Pack 2 and Visual
FoxPro 6.0 are the only versions that run this code correctly. Place the
following code into a program (.prg) file and run it.
**************** Beginning of code. **********
PUBLIC oWord
oWord = CREATEOBJECT("Word.Application")
WITH oWord
.visible=.t. && Makes Word Visible
.documents.add && Opens up blank Word document
* The following Insert commands place the FoxPro fields into
* the Word document. You can change these commands to place
* any text in the document.
* Ensure that the table is already open. If only a portion
* of records are to be printed, change the EOF() to a certain
* number so that all of the records do not get processed.
* Example: DO WHILE RECNO() < 10
GO TOP
DO WHILE NOT EOF()
.Selection.TypeText("Record: "+ALLTRIM(STR(RECNO())))
&& The above line shows the record number.
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeText(ALLTRIM(first)+" "+last)&& Field in the table.
.Selection.TypeParagraph
.Selection.TypeText(address)
.Selection.TypeParagraph
.Selection.TypeText(ALLTRIM(city)+", "+state+" "+zipcode)
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeText("Contents of the General Field:")
.Selection.TypeParagraph
.Selection.TypeParagraph
KEYBOARD "{ctrl+c} {ctrl+w}" && Copies and closes the general
&& field to the clipboard.
MODIFY GENERAL gen && This has to be after the
&& KEYBOARD command.
.Selection.Paste
.Selection.MoveLeft
x = .ActiveDocument.Shapes.Count
.ActiveDocument.Shapes(x).OLEFormat.Edit
.Selection.Wholestory
.Selection.Copy
.ActiveDocument.Close
.Selection.Delete
.Selection.EndKey(6)
.Selection.Paste
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
SKIP && Move the record pointer to
ENDDO
.ActiveDocument.SaveAs("Mydoc.doc") && Saves the Word document as
&& Mydoc.doc.
.Application.PrintOut
ENDWITH
.Quit
**************** End of code ****************
Additional query words: OLE Word
Keywords: kbautomation kbcode kbdesigner kbhowto kbinterop KB175173