
save file as .txt or as .rtf in rich text box
Thanks, I'm using this line at the start, but my code doesn't seem to
work for me. It will change the extension to .rtf or .txt but if the
line is: rtbSelection.SaveFile CommonDialog1.fileName
then, even if I save it as .txt, the actual file is a rich text file.
I can say, for instance rtbSelection.SaveFile CommonDialog1.fileName, 1
but then the file is saved as txt whether I want it that way or not. For
whatever reason, I can't get the code to recognize the extension that's
been selected and then save it that way.
The code I'm using is as follows, after a lot of tries. This at least
does the following, which I need for my particular app: it saves a file
as an 8.3. What I want is for the code to "llok at" the .3 that's been
selected and then save the file either as .rtf or .txt depending on
what's been selected.
The code:
Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo ErrHandle
' the user starts with a template that is in .txt format. The format is
dictated by what the mainframe wants to see, and we have no control over
that; we just want it to be happy.
CommonDialog1.Filter = "Text Files(*.txt)|*.txt|Rich Text Files
(*.rtf)|*.rtf| All Files (*.*)|*.*|"
CommonDialog1.Flags = _
cdlOFNCreatePrompt + cdlOFNOverwritePrompt + _
cdlOFNPathMustExist + cdlOFNHideReadOnly
' this was done to assure that the file title was stripped to just
' file name. This was done because sometimes the file was saved as '
8.3.3, and this I didn't want
CommonDialog1.fileName = Right(CommonDialog1.fileName,
(Len(CommonDialog1.fileName) - (InStrRev(CommonDialog1.fileName, "\",
-1, 1))))
CommonDialog1.fileName = Left(CommonDialog1.fileName, _
(InStr(CommonDialog1.fileName, ".") - 1))
' this sets some directories in the registry that automate where things
are saved.
CommonDialog1.InitDir = GetSetting("eegrprt", "values", "DirSaveAs")
CommonDialog1.ShowSave
' this saves the file title, tacks on .txt and saves as a text file
rtbSelection.SaveFile CommonDialog1.FileTitle, 1
On Error GoTo 0
Exit Sub
ErrHandle:
If Not Err = cdlCancel Then
Resume Next
End If
End Sub
Any suggestions would be greatly appreciated.
Thanks
Ron Lesser
Quote:
> use your usual save dialog box code, but at the very beginning of the save
> code insert this line:
> commondialog1.filters="Rich Text File(*.rtf)|*.rtf| Text File(*txt)|*.txt"
> of course chang the name of the dialog box to whatever you named it, and it
> will then display only the two options in the box that displays file type.
> then it will save the file as whatever the user chooses.
> Clint Knapp
> > To all:
> > I am trying to create a common dialog control based save file function
> > that allows the file to be saved as either a text file or a rich text
> > file. I can do it as either one, but am mystified as to how to allow a
> > choice between the two. The code is below and I'd be greatful for
> > suggestions as to how to make this work
> > Thanks
> > Ron Lesser
> > Private Sub mnuFileSaveClick()
> > CommonDialog1.CancelError = True
> > On Error GoTo ErrHandle
> > CommonDialog1.Filter = "Text Files(*.txt)|*.txt|Rich Text Files
> > (*.rtf)|*.rtf| All Files (*.*)|*.*|"
> > CommonDialog1.Flags = _
> > cdlOFNCreatePrompt + cdlOFNOverwritePrompt + _
> > cdlOFNPathMustExist + cdlOFNHideReadOnly + cdlOFNLongNames
> > CommonDialog1.fileName = Right(CommonDialog1.fileName,
> > (Len(CommonDialog1.fileName) - (InStrRev(CommonDialog1.fileName, "\",
> > -1, 1))))
> > CommonDialog1.fileName = Left(CommonDialog1.fileName, _
> > (InStr(CommonDialog1.fileName, ".") - 1))
> > CommonDialog1.InitDir = GetSetting("eegrprt", "values", "DirSaveAs")
> > CommonDialog1.ShowSave
> > ' on the next line: , 1 saves as text file
> > ' as it is or, 0 saves as rtf
> > 'I'm trying to allow a choice between the two
> > rtbSelection.SaveFile CommonDialog1.FileTitle
> > On Error GoTo 0
> > Exit Sub
> > ErrHandle:
> > If Not Err = cdlCancel Then
> > Resume Next
> > End If
> > End Sub