Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

commondialog problem 3

Status
Not open for further replies.

Jody72

IS-IT--Management
Sep 3, 2003
16
US
First off, thanks to everyone that has helped this newbie programmer get this far.

I can't seem to figure out to get my program to do a "Save As." The program runs fine, even pops up the ShowSave dialog box, but the file never actually saves.

Bascially, the user opens a file, it gets edited and written into test.txt. I then want the user to be able to specify a file name and have test.txt written to that file, then test.txt deleted.

Here's what I have thus far:


Private Sub Command1_Click()

CommonDialog1.DialogTitle = "Choose File to Open"
CommonDialog1.Filter = "ACL File (*.acl)|*.acl"
CommonDialog1.DefaultExt = "acl"
CommonDialog1.FilterIndex = 1
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen


Dim first, last, Space As String
Dim OutRecord(80), NewRecord As String
Space = " "
Open CommonDialog1.filename For Input As #1
Open App.Path & "\test.txt" For Output Shared As #2
' Read in record reformat and write out
' Remove col 1 len 8 left just

Do While Not EOF(1) ' Loop until end of file.
Line Input #1, first ' Read data
'Debug.Print first ' Print data to Debug window.
'
'OutRecord
i = 0
For j = 9 To 80
i = i + 1
OutRecord(i) = Mid(first, j, 1)
Next
' perpare output record
i = 0

NewRecord = Space
Do While i < 80
NewRecord = NewRecord & OutRecord(i)
i = i + 1

Loop
'Write #2,
Print #2, NewRecord

Loop

Close #1 ' Close file.


CommonDialog1.DialogTitle = &quot;Save File&quot;
CommonDialog1.Filter = &quot;CL File (*.cl)|*.cl&quot;
CommonDialog1.DefaultExt = &quot;cl&quot;
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt _
+ cdlOFNPathMustExist
CommonDialog1.CancelError = True
CommonDialog1.ShowSave


Close #2

Kill App.Path & &quot;\test.txt&quot;



End Sub



Jody

 
The ShowSave doesnot actually save the file it just returns a filepath and name. You just need to do a filecopy before the kill, ie

Filecopy App.Path & &quot;\test.txt&quot;, CommonDialog1.FileName

Also for correctness you should use Freefile to get your filenummbers instead of hard coding #1 and #2


Hope this helps
 
Try this Jody72 ...


Private Sub Command1_Click()
Dim int1 As Integer
Dim int2 As Integer
int1 = FreeFile
int2 = FreeFile

CommonDialog1.DialogTitle = &quot;Choose File to Open&quot;
CommonDialog1.Filter = &quot;ACL File (*.acl)|*.acl&quot;
CommonDialog1.DefaultExt = &quot;acl&quot;
CommonDialog1.FilterIndex = 1
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen


Dim first, last, Space As String
Dim OutRecord(80), NewRecord As String
Space = &quot; &quot;
Open CommonDialog1.FileName For Input As #int1
int2 = FreeFile
Open App.Path & &quot;\test.txt&quot; For Output Shared As #int2
' Read in record reformat and write out
' Remove col 1 len 8 left just

Do While Not EOF(1) ' Loop until end of file.
Line Input #int1, first ' Read data
'Debug.Print first ' Print data to Debug window.
'
'OutRecord
i = 0
For j = 9 To 80
i = i + 1
OutRecord(i) = Mid(first, j, 1)
Next
' perpare output record
i = 0

NewRecord = Space
Do While i < 80
NewRecord = NewRecord & OutRecord(i)
i = i + 1

Loop
'Write #2,
Print #int2, NewRecord

Loop

Close #int1 ' Close file.
Close #int2

int1 = FreeFile

CommonDialog1.DialogTitle = &quot;Save File&quot;
CommonDialog1.Filter = &quot;CL File (*.cl)|*.cl&quot;
CommonDialog1.DefaultExt = &quot;cl&quot;
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt _
+ cdlOFNPathMustExist
CommonDialog1.CancelError = True
CommonDialog1.ShowSave

Open CommonDialog1.FileName For Output As #int1
Print #int1, NewRecord
Close #int1

On Error Resume Next
Kill App.Path & &quot;\test.txt&quot;

End Sub
 
Or... do away with the temp file save/delete...

Private Sub Command1_Click()
Dim int1 As Integer
int1 = FreeFile


CommonDialog1.DialogTitle = &quot;Choose File to Open&quot;
CommonDialog1.Filter = &quot;ACL File (*.acl)|*.acl&quot;
CommonDialog1.DefaultExt = &quot;acl&quot;
CommonDialog1.FilterIndex = 1
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen


Dim first, last, Space As String
Dim OutRecord(80), NewRecord As String
Space = &quot; &quot;
Open CommonDialog1.FileName For Input As #int1

' Read in record reformat and write out
' Remove col 1 len 8 left jus
Do While Not EOF(1) ' Loop until end of file.
Line Input #int1, first ' Read data
'Debug.Print first ' Print data to Debug window.
'OutRecord
i = 0
For j = 9 To 80
i = i + 1
OutRecord(i) = Mid(first, j, 1)
Next
' perpare output record
i = 0

NewRecord = Space
Do While i < 80
NewRecord = NewRecord & OutRecord(i)
i = i + 1
Loop
Loop

Close #int1 ' Close file.

int1 = FreeFile

CommonDialog1.DialogTitle = &quot;Save File&quot;
CommonDialog1.Filter = &quot;CL File (*.cl)|*.cl&quot;
CommonDialog1.DefaultExt = &quot;cl&quot;
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt _
+ cdlOFNPathMustExist
CommonDialog1.CancelError = True
CommonDialog1.ShowSave

Open CommonDialog1.FileName For Output As #int1
Print #int1, NewRecord
Close #int1

End Sub
 
Thank you to both of you. That helped me alot.
 
Jody72,

Welcome to the forum.
I see you haven't yet marked any answers as helpful. Have a look at paragraph 15 of faq222-2244 if you found any of the answers to any of your questions either helpful or expert.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top