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