You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

"SaveAs" locks files, and won't create copy   Robert Potter Aug 09, 2007
Re: "SaveAs" locks files, and won't create...   Jeffrey Henry Jun 16, 2008
Re: "SaveAs" locks files, and won't create...   Robert Potter Jun 16, 2008
Re: "SaveAs" locks files, and won't create...   Matt leaver Nov 27, 2008
Re: "SaveAs" locks files, and won't create...   Solana Foo May 11, 2010
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject "SaveAs" locks files, and won't create copy 
Author Robert Potter 
Date Aug 09, 2007 
Message I have a VBA program that uses "Application.SaveAs Newfilename, true", which once I have run it it will not allow me to save the original MXD but also fails to save the copy to the new file name. Well that is not exactly correct it creates a tiny (16kb) version of nothing of the new file, and throws errors (Failed to save document) whilst doing it. once the errors have been dealt with it maintains a lock on these files ie. I am unable to delete them!.
Now for the really odd bit, it works on some machiines perfectly, but on others, nothing.
I thought it maybe the FixPack version, but alas it does not seem to be that either. It works fine on build 1324, but not on 1350.
Oh please help me! This is driving me mad!
 
 
NewFileName = UserForm1.TextBox4.Text & "MXDs\" & RampImage
Application.SaveAsDocument NewFileName, True
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: "SaveAs" locks files, and won't create copy 
Author Jeffrey Henry 
Date Jun 16, 2008 
Message Robert,
Did you ever find an answer to your question? I have come across the same problem and it is driving me nuts too!If you or anyone else figured out how to fix this please let me know! Thank you

Jeff Henry 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: "SaveAs" locks files, and won't create copy 
Author Robert Potter 
Date Jun 16, 2008 
Message Jeff,

After many months of corespondance with the ESRI expert here in Australia, she did manage to develope a work around. I must admit I have not actually tried it but I would hope it works. In the time since I originally posted this I have changed positons and thus it had become less important to me, but any I will include the code below. If you do try it I would love to know if it works.
===================================
Instructions on how it works, from ESRI
===================================
Essentially instead of using
IApplication::SaveasDocument,
I am using the Windows API Copyfile function - as described at :
http://support.microsoft.com/kb/172711
and SUCCESSFULLY tested via the code included at the end of this email.

I have also attached the 9.2 sp5 mxd, that I created to test this code.


All you need do to do is open the mxd, run the ShowForm macro ... which
opens
a form, and then click the contained commandbutton ... and it will
successfully
run the SaveCopy procedure, which :
1. saves the existing mxd,
2. Backs up the existing mxd to a new name (replacing the
IApplication::SaveasDocument method)
3. saves the mxds again

 
 
Option Explicit
   Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
      (ByVal lpExistingFileName As String, _
      ByVal lpNewFileName As String, _
      ByVal bFailIfExists As Long) As Long

Sub saveCopy()
On Error GoTo errHandle
    'get the app
    Dim pApp As IApplication
    Set pApp = Application
    'save once
    Call pApp.SaveDocument
    MsgBox "saved"
    'do a backup : this method gives the discovered bug
   ' Call pApp.SaveAsDocument("C:\Temp\Test.mxd", True)
   ' MsgBox "backed up"
    'do a Backup : this method gives a workaround, using the Windows API
CopyFile function
    ' as described at : http://support.microsoft.com/kb/172711
    Dim VbProj As Object
    Set VbProj = pApp.Document.VBProject
    Dim thePath As String
    Dim newMXDFilename As String
    thePath = Left(VbProj.FileName, (Len(VbProj.FileName) -
Len(pApp.Document.Title)))
    newMXDFilename = thePath & "replacement.mxd"
    CopyFile VbProj.FileName, newMXDFilename
    MsgBox "backed up"
    'save again - this fails on Windows 2000 Professional.... but with
workaround it does not fail
    Call pApp.SaveDocument
    MsgBox "saved again"
    Exit Sub

errHandle:
    MsgBox "Error: " & Err.Description & " num: " & Err.Number
End Sub
      Sub CopyFile(SourceFile As String, DestFile As String)
      '---------------------------------------------------------------
      ' PURPOSE: Copy a file on disk from one location to another.
      ' ACCEPTS: The name of the source file and destination file.
      ' RETURNS: Nothing
      '---------------------------------------------------------------
        Dim Result As Long
         If Dir(SourceFile) = "" Then
            MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
         Else
            Result = apiCopyFile(SourceFile, DestFile, False)
         End If
      End Sub
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: "SaveAs" locks files, and won't create copy 
Author Matt leaver 
Date Nov 27, 2008 
Message I don't know if anyone has tried the workaround described by Jeff above, but it works with Arc 9.2 desktop.

matt leaver

GIS Analyst
Capita Symonds 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: "SaveAs" locks files, and won't create copy 
Author Solana Foo 
Date May 11, 2010 
Message This worked for me too! Too bad ESRI is getting rid of the VBA interface! 
  SF