Microsoft KB Archive/172847

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 11:30, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 172847

Article Last Modified on 2/9/2006


  • 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


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.


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

  1. Create a new Word document in Word 97, and format the document as follows:

       Dear NAME,
       John Smith
  2. Create a bookmark for CONTENT and NAME (the names of the bookmarks are CONTENT and ANAME, respectively). To create these bookmarks, follow these steps:
    1. Highlight the items in the document (for example, CONTENT).
    2. On the Insert menu, click Bookmark.
    3. In the Bookmark dialog box, type in the bookmark's name and then click Add.
    4. Repeat steps a through c for the NAME bookmark.
  3. Save the Word document as mytest.doc.

Visual FoxPro-specific items

  1. Create a table named "mydoc" with the following structure:

          Field name    Type       Size
          Content       General     4
          Name          Character   10
  2. 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.
  3. Close the table.
  4. Create a form that is named mail.scx in Visual FoxPro that only has a Command button.
  5. Place the following code in the Click method of the Command button:

          LOCAL oWordObj
          *   Open table
          IF !USED("mydoc")
            USE mydoc
            SELECT mydoc
          *   Open a Word Object
          oWordObj = CREATEOBJECT("Word.Application")
          *   Next open the original document in a second window
          *   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
          *   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
            *         Starts Word
            *         Creates Form in VFP
            *         Adds OLE object to form
            *         Sets the control source
                   frmWord.OBJWORDDOC.DoVerb(0) && Default Word doc is 'Edit'
          * 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
          *    Close Word
          *    Quit Word, release the object and clear the desktop
          Release oWordObj
  6. 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:

OLE IDispatch exception Code 0 from Microsoft Word. The requested member of the collection does not exist.

Keywords: kbhowto kbinterop kbautomation KB172847