No warning when you overwrite PowerPoint 2002 files in Visual Basic for Applications (278544)



The information in this article applies to:

  • Microsoft PowerPoint 2002

This article was previously published under Q278544

SYMPTOMS

When you save a Microsoft PowerPoint 2002 file by using the Save or SaveAs methods in Microsoft Visual Basic for Applications (VBA), you do not receive any warning if the file overwrites an existing file with the same name.

This behavior differs from other Microsoft Office programs that warn you in this case.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. NOTE: The following macro examples work only in PowerPoint. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, click the following article number to view the article in the Microsoft Knowledge Base: To work around this problem, use either of the following methods:
  • Save the file by using only the PowerPoint object model.
  • Save the file by using the FileSystemObject.

Save the File by Using Only the PowerPoint Object Model

The following sample macro checks whether or not the file name you are about to save exists, and prompts you with a warning if it does. To create the macro, follow these steps:
  1. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  2. On the Insert menu, click Module.
  3. Type the following code in the module:
    Function VerifyFilename(strFilename As String) As Boolean
    
       On Error Resume Next
    
       Dim strPath         As String
       Dim strFile         As String
       Dim i               As Long
       Dim strNextFile     As String
       '
       ' Get the path from the file name.
       '
       For i = Len(strFilename) To 1 Step -1
          If Mid(strFilename, i, 1) = "\" Then
             strPath = Left(strFilename, i)
             strFile = Right(strFilename, Len(strFilename) - i)
             Exit For
          End If
       Next i
       '
       ' Set the flag to True.
       '
       VerifyFilename = True
       '
       ' Start listing all the files in the SaveAs Path.
       '
       strNextFile = Dir(strPath)
       '
       ' Loop though all files in that folder.
       '
       Do Until strNextFile = ""
          If LCase(strNextFile) = LCase(strFile) Then
       '
       ' If the file is found, set the flag to False and exit.
       '
             VerifyFilename = False
             Exit Function
          End If
    
          strNextFile = Dir
    
       Loop
    End Function
    
    
    Sub SaveFile()
       Dim bolVerify As Boolean
       Dim Msg As String
       Dim Style As Long
       Dim Title As String
       Dim Response As String
       Dim MyString As String
    
       With ActivePresentation
          bolVerify = VerifyFilename(.FullName)
    
          If bolVerify = False Then
       '
       ' Define success message.
       '
             Msg = "You're about to overwrite " & .FullName & _
                   "Do you want to continue ?"
       '
       ' Define buttons for message box.
       '
             Style = vbYesNo + vbCritical + vbDefaultButton2
       '
       ' Define the title for the message box.
       '
             Title = "FILE OVERWRITE WARNING!!!"   
             Response = MsgBox(Msg, Style, Title)
       '
       ' User chose No and is sent to the end of the Sub without saving.
       '
             If Response = vbYes Then
                ActivePresentation.Save
                MsgBox "File Saved!"
             End If
          End If
       End With
    End Sub
    					

Save the File by Using the FileSystemObject

The following sample macro uses the Microsoft Scripting Runtime library to verify that the file name is not in use in the folder that you choose. To create the macro, follow these steps:
  1. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  2. On the Tools menu, click References.
  3. In the list of references, click to select the Microsoft Scripting Runtime check box. Click OK.
  4. On the Insert menu, click Module.
  5. Type the following code in the module:
    Function SaveAsFile(strFullPath) As Boolean
       Dim fsoFile As FileSystemObject
       Dim msgResults As VbMsgBoxResult
       '
       ' Set fsoFile to the Microsoft Scripting Runtime FileSystemObject.
       '
       Set fsoFile = CreateObject("scripting.filesystemobject")
       '
       ' Initialize the return value of the function to True.
       '
       SaveAsFile = True
    
       With ActivePresentation
       '
       ' Check to see if the file name exists at the file destination folder.
       ' If it does, then ask the user if they want to overwrite the 
       ' existing file or not.
       '
          If fsoFile.FileExists(strFullPath) Then
             msgResults = MsgBox("Warning! File: " & strFullPath & " exists!" _
                   & vbNewLine & "Overwrite?", vbYesNo, "File Exists")
       '
       ' If the answer is yes, overwrite the file.
       '
             If msgResults = vbYes Then
                .SaveAs strFullPath
             Else
                SaveAsFile = False
             End If
       '
       ' If the file does not exist at the destination folder, save it.
       '
          Else
             .SaveAs strFullPath
          End If
       End With
       '
       ' Clear the fsoFile object.
       '
       Set fsoFile = Nothing
    End Function
    
    Sub SaveFile()
       Dim strPresentationPath As String
       Dim bSaved As Boolean
    
       '
       ' Prompt user for the location to save the file.
       '
       strPresentationPath = InputBox("Enter path and name to save file as:", _
             "Save as new File", ActivePresentation.FullName)
       '
       ' Call the SaveFileAs function with the file location and name.
       '
       bSaved = SaveAsFile(strPresentationPath)
       '
       ' If it saved, tell the user. If it didn't, tell the user that too.
       '
       If bSaved Then
          MsgBox "Saved Presentation: " & strPresentationPath
       Else
          MsgBox "Presentation, " & strPresentationPath & ", was not saved."
       End If
    End Sub
    					

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbbug kbnofix KB278544