HOWTO: Save an Embedded Word Document in Visual Basic

Last reviewed: September 29, 1997
Article ID: Q112440
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Word for Windows, version 6.0

SUMMARY

Word version 6.0 for Windows disables the ability to do a FileSaveAs while an object is being edited in an OLE container. While these methods may not be enabled, it is possible to work around this limitation in code. This article explains how.

MORE INFORMATION

Commands that are part of the workspace are the responsibility of the top container (the Visual Basic application). That is, the application is responsible for the organization of windows, file level operations, and how edits are ultimately saved. The top container must supply a single File menu that contains file level commands such as Open, Close, Save, and Print.

If the object is an opened object server application, the commands in its File menu are modified to show containership (Close & Return to <container doc>, Exit & Return to <container doc>).

A well-behaved OLE server will not allow workspace commands to be executed. This is why they are disabled. To work around the problem, edit the object in the server application -- without using in-place editing. In the server, you'll find that the workspace commands are enabled. Therefore edit the object in the server and use OLE Automation to control the server to execute the Workspace commands.

Step-by-Step Example

The following example uses an OLE2 control called OLE1, which contains an embedded Word version 6.0 document and a CommonDialog control called CMDialog1. To make the code generic, the OLE1 control is passed to the WordFileSave subroutine.

  1. Start a new project in Visual Basic, Form1 is created by default.

  2. Add a command button (Command1), MSOLE2.VBX (OLE1) control, and a CMDIALOG.VBX (CMDialog1) control to Form1.

  3. Add the following code to the Command1_Click event:

       Sub Command1_Click ()
          ' Pass the name of the Control to WordFileSave subroutine.
          WordFileSave OLE1
       End Sub
    
    

  4. Add the following code to the general declarations section of Form1:

       Sub WordFileSave (OLECtrl As Control)
          'Purpose: Example of how to save an embedded Word object from
          'Visual Basic as a Word Document.
    
          'Overview of technique:      '
          'Activate Object. Select its contents. Copy contents to clipboard.
          'Launch a hidden instance of Word. Create a new file.
          'Paste clipboard into document. Save document.
    
          Dim Word As Object  'Alias to Hidden instance of Word.
                              'Only if Word is not already running.
          Dim WB As Object    'alias to WordBasic object.
    
          OLECtrl.Action = 7  'Activate OLE control. This must be done in order
                              'to have the Word Basic alias act on the correct
                              'instance of Word.
          Set WB = CreateObject("Word.Basic") 'Set the object variable.
    
          WB.editselectall    'Select the contents of the embedded object.
          WB.EditCopy         'Copy the selection to the clipboard.
          OLECtrl.Action = 9  'Deactivate the OLE control. This must be
                              'done before the following set statements to
                              'reference the correct instances of Word.
    
          'Use the Common dialog control to display a SaveAs dialog.
          CMDialog1.Filter = "Word Document (*.Doc)|*.doc"  'Set the filter
          CMDialog1.DefaultExt = "*.doc"            'Set the default extension
          CMDialog1.FileName = OLECtrl.SourceDoc 'Set default filename
          CMDialog1.Action = 2                      'Display the dialog.
    
    
          Set WB = Nothing    'Free the WB object reference.
          Set Word = GetObject("", "Word.Document.6")  'Create a hidden inst.
          Set WB = Word.application.Wordbasic  'Set WB to the WordBasic object
                                               'of the new instance of Word.
    
          WB.filenew        'Create a New file in hidden instance of Word.
          WB.editpaste      'Paste contents of clipboard into new document.
          WB.filesaveas CMDialog1.Filename  'Save file as selected by user.
          WB.fileclose       'Close document.
    
          Set WB = Nothing   'Free WordBasic object
          Set Word = Nothing 'Free Word Document object, if Word wasn't
                             'running previously, Word will shut itself down
                             'from memory; otherwise, it is up to the user to
                             'shut Word down.
       End Sub
    
    

  5. Run the program. The program will ask you to input a name and then save the document to the name that you input.
Keywords          : IAPOLE PrgCtrlsCus vbwin GnrlVb kbprg kbfasttip
Technology        : kbole kbvba
Version           : WINDOWS:3.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.