WD: FileSaveAs Macro Causes Error Message in Word Document Objec

Last reviewed: March 12, 1998
Article ID: Q96264
The information in this article applies to:
  • Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, version 7.0
  • Word for the Macintosh, versions 6.0, 6.0.1

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 : winword2 word6 word7 word95 wordnt macword6 word kbmacro kbole
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
Solution Type : kbworkaround


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: March 12, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.