Microsoft KB Archive/172847: Difference between revisions
(importing KB archive) |
m (Text replacement - """ to """) |
||
Line 49: | Line 49: | ||
== SUMMARY == | == SUMMARY == | ||
This article illustrates how to | This article illustrates how to "Select All" and "Cut and Paste" an embedded Word Document from a general field into another Word document using code and OLE automation. | ||
</div> | </div> | ||
Line 82: | Line 82: | ||
<ol> | <ol> | ||
<li><p>Create a table named | <li><p>Create a table named "mydoc" with the following structure:</p> | ||
<pre class="fixed_text"> Field name Type Size | <pre class="fixed_text"> Field name Type Size | ||
----------------------------- | ----------------------------- | ||
Line 93: | Line 93: | ||
<pre class="codesample"> LOCAL oWordObj | <pre class="codesample"> LOCAL oWordObj | ||
* Open table | * Open table | ||
IF !USED( | IF !USED("mydoc") | ||
USE mydoc | USE mydoc | ||
ELSE | ELSE | ||
Line 100: | Line 100: | ||
* Open a Word Object | * Open a Word Object | ||
oWordObj = CREATEOBJECT( | oWordObj = CREATEOBJECT("Word.Application") | ||
* Next open the original document in a second window | * Next open the original document in a second window | ||
oWordObj.Documents.Open( | oWordObj.Documents.Open("C:\VFP50\mytest.doc") | ||
* Retrieve the number of bookmarks defined in DOC | * Retrieve the number of bookmarks defined in DOC | ||
Line 123: | Line 123: | ||
DO CASE | DO CASE | ||
CASE pcMarkName = | CASE pcMarkName = "ANAME" | ||
.text = ALLTRIM(Name) | .text = ALLTRIM(Name) | ||
CASE pcMarkName = | CASE pcMarkName = "CONTENT" && goes to bookmark | ||
oWordObj.WordBasic.ww7_editgoto( | oWordObj.WordBasic.ww7_editgoto("CONTENT") | ||
* Starts Word | * Starts Word | ||
oWord=GETOBJECT(, | oWord=GETOBJECT(,"WORD.basic") | ||
* Creates Form in VFP | * Creates Form in VFP | ||
frmWord=CREATEOBJECT( | frmWord=CREATEOBJECT("FORM") | ||
* Adds OLE object to form | * Adds OLE object to form | ||
frmWord.ADDOBJECT( | frmWord.ADDOBJECT("OBJWORDDOC","OLEBOUNDCONTROL") | ||
* Sets the control source | * Sets the control source | ||
frmWord.OBJWORDDOC.CONTROLSOURCE= | frmWord.OBJWORDDOC.CONTROLSOURCE="mydoc.content" | ||
* frmWord.show | * frmWord.show | ||
frmWord.OBJWORDDOC.DoVerb(0) && Default Word doc is 'Edit' | frmWord.OBJWORDDOC.DoVerb(0) && Default Word doc is 'Edit' | ||
Line 152: | Line 152: | ||
* Save and Print the document | * Save and Print the document | ||
oWordObj.WordBasic.FilePrint(0) && Turn background printing off | oWordObj.WordBasic.FilePrint(0) && Turn background printing off | ||
WAIT WINDOW | WAIT WINDOW "Printing in Progress" TIMEOUT 3 | ||
oWordObj.WordBasic.FileSaveAs( | oWordObj.WordBasic.FileSaveAs("c:\VFP50\DEMO1.DOC") | ||
* Close Word | * Close Word |
Revision as of 11:06, 21 July 2020
Article ID: 172847
Article Last Modified on 2/9/2006
APPLIES TO
- Microsoft Visual FoxPro 5.0 Standard Edition
- Microsoft Visual FoxPro 5.0a
- Microsoft Visual FoxPro 6.0 Professional Edition
- Microsoft Visual FoxPro 7.0 Professional Edition
- Microsoft Visual FoxPro 8.0 Professional Edition
- Microsoft Visual FoxPro 9.0 Professional Edition
This article was previously published under Q172847
SUMMARY
This article illustrates how to "Select All" and "Cut and Paste" an embedded Word Document from a general field into another Word document using code and OLE automation.
MORE INFORMATION
The following example consists of two sections. The first section is to create a mail merge Word document with bookmarks.
Note This example only works in Microsoft Word 97.
Create a Word 97 merge document
Create a new Word document in Word 97, and format the document as follows:
Dear NAME, CONTENT Sincerely, John Smith
- Create a bookmark for CONTENT and NAME (the names of the bookmarks are CONTENT and ANAME, respectively). To create these bookmarks, follow these steps:
- Highlight the items in the document (for example, CONTENT).
- On the Insert menu, click Bookmark.
- In the Bookmark dialog box, type in the bookmark's name and then click Add.
- Repeat steps a through c for the NAME bookmark.
- Save the Word document as mytest.doc.
Visual FoxPro-specific items
Create a table named "mydoc" with the following structure:
Field name Type Size ----------------------------- Content General 4 Name Character 10
- There should be only one record in this table. In the general field, paste in any existing Word document. In the Name field, place a ten-character expression.
- Close the table.
- Create a form that is named mail.scx in Visual FoxPro that only has a Command button.
Place the following code in the Click method of the Command button:
LOCAL oWordObj * Open table IF !USED("mydoc") USE mydoc ELSE SELECT mydoc ENDIF * Open a Word Object oWordObj = CREATEOBJECT("Word.Application") * Next open the original document in a second window oWordObj.Documents.Open("C:\VFP50\mytest.doc") * Retrieve the number of bookmarks defined in DOC pnCountMarks = oWordObj.ActiveDocument.Bookmarks.Count * Store bookmarks in an array DIMENSION aMark[pnCountMarks] FOR pnCount = 1 TO pnCountMarks aMark[pnCount] = oWordObj.ActiveDocument.Bookmarks(pnCount).Name ENDFOR * Loop through all the bookmarks in the document FOR pnCount = 1 TO pnCountMarks pcMarkName = UPPER(aMark[pnCount]) * Move to the next bookmark WITH oWordObj.ActiveDocument.Bookmarks(pcMarkName).Range DO CASE CASE pcMarkName = "ANAME" .text = ALLTRIM(Name) CASE pcMarkName = "CONTENT" && goes to bookmark oWordObj.WordBasic.ww7_editgoto("CONTENT") * Starts Word oWord=GETOBJECT(,"WORD.basic") * Creates Form in VFP frmWord=CREATEOBJECT("FORM") * Adds OLE object to form frmWord.ADDOBJECT("OBJWORDDOC","OLEBOUNDCONTROL") * Sets the control source frmWord.OBJWORDDOC.CONTROLSOURCE="mydoc.content" * frmWord.show frmWord.OBJWORDDOC.DoVerb(0) && Default Word doc is 'Edit' oWord.EditSelectAll oWord.EditCopy oWordObj.WordBasic.editpaste ENDCASE ENDWITH ENDFOR * Release the Word.Basic object that edits the General field * contents, and close the Form that was opened to access the VFP * table with that General field. Release oWord Release frmWord * Save and Print the document oWordObj.WordBasic.FilePrint(0) && Turn background printing off WAIT WINDOW "Printing in Progress" TIMEOUT 3 oWordObj.WordBasic.FileSaveAs("c:\VFP50\DEMO1.DOC") * Close Word oWordObj.WordBasic.DocClose * Quit Word, release the object and clear the desktop oWOrdObj.Quit Release oWordObj Thisform.release
- Save, and run the form. Click on the command button, and Word prints out the document with the Name and the Content of the mail merge.
Note With the GetObject() command, Word 97 looks for a document with the desired name. If you want to refer to an already launched instance containing your word document, you should leave the first parameter empty instead of using an empty string, otherwise you can launch a new (invisible) instance of Word containing a NEW document.
Note On random occasions this solution can cause an unexplained OLE error at the following line of code:
WITH oWordObj.ActiveDocument.Bookmarks(pcMarkName).Range
The error message follows:
Keywords: kbhowto kbinterop kbautomation KB172847