Results 1 to 6 of 6

Thread: [RESOLVED] Can I save the contents in a listbox using CommonDialog?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Resolved [RESOLVED] Can I save the contents in a listbox using CommonDialog?

    I'm trying to figure out how to save the contents in a listbox:

    1
    2
    3
    4
    5

    using the CommonDialog so the user can create a filename and click Save.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    148

    Re: Can I save the contents in a listbox using CommonDialog?

    I figured it out

    VB Code:
    1. Private Sub Command2_Click()
    2. On Error Resume Next
    3. With CommonDialog1  'open a with statement as there is a lot of code
    4.      .DialogTitle = "Save"  'sets the dialog title
    5.      .InitDir = "C:\"  'sets the initial directory
    6.      .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"  'sets the file
    7.      'types
    8.  
    9.      .ShowSave  'show the dialog
    10. End With
    11.  
    12. Open CommonDialog1.FileName For Output As #1
    13.     For X = 0 To List2.ListCount
    14.         Print #1, List2.List(X)
    15.     Next
    16. Close #1
    17.  
    18. End Sub

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: [RESOLVED] Can I save the contents in a listbox using CommonDialog?

    The list indexes in the listbox start at 0 and go to .listcount-1. Just to keep in mind since you might notice that you have an extra empty line at the end of your file


    Has someone helped you? Then you can Rate their helpful post.

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED] Can I save the contents in a listbox using CommonDialog?

    ...also,
    1. Don't use hard-coded file number. Use FreeFile function.
    2. Trap when user presses Cancel. (Set, CommonDialog1.CancelError = True , and trap cdlCancel error in error handler.
    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     On Error Goto ErrChkr
    4.  
    5.     Dim F As Integer
    6.     Dim i As Integer
    7.  
    8.     With CommonDialog1
    9.         .FileName = ""
    10.         .DefaultExt = ".txt"
    11.         .Filter = "Text file|*.txt"
    12.         .Flags = cdlOFNOverwritePrompt 'Overwrite prompt
    13.         .CancelError = True
    14.         .ShowSave
    15.        
    16.         F = Freefile
    17.         Open .FileName For Output As #F
    18.    
    19.         For i = 0 To List1.ListCount - 1
    20.             'Save each listitem
    21.             Print #F, List1.List(i)
    22.         Next i
    23.    
    24.         Close #F
    25.  
    26.     End With
    27.  
    28.     Exit Sub
    29.  
    30. ErrChkr:
    31.  
    32.     If Err.Number = cdlCancel Then
    33.         Msgbox Err.Description
    34.         ' do whatever else you want to do if cancel was clicked
    35.     End If
    36.    
    37. End Sub
    Last edited by iPrank; Jan 15th, 2006 at 05:14 PM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: [RESOLVED] Can I save the contents in a listbox using CommonDialog?

    Quote Originally Posted by iPrank
    ...also,
    1. Don't use hard-coded file number. Use FreeFile function.
    2. Trap when user presses Cancel. (Set, CommonDialog1.CancelError = True , and trap cdlCancel error in error handler.
    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     On Error Goto ErrChkr
    4.  
    5.     Dim F As Integer
    6.     Dim i As Integer
    7.  
    8.     With CommonDialog1
    9.         .FileName = ""
    10.         .DefaultExt = ".txt"
    11.         .Filter = "Text file|*.txt"
    12.         .Flags = cdlOFNOverwritePrompt 'Overwrite prompt
    13.         .CancelError = True
    14.         .ShowSave
    15.        
    16.         F = Freefile
    17.         Open .FileName For Output As #F
    18.    
    19.         For i = 0 To List1.ListCount - 1
    20.             'Save each listitem
    21.             Write #F, List1.List(i)
    22.         Next i
    23.    
    24.         Close #F
    25.  
    26.     End With
    27.  
    28.     Exit Sub
    29.  
    30. ErrChkr:
    31.  
    32.     If Err.Number = cdlCancel Then
    33.         Msgbox Err.Description
    34.         ' do whatever else you want to do if cancel was clicked
    35.     End If
    36.    
    37. End Sub
    If memory serves me right, Write() adds quotation marks ("") around the data and Print() doesn't... Could be wrong though...


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED] Can I save the contents in a listbox using CommonDialog?

    Oops !
    You are right.
    Code edited.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width