PDA

View Full Version : Visual basic


neo_lover2000
Jun 25, 2009, 10:48 PM
I made this code to save text file. But now I am wondering how to save a file if there is an existing file name. I would like to add a mnuItemSave for that. Is it work? How to code that?

Private Sub mnuItemsave_Click()
CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
CommonDialog1.ShowSave 'display Save dialog
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Append As #1
Print #1, txtFile.Text 'save string to file
Close #1 'close file
End If
End Sub

Private Sub mnuItemOpen_Click()
Wrap$ = Chr$(13) + Chr$(10) 'create wrap character
CommonDialog1.Filter = "Text files (*.TXT)|*.TXT|EXCEL files (*.xls)|*.xls|All Files (*.*)|*.*"
CommonDialog1.ShowOpen 'display Open dialog box
If CommonDialog1.FileName <> "" Then
' Form1.MousePointer = 11 'display hourglass
Open CommonDialog1.FileName For Input As #1

' On Error GoTo TooBig: 'set error handler

Do Until EOF(1) 'then read lines from file
Line Input #1, LineOfText$
AllText$ = AllText$ & LineOfText$ & Wrap$
Loop
lblFile.Caption = CommonDialog1.FileName
txtFile.Text = AllText$ 'display file
txtFile.Enabled = True
mnuItemClose.Enabled = True
mnuItemOpen.Enabled = False
End If
Close #1
'CleanUp:
' Form1.MousePointer = 0 'reset mouse
' Close #1 'close file
' End If
'Exit Sub
'TooBig: 'error handler displays message
' MsgBox ("The specified file is too large.")
' Resume CleanUp: 'then jumps to CleanUp routine
End Sub


Private Sub mnuItemClose_Click()
txtFile.Text = "" 'clear text box
lblFile.Caption = "Load a text file with the Open command."
mnuItemClose.Enabled = False 'dim Close command
mnuItemOpen.Enabled = True 'enable Open command
txtFile.Enabled = False 'disable text box
End Sub

Private Sub mnuItemExit_Click()
End 'quit program
End Sub

thanks

Perito
Jun 26, 2009, 08:13 AM
i made this code to save text file. but now i am wondering how to save a file if there is an existing file name. i would like to add a mnuItemSave for that. is it work? how to code that?


Let's see if I understand this. You want to save a file if the user has already specified a file name. Is this what you mean? In the following, when the user specifies a file name, it's saved as a module-level variable (not inside a subroutine), LastFileName. If the user clicks a menu item labeled "Save Existing" (mnuSaveExisting), it calls the second subroutine, below. That subroutine checks the LastFileName variable to make sure that it's not empty and then does the same thing that the mnuItemsave_click subroutine does -- except it doesn't call the CommonDialog1 object.

Private LastFileName As String

Private Sub mnuItemsave_Click()
CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
CommonDialog1.ShowSave 'display Save dialog
If CommonDialog1.FileName <> "" Then
LastFileName = CommonDialog1.FileName
Open CommonDialog1.FileName For Append As #1
Print #1, txtfile.Text 'save string to file
Close #1 'close file
End If
End Sub

Private Sub mnuExisting_Click()
If LastFileName <> "" Then
Open LastFileName For Append As #1
Print #1, txtfile.Text 'save string to file
Close #1 'close file
End If
End Sub