PDA

View Full Version : Display overwrite alert when saving an ASCII file



Tyke
23rd Sep 2010, 02:07 pm
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?

BlackBox
23rd Sep 2010, 02:37 pm
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 SaveFileName Then
If SaveFileName_Exists=Yes Then
If PromptForOverwrite=Yes Then
' Overwrite file
Else
' Prompt for new filename (start this process over?
' Perhaps make it a sub-routine?)
End If
End If
Else
' Create/Save new file
End If


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

Hope this helps!

Tyke
23rd Sep 2010, 03:32 pm
I tried adapting the code, but without success. Thanks for the advice.