Discussion:
VB6 Common Dialog Save
(too old to reply)
n***@notsosmart.fen
2007-08-04 18:13:04 UTC
Permalink
I want to save file Punk_History.def to a directory chosen by the
user. When I execute the code below, the dialog box says the file has
been saved. Problem is that the file does not exist in the directory
I chose to save it in. Why doesn't it work?


Dim Pathy as string
Pathy = "C:\Program Files\Games\"

With CommonDialog
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".abd"
.Filename = "Punk_History.def"
.InitDir = Pathy
.ShowSave
MsgBox "You saved the files as " & .Filename
End With
Bill Plenge
2007-08-04 18:40:48 UTC
Permalink
Post by n***@notsosmart.fen
I want to save file Punk_History.def to a directory chosen by the
user. When I execute the code below, the dialog box says the file has
been saved. Problem is that the file does not exist in the directory
I chose to save it in. Why doesn't it work?
Dim Pathy as string
Pathy = "C:\Program Files\Games\"
With CommonDialog
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".abd"
.Filename = "Punk_History.def"
.InitDir = Pathy
.ShowSave
MsgBox "You saved the files as " & .Filename
End With
The Common Dialog box allows you to pick, or type in a file name to save the
file as, it doesn't save anything in and of itself. You'd need to redo your
code above as ...

Everything within your With statement except the MsgBox Statement
Insert code here to actually create the file and write the information into
it then close the file
Your MsgBox reporting a successful save, or a failed one if it didn't work.

Hope this is some help.


Best,
Bill
Rod Johnson
2007-08-04 21:15:06 UTC
Permalink
Hi,

Perhaps you might like to try this Code:

********************************************************
Private Sub Command1_Click()

On Error GoTo errhandler

Dim Pathy, x As String

Pathy = "C:\Program Files\Games"

'Firstly, check if the Folder exists. If not, then create it...
x = Dir(Pathy, 16)
If x = "" Then
MkDir Pathy
Else
End If

With CommonDialog1
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.flags = cdlOFNHideReadOnly
.flags = cdlOFNOverwritePrompt
.flags = cdlOFNPathMustExist
.flags = cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open 'This is used to specify which
extension is displayed on top.
.DefaultExt = ".abd"
.FileName = "Punk_History.def"
.InitDir = Pathy
.ShowSave
End With

'At this point, I don't know where the File "Punk_History.def" is located,
'so I will assume that it is in the Folder where you are running the VB App.
'If not, then you will have to change the path...

Open App.Path & "\Punk_History.def" For Input As #1
Open CommonDialog1.FileName For Output As #2
Do Until EOF(1)
Line Input #1, x
Print #2, x
Loop
Close

'Now, check if the file is in the Pathy folder

x = Dir(CommonDialog1.FileName, 0)
If Not x = "" Then
MsgBox "You Saved the File as " & CommonDialog1.FileName, vbInformation,
"File Test"
Else
MsgBox "FAILURE! The File was NOT Saved.", vbCritical, "File Test"
End If

Exit Sub

done1:
Exit Sub

errhandler:
Close
If Left(Error, 6) = "Cancel" Then Resume done1 Else
MsgBox Error
Resume done1

End Sub
********************************************************
Regards,

Rod Johnson
Post by n***@notsosmart.fen
I want to save file Punk_History.def to a directory chosen by the
user. When I execute the code below, the dialog box says the file has
been saved. Problem is that the file does not exist in the directory
I chose to save it in. Why doesn't it work?
Dim Pathy as string
Pathy = "C:\Program Files\Games\"
With CommonDialog
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".abd"
.Filename = "Punk_History.def"
.InitDir = Pathy
.ShowSave
MsgBox "You saved the files as " & .Filename
End With
n***@notsosmart.fen
2007-08-05 13:18:23 UTC
Permalink
Post by Rod Johnson
Hi,
********************************************************
Private Sub Command1_Click()
On Error GoTo errhandler
Dim Pathy, x As String
Pathy = "C:\Program Files\Games"
'Firstly, check if the Folder exists. If not, then create it...
x = Dir(Pathy, 16)
If x = "" Then
MkDir Pathy
Else
End If
With CommonDialog1
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.flags = cdlOFNHideReadOnly
.flags = cdlOFNOverwritePrompt
.flags = cdlOFNPathMustExist
.flags = cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open 'This is used to specify which
extension is displayed on top.
.DefaultExt = ".abd"
.FileName = "Punk_History.def"
.InitDir = Pathy
.ShowSave
End With
'At this point, I don't know where the File "Punk_History.def" is located,
'so I will assume that it is in the Folder where you are running the VB App.
'If not, then you will have to change the path...
Open App.Path & "\Punk_History.def" For Input As #1
Open CommonDialog1.FileName For Output As #2
Do Until EOF(1)
Line Input #1, x
Print #2, x
Line Input and Print work on a text file. Punk_History.def is a
binary file, not a text file. Since I couldn't use CommonDialog to
copy a binary file, I used another solution. I use the CommonDialog
because it is familiar to all users, but the file copying is using the
FileSystemObject. The darn thing works. The user uses the
commondialog to determine where to copy the file to, and fPath does
the actual copying.

Dim fPath As New FileSystemObject, FilePath

Pathy = "C:\Program Files\Games"

With CommonDialog
On Error GoTo Pinko
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir '&H1000 Or &H200000 Or &H8 Or
&H2 Or &H800
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".def"
.Filename = "Punk_History.def"
.InitDir =Pathy
.ShowSave
FilePath = .Filename
If Err.Number Then
'Caption = "Error"
fPath.CopyFile Pathy & "\" & "Punk_History.def", FilePath,
False
Else
'Caption = "Overwrite"
fPath.CopyFile Pathy & "\" & "Punk_History.def", FilePath,
True
End If
End With

Exit Sub

Pinko:
If Err.Number = 32755 Then
'User hit the Cancel button
Exit Sub
End If
Neila
2007-08-07 06:47:33 UTC
Permalink
Well, most people with any sense disable the FSO!
Good luck, happy trails!
Freaking System Obombonation!
Post by n***@notsosmart.fen
Post by Rod Johnson
Hi,
********************************************************
Private Sub Command1_Click()
On Error GoTo errhandler
Dim Pathy, x As String
Pathy = "C:\Program Files\Games"
'Firstly, check if the Folder exists. If not, then create it...
x = Dir(Pathy, 16)
If x = "" Then
MkDir Pathy
Else
End If
With CommonDialog1
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.flags = cdlOFNHideReadOnly
.flags = cdlOFNOverwritePrompt
.flags = cdlOFNPathMustExist
.flags = cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open 'This is used to specify which
extension is displayed on top.
.DefaultExt = ".abd"
.FileName = "Punk_History.def"
.InitDir = Pathy
.ShowSave
End With
'At this point, I don't know where the File "Punk_History.def" is located,
'so I will assume that it is in the Folder where you are running the VB App.
'If not, then you will have to change the path...
Open App.Path & "\Punk_History.def" For Input As #1
Open CommonDialog1.FileName For Output As #2
Do Until EOF(1)
Line Input #1, x
Print #2, x
Line Input and Print work on a text file. Punk_History.def is a
binary file, not a text file. Since I couldn't use CommonDialog to
copy a binary file, I used another solution. I use the CommonDialog
because it is familiar to all users, but the file copying is using the
FileSystemObject. The darn thing works. The user uses the
commondialog to determine where to copy the file to, and fPath does
the actual copying.
Dim fPath As New FileSystemObject, FilePath
Pathy = "C:\Program Files\Games"
With CommonDialog
On Error GoTo Pinko
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir '&H1000 Or &H200000 Or &H8 Or
&H2 Or &H800
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".def"
.Filename = "Punk_History.def"
.InitDir =Pathy
.ShowSave
FilePath = .Filename
If Err.Number Then
'Caption = "Error"
fPath.CopyFile Pathy & "\" & "Punk_History.def", FilePath,
False
Else
'Caption = "Overwrite"
fPath.CopyFile Pathy & "\" & "Punk_History.def", FilePath,
True
End If
End With
Exit Sub
If Err.Number = 32755 Then
'User hit the Cancel button
Exit Sub
End If
Mike Williams
2007-08-07 07:58:58 UTC
Permalink
Punk_History.def is a binary file,
not a text file.
This is the first time you've told us what kind of file you are
dealing with and until that little secret was out of the bag the other
respondents simply posted a sample showing you how to write a text
based data file purely as an example. The main point of their
responses was to inform you that the CommonDialog does not actually
write any files for you. They probably assumed that once they had told
you that little fact you would be able to write the file yourself
using a method suitable to the file you are dealing with.
I use a CommonDialog because it is familiar to
all users, but the file copying [since I was taught
how the CommonDialog works by the previous kind
reponses to my question!!!] is using the
FileSystemObject.
Now it appears that you are copying a file [where the meaning is quite
clear] instead of writing one [where it is not clear, because you
could have meant either crerating from new or copying or almost
anything]. This is the first time you have told us that you are
copying an existing file rather than creating one.
The darn thing works [the FileSystem object]
Not on my system it doesn't. Nor on my wife's laptop. And I've just
nipped over to Uncle Charlie's Kebab shop where he does the accounts
and, blow me down with a feather, it doesn't work on his computer
either! I wonder why? Is it perhaps because the FileSystem object
relies on a scripting system that is historically viewed as a
potential virus delivery system due to the fact that such scripts can
be contained in otherwise innocuous files such as word processor
documents and spreadsheets and things? Do you think the administrator
might have disabled the little beast in order to protect his sytem
from it? How sad. I would advise you to follow the advice of Uncle
Charlie in the Kebab shop and ditch the FSO :-)
Line Input and Print work on a text file.
Ditch the FileSystem object and have a look at standard VB6 file
handling. Regarding your comments about Input and Print (which of
course deal mostly with ascii data or ascii repesentations of the
contents of variables of one sort or another, since that is their job)
have you looked at all the other built in VB file I/O methods? You'll
find all sorts of things such as Open for Binary and Get and Put and
lots of other stuff that allows you to handle files and file data in
just about any way you wish, including very easy binary data handling.
The user uses the commondialog to determine
where to copy the file to, and fPath [part of
my obnoxious and foul FSO code ;-)] does
the actual copying.
Check out the VB FileCopy statement :-)

Mike
Larry Linson
2007-08-08 00:59:41 UTC
Permalink
On the other hand, Mike, perhaps he is always executing in his own
environment and neither he, nor a system administrator, has chosen to
disable FSO (and, if really careful, search and destroy any "rogue
reinstalls" of the WSH on every startup), as many do. If you can count on
it being there, FSO pretty much works as advertised, though for the things I
do, it has never proven any better, and sometimes not so good as, regular,
traditional VB File I/O.

I remember FSO being highly touted it in a column in one of the trade
magazines. You'd have thought it was the greatest discovery since slicing
bread (or an even more monumental discovery sometimes used as a simile), but
for each of the examples used, I created a File I/O example that did exactly
the same thing each with at least one less line of code. When I e-mailed
about it, the columnist had a hard time not admitting that someone at
Microsoft had done a good sales job about the wondrousness of FSO.

A line of code, more or less, is no big deal, but why spend time and effort
to learn a "new thing," in order to be even minutely LESS efficient, even if
you could count on it being in random systems where your code might be
executed (which you can't)?

Larry
Post by Mike Williams
Punk_History.def is a binary file,
not a text file.
This is the first time you've told us what kind of file you are
dealing with and until that little secret was out of the bag the other
respondents simply posted a sample showing you how to write a text
based data file purely as an example. The main point of their
responses was to inform you that the CommonDialog does not actually
write any files for you. They probably assumed that once they had told
you that little fact you would be able to write the file yourself
using a method suitable to the file you are dealing with.
I use a CommonDialog because it is familiar to
all users, but the file copying [since I was taught
how the CommonDialog works by the previous kind
reponses to my question!!!] is using the
FileSystemObject.
Now it appears that you are copying a file [where the meaning is quite
clear] instead of writing one [where it is not clear, because you
could have meant either crerating from new or copying or almost
anything]. This is the first time you have told us that you are
copying an existing file rather than creating one.
The darn thing works [the FileSystem object]
Not on my system it doesn't. Nor on my wife's laptop. And I've just
nipped over to Uncle Charlie's Kebab shop where he does the accounts
and, blow me down with a feather, it doesn't work on his computer
either! I wonder why? Is it perhaps because the FileSystem object
relies on a scripting system that is historically viewed as a
potential virus delivery system due to the fact that such scripts can
be contained in otherwise innocuous files such as word processor
documents and spreadsheets and things? Do you think the administrator
might have disabled the little beast in order to protect his sytem
from it? How sad. I would advise you to follow the advice of Uncle
Charlie in the Kebab shop and ditch the FSO :-)
Line Input and Print work on a text file.
Ditch the FileSystem object and have a look at standard VB6 file
handling. Regarding your comments about Input and Print (which of
course deal mostly with ascii data or ascii repesentations of the
contents of variables of one sort or another, since that is their job)
have you looked at all the other built in VB file I/O methods? You'll
find all sorts of things such as Open for Binary and Get and Put and
lots of other stuff that allows you to handle files and file data in
just about any way you wish, including very easy binary data handling.
The user uses the commondialog to determine
where to copy the file to, and fPath [part of
my obnoxious and foul FSO code ;-)] does
the actual copying.
Check out the VB FileCopy statement :-)
Mike
Mike Williams
2007-08-08 01:34:09 UTC
Permalink
Post by Larry Linson
On the other hand, Mike, perhaps he is always
executing in his own environment and neither he,
nor a system administrator, has chosen to disable
FSO (and, if really careful, search and destroy
any "rogue reinstalls" of the WSH on every startup)
Yeah. He should be okay if he uses it only on known systems that
definitely do have the scripting stuff installed and enabled and that
will always be in such a state. The important thing is that he
realises his code won't work in Uncle Charlie's Kebab shop and in lots
of other places ;-)

Mike
Larry Linson
2007-08-08 02:04:46 UTC
Permalink
Post by Mike Williams
Yeah. He should be okay if he uses it only on known systems that
definitely do have the scripting stuff installed and enabled and that
will always be in such a state. The important thing is that he
realises his code won't work in Uncle Charlie's Kebab shop and in lots
of other places ;-)
Aw, darn it, Mike... kebabs sound awfully enticing. Too bad Uncle Charlie's
is half a world away from where I am now!

I did a File I/O presentation for my user group once, and considered
mentioning FSO, but then couldn't think of a good reason to cover it, except
to warn them about "Uncle Charlie's and other places" in case someone tried
to sell them that bill of goods.

I heard of one very sad developer who'd conferred with his SysAdmin and used
a lot of FSO on the SysAdmin's assurance that he wasn't going to remove WSH.
He had a lot of work to do when that SysAdmin moved on to "bigger and
better" and a new SysAdmin recoiled in horror at the idea of WSH "on my
system" and went on a search and destroy mission.

Larry

Rod Johnson
2007-08-04 21:22:33 UTC
Permalink
Hi Again,

Just noticed when viewing the Group Posting of the Code I sent, that some of
the
Lines of Code were wrapped around where they should not be. Be careful and
sort these out or you will have errors.

If you have any other questions, please contact me.

Regards, Rod
Post by n***@notsosmart.fen
I want to save file Punk_History.def to a directory chosen by the
user. When I execute the code below, the dialog box says the file has
been saved. Problem is that the file does not exist in the directory
I chose to save it in. Why doesn't it work?
Dim Pathy as string
Pathy = "C:\Program Files\Games\"
With CommonDialog
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".abd"
.Filename = "Punk_History.def"
.InitDir = Pathy
.ShowSave
MsgBox "You saved the files as " & .Filename
End With
Rod Johnson
2007-08-05 08:01:56 UTC
Permalink
Hi,

In re-running the Code I sent to you, I found that the Overwrite Statement
is not
always reliable. If this happens, then insert this Code below under "End
With"

Regards, Rod
*************************************
x = Dir(CommonDialog1.FileName, 0)
If Not x = "" Then
x = MsgBox("This File Already Exists!" & vbCrLf & "Do You Wish to Overwrite
It?", vbQuestion & vbYesNo, "File Test")
If x = vbNo Then Exit Sub Else
Else
End If
*************************************
Post by n***@notsosmart.fen
I want to save file Punk_History.def to a directory chosen by the
user. When I execute the code below, the dialog box says the file has
been saved. Problem is that the file does not exist in the directory
I chose to save it in. Why doesn't it work?
Dim Pathy as string
Pathy = "C:\Program Files\Games\"
With CommonDialog
.DialogTitle = "Archive File - Punk_History.def"
.CancelError = True
.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt +
cdlOFNPathMustExist + cdlOFNNoChangeDir
.Filter = "All Files(*.def)|*.def"
.FilterIndex = 1 '1 = Save and 2 = Open
.DefaultExt = ".abd"
.Filename = "Punk_History.def"
.InitDir = Pathy
.ShowSave
MsgBox "You saved the files as " & .Filename
End With
Steve Gerrard
2007-08-05 08:55:26 UTC
Permalink
Hi,
In re-running the Code I sent to you, I found that the Overwrite Statement is
not
always reliable. If this happens, then insert this Code below under "End With"
Your code contained:
.flags = cdlOFNHideReadOnly
.flags = cdlOFNOverwritePrompt
.flags = cdlOFNPathMustExist
.flags = cdlOFNNoChangeDir

which makes .Flags = cdlOFNNoChangeDir, with none of the other three set.

You need to Or the flags together:
.Flags = cdlOFNHideReadOnly _
Or cdlOFNOverwritePrompt _
Or cdlOFNPathMustExist _
Or cdlOFNNoChangeDir
Continue reading on narkive:
Loading...