Jump to content

Display overwrite alert when saving an ASCII file


Tyke

Recommended Posts

I have the situation where the insertion points of a number of circles are written to an ASCII file for use in another programm. A filename is automatically sugested and if the user accepts it the file is saved, overwriting any existing file with the same name, if it exists and without displaying an alert message, eg "Do want to overwrite the existing file?".

 

Hers's the code for the function that I'm currently using

 

Public Function ShowSave(Filter As String, _
                        InitialDir As String, _
                        DialogTitle As String) As String
                        
   Dim SFName As SAVEFILENAME
   ShowSave = ""
     ' suggest a file name
     SFName.lpstrFile = strAsciiFileName
     'Set the structure size
     SFName.lStructSize = Len(SFName)
     'Set the owner window
     SFName.hwndOwner = 0
     'Set the filter
     SFName.lpstrFilter = Filter
     'Set the maximum number of chars
     SFName.nMaxFile = 255
     'Create a buffer
     SFName.lpstrFileTitle = strAsciiFileName     
     'Set the maximum number of chars
     SFName.nMaxFileTitle = 255
     'Set the initial directory
     SFName.lpstrInitialDir = InitialDir
     'Set the dialog title
     SFName.lpstrTitle = DialogTitle
     'no extra flags
     SFName.flags = 0
     'Show the 'SaveAs File’ dialog
     If GetSaveFileName(SFName) Then
       ShowSave = Trim(SFName.lpstrFile)
       Else
         ShowSave = "QUIT"
     End If
     
End Function

Is it possible to easily implement such an alert?

Link to comment
Share on other sites

Yes.

 

I do not do much VBA development (I primarily use the Visual LISP ActiveX COM API), but conceptually speaking...

 

You could use a nested if statement, such that once the user accepts a filename, *IF* the filename already exists, prompt for overwrite, else create/save new file.

 

For example (forgive my poor VBA syntax):

 

If [color=red]Save[/color][color=red]FileName[/color] Then
 If [color=red]Save[color=red]FileName_[/color]Exists[/color]=[color=blue]Yes[/color] Then
   If [color=red]PromptForOverwrite[/color]=[color=blue]Yes[/color] Then
     [color=seagreen]' Overwrite file[/color]
     Else
       [color=seagreen]' Prompt for new filename (start this process over? [/color]
[color=seagreen]        ' Perhaps make it a sub-routine?)[/color]
   End If
 End If
 Else
   [color=seagreen]' Create/Save new file[/color]
End If

 

The LISP function that comes to mind is findfile (not sure of the vba equivillent?).

 

Hope this helps!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...