|
-
Jun 22nd, 2000, 12:05 AM
#1
Thread Starter
Hyperactive Member
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.
-
Jun 22nd, 2000, 12:10 AM
#2
Fanatic Member
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
-
Jun 22nd, 2000, 12:14 AM
#3
Thread Starter
Hyperactive Member
How would I say "HTM, HTML, ASP|*.htm, *.html,*.asp"
If you think education is expensive, try ignorance.
-
Jun 22nd, 2000, 12:23 AM
#4
Thread Starter
Hyperactive Member
I guessed and got it right, thanks anyway.
If you think education is expensive, try ignorance.
-
Jun 22nd, 2000, 12:44 AM
#5
Thread Starter
Hyperactive Member
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.
-
Jun 22nd, 2000, 12:50 AM
#6
Fanatic Member
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
-
Jun 22nd, 2000, 12:54 AM
#7
Hyperactive Member
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)
-
Jun 22nd, 2000, 01:29 AM
#8
Thread Starter
Hyperactive Member
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.
-
Jun 22nd, 2000, 05:35 AM
#9
Hyperactive Member
must u ....
must u put the quotations ...
CommonDialog1.Filter = "HTM, HTML, ASP|*.htm; *.html; *.asp"
if ur putting them in the property box?
-
Jun 22nd, 2000, 05:51 AM
#10
Hyperactive Member
<<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).
-
Jun 22nd, 2000, 11:07 AM
#11
Member
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
-
Jun 22nd, 2000, 09:25 PM
#12
Thread Starter
Hyperactive Member
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.
-
Jun 23rd, 2000, 04:00 AM
#13
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.
-
Jun 23rd, 2000, 04:13 AM
#14
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|