Results 1 to 14 of 14

Thread: Open, Save, Save As, etc

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    I am looking at a snipet of another program as a reference to make my program open files, save files, etc.

    I have a Common Dialog Box on the form. How would I make .htm, .html, and .asp files visible in Open, Save As, etc?
    If you think education is expensive, try ignorance.

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    CommonDialog1.Filter = "HTM Files|*.HTM |HTML Files |*.HTML |ASP Files|*.ASP"

    Rule of thumb
    Caption Name follows with a pipe sign then the filter(*.whatever file extention)
    Chemically Formulated As:
    Dr. Nitro

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    How would I say "HTM, HTML, ASP|*.htm, *.html,*.asp"
    If you think education is expensive, try ignorance.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    I guessed and got it right, thanks anyway.
    If you think education is expensive, try ignorance.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    I just tried to save something and it looks like it's doing it, but there is no file being made. Can someone help me with the code needed for that please?

    Thank you.
    If you think education is expensive, try ignorance.

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    Smile

    if you are doing the following, it is incorrect!

    CommonDialog1.Filter = "HTM, HTML, ASP|*.htm, *.html,*.asp"


    You must have a semicolon instead of a comma.

    CommonDialog1.Filter = "HTM, HTML, ASP|*.htm; *.html; *.asp"


    Cool!
    Chemically Formulated As:
    Dr. Nitro

  7. #7
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    The CommonDialog Box doesn't actually save anything. It just retrieves the filename. To do the saving, you have to provide the actual code for that. This is a snippet of code that I used for a RichTextBox that I wanted to save as a plain text File. Of course I could avoid using the FileSystemObject if I didn't mind keeping it in RichTextFormat by using:
    RichTextBox1.SaveFile(sFile)
    But where is the fun of that.

    Code:

    Here is an example of a text file.

    Private sub mnuFileSave_Click()
    'Uses DocName. A global variable defined when the form
    'opens and redefined if a project is opened.
    'CD1 is the CommonDialogBox

    Dim sFile As String
    Dim fso, sTemp(3)
    Set fso = CreateObject("scripting.FileSystemObject")

    If DocName = "" Then
    CD1.FileName = "Untitled.vbs"
    Else
    If fso.FileExists(DocName) = True Then
    fso.CreateTextFile (DocName)
    Set sTemp(0) = fso.GetFile(DocName)
    Set sTemp(1) = sTemp(0).OpenAsTextStream(2, -2)
    sTemp(1).Write Code.Text
    sTemp(1).Close
    StatusBar1.Panels(1).Text = DocName & " was successfully saved."
    DocChanged = False
    Exit Sub
    Else
    CD1.FileName = DocName

    End If
    End If

    On Error Resume Next
    CD1.DefaultExt = ".txt"
    CD1.Filter = "HTM Files (*.htm)|*.htm|HTML Files (*.html)|*.html|ASP Files (*.asp)|*.asp|Text File (*.txt)|*.txt|All Files(*.*)|*"
    CD1.CancelError = True
    CD1.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
    CD1.ShowSave
    If Err <> 0 Then
    Exit Sub
    End If

    sFile = CD1.FileName


    fso.CreateTextFile (sFile)
    Set sTemp(0) = fso.GetFile(sFile)
    Set sTemp(1) = sTemp(0).OpenAsTextStream(2, -2)
    sTemp(1).Write Code.Text
    sTemp(1).Close
    DocChanged = False

    End sub

    Hope this helps.


    (By the way, I am using VB6 SP3)


  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Thank you very much. I copied it word for word onto my form and it gets a lot further. It now is making a file like it's supposed to, but the file is empty. I changed the default file to be Untitled.htm. What could I have done wrong?
    If you think education is expensive, try ignorance.

  9. #9
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    Question must u ....

    must u put the quotations ...

    CommonDialog1.Filter = "HTM, HTML, ASP|*.htm; *.html; *.asp"

    if ur putting them in the property box?

  10. #10
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    <<must u put the quotations ...

    <<CommonDialog1.Filter = "HTM, HTML, ASP|*.htm; *.html; <<*.asp"

    <<if ur putting them in the property box?


    No. If you are putting them into the property box, you would enter then without the quotes. (Like this:

    *.htm)|*.htm|(*.txt)|*.txt
    )
    You would only use the format above if you were going to define the properties in the code (which is my personal preference).




  11. #11

    Talking almost

    ok i used that code for saving in my word processor but it doesnt save anything its just an empty file with nothing
    i have the common dialog and everything but what do i need to name my richtextbox right now its called richtextbox1

    and how about some opening code to open the files after they are saved

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    Thanks to a lot of help from Reseet I have some code that might help you. Here is to save it.

    He quickly made a project to text, and the following code should work. All that you need is a Textbox named Text1 and a RichTextbox named RichTextBox1, the commondialog control named CD1 and then 3 commandButtons (Command1, ect.) Give this a try, and if it doesn't work, let me know.

    Private Sub Command1_Click()
    Dim sFile As String
    Dim fso, sTemp(3)
    Set fso = CreateObject("scripting.FileSystemObject")



    CD1.FileName = "Untitled.txt"
    On Error Resume Next
    CD1.DefaultExt = ".htm"
    CD1.Filter = "HTM Files (*.htm)|*.htm|HTML Files (*.html)|*.html|ASP Files (*.asp)|*.asp|Text File (*.txt)|*.txt|All Files(*.*)|*"
    CD1.CancelError = True
    CD1.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
    CD1.ShowSave
    If Err <> 0 Then
    Exit Sub
    End If

    sFile = CD1.FileName



    fso.CreateTextFile (sFile)
    Set sTemp(0) = fso.GetFile(sFile)
    Set sTemp(1) = sTemp(0).OpenAsTextStream(2, -2)
    sTemp(1).Write Text1.Text
    sTemp(1).Close
    End Sub


    Private Sub Command2_Click()
    Dim sFile As String
    Dim fso, sTemp(3)
    Set fso = CreateObject("scripting.FileSystemObject")



    CD1.FileName = "Untitled.txt"
    On Error Resume Next
    CD1.DefaultExt = ".htm"
    CD1.Filter = "HTM Files (*.htm)|*.htm|HTML Files (*.html)|*.html|ASP Files (*.asp)|*.asp|Text File (*.txt)|*.txt|All Files(*.*)|*"
    CD1.CancelError = True
    CD1.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
    CD1.ShowSave
    If Err <> 0 Then
    Exit Sub
    End If

    sFile = CD1.FileName


    fso.CreateTextFile (sFile)
    Set sTemp(0) = fso.GetFile(sFile)
    Set sTemp(1) = sTemp(0).OpenAsTextStream(2, -2)
    sTemp(1).Write RichTextBox1.Text
    sTemp(1).Close



    End Sub

    Private Sub Command3_Click()
    CD1.FileName = "Untitled.txt"
    On Error Resume Next
    CD1.DefaultExt = ".htm"
    CD1.Filter = "HTM Files (*.htm)|*.htm|HTML Files (*.html)|*.html|ASP Files (*.asp)|*.asp|Text File (*.txt)|*.txt|All Files(*.*)|*"
    CD1.CancelError = True
    CD1.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
    CD1.ShowSave
    If Err <> 0 Then
    Exit Sub
    End If

    sFile = CD1.FileName

    RichTextBox1.SaveFile (sFile)

    End Sub

    Private Sub Form_Load()
    Text1.Text = "This is a Test" & vbCrLf _
    & "String. The string will be" & _
    vbCrLf & "saved to location of your" _
    & vbCrLf & "chosing in plain text Format"
    RichTextBox1.Text = Text1.Text

    End Sub

    To open a file you would want to use this code:


    'CD1 is the CommonDialog Control

    Private sub Command4_Click
    Dim sFile as string
    Dim myVar as string
    Dim fso, sTemp(3)
    Set fso = CreateObject("scripting.filesystemObject")
    On Error Resume Next


    On Error Resume Next
    CD1.Filter = "HTM Files(*.htm)|*.htm|Text Files (*.txt)|*.txt|All Files(*.*)|*"
    CD1.CancelError = True
    CD1.Flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
    CD1.ShowOpen
    If Err <> 0 Then
    Exit Sub
    End If

    sFile = CD1.FileName
    Set sTemp(0) = fso.GetFile(sFile)


    'Now here is where you can have two separate methods.
    'If you are using a RichtextBox, just let the RichTextBox load the file using the
    'LoadFile Command

    RichTextBox1.LoadFile (sFile)


    'Otherwise, if you are loading the file into a textbox, use the following code:

    Set sTemp(1)=sTemp(0).OpenAsTextStream(1,-2)
    'myVar holds the file contents
    myVar=sTemp(1).ReadAll
    Text1.Text=myVar

    End sub

    Good Luck
    If you think education is expensive, try ignorance.

  13. #13
    Guest
    If you would like to make your look code like VB, enclose it in code brackets as follows.

    [ code ]

    some code here

    [ /code ]

    Except when you do it, you do not use the extra spaces. I only did it so you guys can see how to do it.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Posts
    323
    alirght, thanks
    If you think education is expensive, try ignorance.

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