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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CommonDialog1 problem 2

Status
Not open for further replies.

utmielili

Programmer
Jul 7, 2004
4
0
0
US
It seems that my CommonDialog1.* Functions don't work when I try to use Open/Save File with them. Who could give me any solutions or right keywords?
 
In the following example of .ShowOpen from MSDN its not obvious but the routine OpenFile is a user supplied routine, the CommonDialog only returns the files spec. To get more info look at the vb help for the control, also search for "CommonDialog Control Constants" for the various flag options.
Code:
Private Sub mnuFileOpen_Click ()
   ' CancelError is True.
   On Error GoTo ErrHandler
   ' Set filters.
   CommonDialog1.Filter = "All Files (*.*)|*.*|Text _
   Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
   ' Specify default filter.
   CommonDialog1.FilterIndex = 2

   ' Display the Open dialog box.
   CommonDialog1.ShowOpen 
   ' Call the open file procedure.
   OpenFile (CommonDialog1.FileName)
   Exit Sub

ErrHandler:
' User pressed Cancel button.
   Exit Sub
End Sub

This example shows a function that returns the name of a file using .ShowSave
Code:
Public Function f_GetLogFolder() As String

On Error GoTo ErrorHandler
 
With CommonDialog1
    .Filter = "log file (Log.csv)|Log.csv"
    .FilterIndex = 1
    .DefaultExt = ""
    .DialogTitle = "Get log file"
    .Flags = cdlOFNHideReadOnly _
              Or cdlOFNPathMustExist _
              Or cdlOFNExtensionDifferent
    .CancelError = True
    .FileName = "Log.csv"
    .InitDir = "c:\temp"
    On Error GoTo CancelHandler    'cancel sets error
    .ShowSave
    On Error GoTo ErrorHandler
    f_GetLogFolder = .FileName
End With

Exit Function
   
CancelHandler:
    f_GetLogFolder = ""
    Exit Function

ErrorHandler:
   MsgBox "Error " & Err.Number & Err.Description
   Resume

Hope this helps get you started.
 
Show us the code that doesn't work. Maybe that could help us help you.

Sam
 
Hi, SonOfEmidec1100 and Sam,
Could you help me find out what's wrong with the code below? By the way, I am learning VB6, thanks for your great tips.
------------------------------------------------------

Private Sub mnuItemSave_Click()
CommonDialog1.Filter = "Text files (*.TXT)|*.TXT"
CommonDialog1.ShowSave
If CommonDialog1.FileName <> " " Then
Open CommonDialog1.FileName For Output As #1
Print #1, txtNote.Text
Close #1
End If
End Sub
 
Two problems the " " should be an empty string ie "", however the string is not always empty after pressing cancel. The recommended way is to use cancelerror.
Code:
Private Sub mnuItemSave_Click()
With CommonDialog1
    .Filter = "Text files (*.TXT)|*.TXT"
    .CancelError = True
    On Error GoTo CancelHandler    
    .ShowSave
    Open .filename For Output As #1
    Print #1, txtNote.Text
    Close #1
End With
Exit Sub

CancelHandler:
    Exit Sub
  
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top