Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private Sub mnuFile_Click(Index As Integer)
Dim strName As String, strFile As String, strTemp As String
Dim Answer As Integer
Select Case Index
Case 0: ' NEW
' If a file is already open, offer the user the option to save it before creating a new file
On Error GoTo ErrorHandler
' Check if there is a file already open
If TextBox.Text <> "" Then
Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before creating another?", vbYesNoCancel, "Save your file?")
Select Case Answer
Case vbYes:
' Save the existing file if the user selects YES then clear the textbox
If CommonDialog.FileName = "" Then ' The file hasn't been saved before
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Save your file..."
.ShowSave
End With
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
Else ' The file has been saved before
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
End If
TextBox.Text = ""
TextBox.SetFocus
Case vbNo:
' Don't save existing file if user selects NO and clear the textbox
Close #1
TextBox.Text = ""
Case vbCancel:
TextBox.SetFocus
End Select
End If
Case 1: ' OPEN
On Error GoTo ErrorHandler
' Check if there is a file already open
If TextBox.Text <> "" Then
Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before opening another?", vbYesNoCancel, "Save your file?")
Select Case Answer
Case vbYes:
' Save the existing file if the user selects YES before opening a new file
If CommonDialog.FileName = "" Then ' The file hasn't been saved before
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Save your file..."
.ShowSave
End With
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
Else ' The file has been saved before
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
End If
' File is now saved, we can open the new file
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Open a file..."
.ShowOpen
End With
strName = CommonDialog.FileName
Open strName For Input As #1
strFile = ""
Do Until EOF(1)
Line Input #1, strTemp
strFile = strFile & strTemp & vbCrLf
Loop
TextBox.Text = strFile
Close #1
' The new file is now opened into the textbox
Case vbNo:
' Don't save existing file if user selects NO and open an new file
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Open a file..."
.ShowOpen
End With
strName = CommonDialog.FileName
Open strName For Input As #1
strFile = ""
Do Until EOF(1)
Line Input #1, strTemp
strFile = strFile & strTemp & vbCrLf
Loop
TextBox.Text = strFile
Close #1
Case vbCancel:
TextBox.SetFocus
End Select
Else
' There is no file open to begin with, so open a new file
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Open a file..."
.ShowOpen
End With
strName = CommonDialog.FileName
Open strName For Input As #1
strFile = ""
Do Until EOF(1)
Line Input #1, strTemp
strFile = strFile & strTemp & vbCrLf
Loop
TextBox.Text = strFile
Close #1
End If
Case 2: ' SAVE
On Error GoTo ErrorHandler
If CommonDialog.FileName = "" Then ' The file hasn't been saved before
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Save your file..."
.ShowSave
End With
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
Else ' The file has been saved before
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
End If
Case 3: ' SAVE AS
On Error GoTo ErrorHandler
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Save your file..."
.ShowSave
End With
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
Case 5: ' EXIT
' If a file is already open, offer the user the option to save it before exiting
On Error GoTo ErrorHandler
' Check if there is a file already open
If TextBox.Text <> "" Then
Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before creating another?", vbYesNoCancel, "Save your file?")
Select Case Answer
Case vbYes:
' Save the existing file if the user selects YES then close the application
If CommonDialog.FileName = "" Then ' The file hasn't been saved before
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Save your file..."
.ShowSave
End With
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
' File saved, close the app
Unload Me
End
Else ' The file has been saved before
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
End If
' File saved, close the app
Unload Me
End
Case vbNo:
' Don't save existing file if user selects NO and close the app
Close #1
Unload Me
End
Case vbCancel:
TextBox.SetFocus
End Select
End If
Unload Me
End
End Select
Exit Sub
ErrorHandler:
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim strName As String, strFile As String, strTemp As String
Dim Answer As Integer
' If a file is already open, offer the user the option to save it before exiting
On Error GoTo ErrorHandler
' Check if there is a file already open
If TextBox.Text <> "" Then
Answer = MsgBox("Changes made to the file you are currently working on will be lost. Do you wish to save this file before creating another?", vbYesNoCancel, "Save your file?")
Select Case Answer
Case vbYes:
' Save the existing file if the user selects YES then close the application
If CommonDialog.FileName = "" Then ' The file hasn't been saved before
With CommonDialog
.Filter = "Text (.txt)|*.txt"
.InitDir = "C:\My Documents"
.DialogTitle = "Save your file..."
.ShowSave
End With
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
' File saved, close the app
Unload Me
End
Else ' The file has been saved before
strName = CommonDialog.FileName
Open strName For Output As #1
Print #1, TextBox.Text
Close #1
MsgBox "Your file has been saved as " & CommonDialog.FileTitle, vbOKOnly, "File save confirmation"
End If
' File saved, close the app
Unload Me
End
Case vbNo:
' Don't save existing file if user selects NO and close the app
Close #1
Unload Me
End
Case vbCancel:
TextBox.SetFocus
End Select
End If
Unload Me
End
Exit Sub
ErrorHandler:
End Sub