Solved

dialog boxes and file i/o

Posted on 1997-03-18
7
271 Views
Last Modified: 2008-03-17
File I/0

I’m a little confused about file saving and save as;  The below program opens up text files with out a problem.  I’ve managed to cal the save (and saveAs) dialog boxes.  I’d like to have the save function to overwrite the existing file using the previous file name, providing the file has not been saved before (how can I tell this?) .  How would I add a msgbox that would ask the user if he would like to save any changes befor exiting?



Private Sub Command1_Click()

End Sub

Private Sub cmdCloseFile_Click()
   
        Close
        cmdOpenFile.Enabled = True
        txtWorkPad.Text = ""
        txtFileNameOpen.Text = ""
End Sub

Private Sub cmdOpenFile_Click()

    On Error GoTo DialogError
   
    cmOpenFile.ShowOpen
    Open cmOpenFile.filename For Input As 1
    txtWorkPad.Text = Input(LOF(1), 1)
    txtFileNameOpen = cmOpenFile.filename
   
    cmdOpenFile.Enabled = False
   
DialogError:
    On Error GoTo 0
    Exit Sub
   
End Sub


Private Sub Form_Load()

' Set up the common dialog control to open a text file

    On Error GoTo DialogError

    With cmOpenFile
        .CancelError = True
        .Filter = "Text Files (*.txt)|*.txt"
        .FilterIndex = 1
        .DialogTitle = "Select a Text File"
    End With

DialogError:
    On Error GoTo 0
    Exit Sub

End Sub

Private Sub mnuEditReplace_Click()

   frmSearchString.Show 1

   
End Sub

Private Sub mnuFileClose_Click()

' Close all open files
        Close
' reset the menus and the caption bar

        mnuFileOpen.Enabled = True
        txtWorkPad.Text = ""
        frmWordPad.Caption = "WordPad"
        mnuFileClose.Enabled = False
        mnuFileSave.Enabled = False


End Sub


Private Sub mnuFileExit_Click()
    End
End Sub


Private Sub mnuFileOpen_Click()

    On Error GoTo DialogError

' Common dialog has been set up in form load event
        cmOpenFile.ShowOpen
' take filename selected by user and open it up
        Open cmOpenFile.filename For Input As 1
' read the whole file into the text box
        txtWorkPad.Text = Input(LOF(1), 1)
       
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
' don't allow another file to be opened
        mnuFileOpen.Enabled = False
' however we can save or close the file
        mnuFileClose.Enabled = True
        mnuFileSave.Enabled = True
        mnuFileSaveAs.Enabled = True
       
DialogError:
        On Error GoTo 0
        Exit Sub
   
    End Sub






Private Sub mnuFileSave_Click()

    On Error GoTo DialogError

'Open up a save file dialog using the previous properties, except title
        cmOpenFile.DialogTitle = "Save Your Text File"
        cmOpenFile.ShowSave
       
        Open cmOpenFile.filename For Output As 1
' put the whole text box into the file
        Print #1, txtWorkPad.Text
' now update the filename on the caption
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
        mnuFileClose.Enabled = True
        Open cmOpenFile.filename For Output As 1
       
DialogError:
    On Error GoTo 0
    Exit Sub
    Close
   
End Sub

Private Sub mnuFileSaveAs_Click()
On Error GoTo DialogError

'Open up a save file dialog using the previous properties, except title
        cmOpenFile.DialogTitle = "Save As"
        cmOpenFile.ShowSave
       
        Open cmOpenFile.filename For Output As 1
' put the whole text box into the file
        Print #1, txtWorkPad.Text
' now update the filename on the caption
        frmWordPad.Caption = "WordPad " + cmOpenFile.filename
        mnuFileClose.Enabled = True
       
DialogError:
    On Error GoTo 0
    Exit Sub
    Close

End Sub
0
Comment
Question by:johndill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:Sargon
ID: 1424617
All this for 50 points?
0
 

Author Comment

by:johndill
ID: 1424618
Well as you can see I wrote most of the code myself.  All I need help with is the mechanics of Save ans Save As.  I've already crested every thing else.  What say you to 70?
0
 

Expert Comment

by:limacher
ID: 1424619
At no point in your code example, have you used FLAGS.

I've had a brief view of the code, it's a little to much for one question and 70 points.

Look in the Help File for the FLAGS property of the Common Dialog Control. This will assist in with most the code.

PS.......If you are opening and Closing a text file, then why not use the RichTextFormat control. It's easier, faster and more code efficiant. Note that it 32bit only.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:johndill
ID: 1424620

0
 
LVL 2

Expert Comment

by:dirkmartin
ID: 1424621
I'd need 150 points for this one.
0
 

Author Comment

by:johndill
ID: 1424622
Thanks I'll try the flags.  I can only offer 90 at this point as my account is tapped
0
 
LVL 2

Accepted Solution

by:
weekee earned 90 total points
ID: 1424623
Basically you are trying to detect whether a user has save a file or. My approach is not very classic but it si simple. Simply creat a variable called saveFlag as boolean. Every time i open a new file  and the text is modified i set the flag to false. This is done by setting the flag to false in the keypress event of the textbox where the text is displayed. So what you need to do is compared whether this flag is False before exiting. If it is false, prompt the user. Once the file is save, set the flag to true...
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question