WD: FileSaveAs Macro Causes Error Message in Word Document Objec

ID: Q96264

The information in this article applies to:

SYMPTOMS

In Word versions 2.x through 6.x, if you execute the FileSaveAs command from a WordBasic macro while editing a Word Document Object, the following error message occurs, regardless of the file name specified:

   This is not a valid filename.()

Word for Windows version 7.x does not issue an error message.

For example, the following macro command causes this error message:

   FileSaveAs .name="test.doc"

WORKAROUND

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Use the WindowName$() function to determine if the active document is an embedded Word document object. If the active document window is not an embedded Word document object, the file is saved. Otherwise, the macro uses the SendKeys command to use the Save Copy As command(On the File menu) to save editing changes made to the embedded document object to the original document.

   Sub MAIN
      a$ = WindowName$()
      ' If the active document is the embedded document.
      If InStr(a$, "Document in") <> 0 Then
         ' The following sequence of commands are sent using SendKeys:
         '
         ' File
         ' Save Copy As
         ' Name of file to save to
         ' OK(Enter or Return)
         ' (y)es-overwrite the file
         '
         SendKeys "%f" : SendKeys "a" : SendKeys "C:\test.doc" : \ 
         SendKeys "{ENTER}" : SendKeys "y"
      Else
         ' Otherwise, save the document.
         FileSaveAs .Name = "test.doc"
      End If
   End Sub

Note: Use SendKeys only when there is no alternative, and then use it with caution. You should test even the simplest use of SendKeys under a variety of conditions to avoid unpredictable results, data loss, or both. When stepping through a macro (using the Step or Step Subs button on the Macro toolbar) that contains a SendKeys instruction, note that the macro will not perform as expected unless the SendKeys instruction and the instruction that activates the target application or dialog box are on the same line, separated by a colon.

MORE INFORMATION

If you execute the FileSaveAs command by choosing Save As from the File menu, Word saves the object as a Word document. You cannot perform the same operation using a WordBasic macro.

By contrast, the FileSave command behaves the same regardless of whether you execute the command from a macro or by choosing Update from the File menu (the Save command changes to Update while editing a Word Document Object).

Additional query words: ole embed

Keywords          : kbmacro kbole wordnt word6 winword2 word7 word95 macword6 word 
Version           : WINDOWS:2.0,2.0a,2.0a-CD,2.0b,2.0c,6.0,6.0a,6.0.1:MACINTOSH:6.0,6.01,6.01a
Platform          : MACINTOSH WINDOWS
Issue type        : kbprb

Last Reviewed: February 12, 1999