HOWTO: Save an Embedded Word Document in Visual Basic

ID: Q112440


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 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.


Additional query words:

Keywords : kbole kbprg kbCtrl kbVBp300 kbDSupport kbvbp200
Version : WINDOWS:2.0,3.0,6.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: October 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.