Visual basic 6.0 - richtextbox

richtextbox - removing rtf tags from a saved file

Page 1 of 1

9 Replies - 21910 Views - Last Post: 22 April 2011 - 08:59 PM Rate Topic: -----

#1 Kal1963  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 07

Visual basic 6.0 - richtextbox

Post icon  Posted 04 October 2007 - 06:54 AM

I write an output to a richtextbox and I save the contents of the richtextbox to a file using a command dialog. When the command dialog prompted me to enter the name and the type of the file to save, I select the type of the file to be “*.txt”. The command dialog prints the contents of the richtextbox to a file with rtf tags. I need the rtf tags to be removed. However, I do not see this problem when I select the type of file as “*.doc”. Could anyone help me? Thanks.

Here is the code I have:

Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel
	
	CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
	CommonDialog1.ShowSave
	
	' default is a word document.
	If CommonDialog1.FilterIndex = 2 Then
	   'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
	   CommonDialog1.FileName = CommonDialog1.FileName
	Else
		'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
		CommonDialog1.FileName = CommonDialog1.FileName
  
	End If
	
   ' Note: Dout1frm = form name, rtb1 = name of richtextbox that 
   ' has the data that I need to save to a file.

	Dout1frm.rtb1.SaveFile (CommonDialog1.FileName)
  
	Exit Sub
	
UserPressedCancel:
	 Exit Sub
End Sub



*Always use code tags :)

This post has been edited by PsychoCoder: 04 October 2007 - 08:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Visual basic 6.0 - richtextbox

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Visual basic 6.0 - richtextbox

Posted 04 October 2007 - 08:14 AM

To save the contents without the RTF tags, use the SaveFile Method of the RichTextBox. That gives you the nopportunity to add a 2nd paramtere, where you can spefify saving as RTF, which includes the RTF Tags, or os plain text, which is the contents without the RTF tags, change your code to try this:

Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel
	
	CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
	CommonDialog1.ShowSave
	
	' default is a word document.
	If CommonDialog1.FilterIndex = 2 Then
	   'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
	   CommonDialog1.FileName = CommonDialog1.FileName
	Else
		'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
		CommonDialog1.FileName = CommonDialog1.FileName
  
	End If
	
   ' Note: Dout1frm = form name, rtb1 = name of richtextbox that
   ' has the data that I need to save to a file.

	richTextBox1.SaveFile(CommonDialog1.FileName, RichTextBoxStreamType.PlainText)
  
	Exit Sub
	
UserPressedCancel:
	 Exit Sub
End Sub



Notice that I:
  • Used the SaveFile Method of the RichTextBox, not the CommonDialog
  • Added RichTextBoxStreamType.PlainText as the 2nd parameter, you could also use RichTextBoxStreamType.RichText to save as RichText, which has the RTF tags

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 Kal1963  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 07

Re: Visual basic 6.0 - richtextbox

Posted 04 October 2007 - 09:35 AM

Thank you very much for your quick response. I tried the following and it gave me a compilation error:


Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel

CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
CommonDialog1.ShowSave


'NOTE: THIS LINE GAVE ME A COMPILCATION ERROR. NOTICE HTAT RTB1 IS THE RICHTEXTBOX AND DOUT1FRM IS THE FORM THAT HAS THE RICHTEXTBOX

Dout1frm.rtb1.SaveFile (CommonDialog1.FileName, RichTextBoxStreamType.PlainText)

Exit Sub

UserPressedCancel:
Exit Sub
End Sub

DO YOU WHAT IS THE PROBLEM?

Thank.
Kal.



View PostPsychoCoder, on 4 Oct, 2007 - 08:14 AM, said:

To save the contents without the RTF tags, use the SaveFile Method of the RichTextBox. That gives you the nopportunity to add a 2nd paramtere, where you can spefify saving as RTF, which includes the RTF Tags, or os plain text, which is the contents without the RTF tags, change your code to try this:

Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel
	
	CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
	CommonDialog1.ShowSave
	
	' default is a word document.
	If CommonDialog1.FilterIndex = 2 Then
	   'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
	   CommonDialog1.FileName = CommonDialog1.FileName
	Else
		'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
		CommonDialog1.FileName = CommonDialog1.FileName
  
	End If
	
   ' Note: Dout1frm = form name, rtb1 = name of richtextbox that
   ' has the data that I need to save to a file.

	richTextBox1.SaveFile(CommonDialog1.FileName, RichTextBoxStreamType.PlainText)
  
	Exit Sub
	
UserPressedCancel:
	 Exit Sub
End Sub



Notice that I:
  • Used the SaveFile Method of the RichTextBox, not the CommonDialog
  • Added RichTextBoxStreamType.PlainText as the 2nd parameter, you could also use RichTextBoxStreamType.RichText to save as RichText, which has the RTF tags
Hope this helps :)

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Visual basic 6.0 - richtextbox

Posted 04 October 2007 - 11:36 AM

You need to tell me what the error says, not just its a compilation error
Was This Post Helpful? 0
  • +
  • -

#5 Kal1963  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 07

Re: Visual basic 6.0 - richtextbox

Posted 05 October 2007 - 10:06 AM

Thanks again for your hlep; I needed it. I get a mesg box with this error: "Compiler Error: Expected :=". There is not an error number. Keep in mind that I am using Visual basic 6.

Regards,
Kal1963


View PostKal1963, on 4 Oct, 2007 - 09:35 AM, said:

Thank you very much for your quick response. I tried the following and it gave me a compilation error:


Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel

CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
CommonDialog1.ShowSave


'NOTE: THIS LINE GAVE ME A COMPILCATION ERROR. NOTICE HTAT RTB1 IS THE RICHTEXTBOX AND DOUT1FRM IS THE FORM THAT HAS THE RICHTEXTBOX

Dout1frm.rtb1.SaveFile (CommonDialog1.FileName, RichTextBoxStreamType.PlainText)

Exit Sub

UserPressedCancel:
Exit Sub
End Sub

DO YOU WHAT IS THE PROBLEM?

Thank.
Kal.



View PostPsychoCoder, on 4 Oct, 2007 - 08:14 AM, said:

To save the contents without the RTF tags, use the SaveFile Method of the RichTextBox. That gives you the nopportunity to add a 2nd paramtere, where you can spefify saving as RTF, which includes the RTF Tags, or os plain text, which is the contents without the RTF tags, change your code to try this:

Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel
	
	CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
	CommonDialog1.ShowSave
	
	' default is a word document.
	If CommonDialog1.FilterIndex = 2 Then
	   'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
	   CommonDialog1.FileName = CommonDialog1.FileName
	Else
		'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
		CommonDialog1.FileName = CommonDialog1.FileName
  
	End If
	
   ' Note: Dout1frm = form name, rtb1 = name of richtextbox that
   ' has the data that I need to save to a file.

	richTextBox1.SaveFile(CommonDialog1.FileName, RichTextBoxStreamType.PlainText)
  
	Exit Sub
	
UserPressedCancel:
	 Exit Sub
End Sub



Notice that I:
  • Used the SaveFile Method of the RichTextBox, not the CommonDialog
  • Added RichTextBoxStreamType.PlainText as the 2nd parameter, you could also use RichTextBoxStreamType.RichText to save as RichText, which has the RTF tags
Hope this helps :)

Was This Post Helpful? 0
  • +
  • -

#6 Kal1963  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 07

Re: Visual basic 6.0 - richtextbox

Posted 08 October 2007 - 08:01 AM

Thanks again for your hlep; I needed it. I get a mesg box with this error: "Compiler Error: Expected :=". There is not an error number. Keep in mind that I am using Visual basic 6.

Regards,
Kal1963
Was This Post Helpful? 0
  • +
  • -

#7 unicorn_33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-March 10

Re: Visual basic 6.0 - richtextbox

Posted 13 March 2010 - 03:22 AM

View PostKal1963, on 04 October 2007 - 08:35 AM, said:

Thank you very much for your quick response. I tried the following and it gave me a compilation error:


Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel

CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
CommonDialog1.ShowSave


'NOTE: THIS LINE GAVE ME A COMPILCATION ERROR. NOTICE HTAT RTB1 IS THE RICHTEXTBOX AND DOUT1FRM IS THE FORM THAT HAS THE RICHTEXTBOX

Dout1frm.rtb1.SaveFile (CommonDialog1.FileName, RichTextBoxStreamType.PlainText)

Exit Sub

UserPressedCancel:
Exit Sub
End Sub

DO YOU WHAT IS THE PROBLEM?

Thank.
Kal.



View PostPsychoCoder, on 4 Oct, 2007 - 08:14 AM, said:

To save the contents without the RTF tags, use the SaveFile Method of the RichTextBox. That gives you the nopportunity to add a 2nd paramtere, where you can spefify saving as RTF, which includes the RTF Tags, or os plain text, which is the contents without the RTF tags, change your code to try this:

Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel
	
	CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
	CommonDialog1.ShowSave
	
	' default is a word document.
	If CommonDialog1.FilterIndex = 2 Then
	   'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
	   CommonDialog1.FileName = CommonDialog1.FileName
	Else
		'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
		CommonDialog1.FileName = CommonDialog1.FileName
  
	End If
	
   ' Note: Dout1frm = form name, rtb1 = name of richtextbox that
   ' has the data that I need to save to a file.

	richTextBox1.SaveFile(CommonDialog1.FileName, RichTextBoxStreamType.PlainText)
  
	Exit Sub
	
UserPressedCancel:
	 Exit Sub
End Sub



Notice that I:
  • Used the SaveFile Method of the RichTextBox, not the CommonDialog
  • Added RichTextBoxStreamType.PlainText as the 2nd parameter, you could also use RichTextBoxStreamType.RichText to save as RichText, which has the RTF tags
Hope this helps :)


TRY PUTTING SOMETHING LIKE THIS INTO THE CODE:

On Error GoTo UserPressedCancel
'Dim rtfRTF As Integer '(Default) RTF. The RichTextBox control saves its contents as an .rtf file.
'Dim rtfText As Boolean 'Text. The RichTextBox control saves its contents as a text file.
'rtfRTF = 0
'rtfText = True
cdlCommon.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
cdlCommon.ShowSave

' default is a word document.
If cdlCommon.FilterIndex = 2 Then
'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
a$ = cdlCommon.FileName
If InStr(1, ".txt", a$) Then

Else

cdlCommon.FileName = cdlCommon.FileName & ".txt"
End If
Else
'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
cdlCommon.FileName = cdlCommon.FileName & ".doc"

End If

' Note: Dout1frm = form name, rtb1 = name of richtextbox that
' has the data that I need to save to a file.

'i = RichTextBox1.SaveFile(cdlCommon.FileName, RichTextBoxStreamType.PlainText)
RichTextBox1.SaveFile cdlCommon.FileName, rtfText 'rtfText or rtf
Exit Sub

UserPressedCancel:
Exit Sub
Was This Post Helpful? 0
  • +
  • -

#8 Guest_premji*


Reputation:

Re: Visual basic 6.0 - richtextbox

Posted 04 April 2010 - 10:11 PM

 Private Sub mnuSave_Click()
Dim filepath$
cmndlgNote.ShowSave
filepath = cmndlgNote.FileName
rtbNotepad.SaveFile (filepath)
End Sub



from the above it compile with no errors...when i run the code and entered the input as "HelloWorld" and saved the filename as "pro1.txt"...when i opened the above filename "pro1.txt" it shows

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Hello World
\par }

note the above problem and find the bugs regarding above code
Was This Post Helpful? 0

#9 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Visual basic 6.0 - richtextbox

Posted 04 April 2010 - 10:46 PM

You need to specify the optional parameter. Presently it is saving the file as a rtf file. So, highlight savefile and press F1 to read about the options...



Good Luck
Was This Post Helpful? 0
  • +
  • -

#10 rootabegax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-April 11

Re: Visual basic 6.0 - richtextbox

Posted 22 April 2011 - 08:59 PM

Solved it--you need to "call" the richtextbox's function--weird huh?

Call RichTextBox1.SaveFile(CommonDialog1.FileName, rtfText)



You can also specify "rtfRTF" for rich text format.

That will save the contents of the richtextbox as a plain text file, and not give you that annoying error with the "Compile error: Expected: =" error.

View PostKal1963, on 04 October 2007 - 06:54 AM, said:

I write an output to a richtextbox and I save the contents of the richtextbox to a file using a command dialog. When the command dialog prompted me to enter the name and the type of the file to save, I select the type of the file to be “*.txt”. The command dialog prints the contents of the richtextbox to a file with rtf tags. I need the rtf tags to be removed. However, I do not see this problem when I select the type of file as “*.doc”. Could anyone help me? Thanks.

Here is the code I have:

Private Sub mnuFileSave_Click()
CommonDialog1.CancelError = True
On Error GoTo UserPressedCancel
	
	CommonDialog1.Filter = "Word Document | *.doc| text files (*.txt) |*.txt| all Files (*.*) |*.*"
	CommonDialog1.ShowSave
	
	' default is a word document.
	If CommonDialog1.FilterIndex = 2 Then
	   'CommonDialog1.FileName = CommonDialog1.FileName & ".txt"
	   CommonDialog1.FileName = CommonDialog1.FileName
	Else
		'CommonDialog1.FileName = CommonDialog1.FileName & ".doc"
		CommonDialog1.FileName = CommonDialog1.FileName
  
	End If
	
   ' Note: Dout1frm = form name, rtb1 = name of richtextbox that 
   ' has the data that I need to save to a file.

	Dout1frm.rtb1.SaveFile (CommonDialog1.FileName)
  
	Exit Sub
	
UserPressedCancel:
	 Exit Sub
End Sub



*Always use code tags :)

This post has been edited by rootabegax: 22 April 2011 - 09:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1