vb.net RTF textbox to save formatting for word

I have a textbox that is RTF and I have some formatting that I would like to save.

example: H<subscript>2</subscript>0

Problem is is there maybe more then 1 sub or superscript in my rtf textbox

I want to be able to save the text from the rtf textbox to notepad as:
H{%format sub%}2{%endformat%}0

µmol × minute{%format super%}-1{%endformat%} × mg{%format super%}-5{%endformat%}
taviafAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Get the control text in a String and use the replace function of the string to do the job for you:

		Dim text As String = RichTextBox1.Text
		text = text.Replace("<subscript>", "{%format sub%}")
		text = text.Replace("</subscript>", "{%endformat%}")

Open in new window

Save the text in a text file and it will be ready for Notepad
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
taviafAuthor Commented:
I might not have been clear about my question. The H2O was another example....
the rtf = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}" & vbCrLf & "\viewkind4\uc1\pard\b\f0\fs20 U/mg \up7 2\up0\par" & vbCrLf & "}" & vbCrLf & ""

and

I want the actual text  or the position of where I can put: U/mg{%format sub%}7{%endformat%}

I want to strip the rtf stuff and still have a formatted string...
0
Jacques Bourgeois (James Burger)PresidentCommented:
First of all sorry, I made a mistake in my initial post. You should read Rtf property in Text if you want to "play" with the codes.

Maybe it's me, but I not, I do not think you were clear. And this time either.

You say that you want "the actual text  or the position". Which one?

How can you "still have a formatted string" if you remove the tags? Wasn't the deal to send the text to Notepad? You can't have formatted text in Notepad.

What are these {%format sub%} and {%endformat%}, but placeholders of some kind. I thought that you wanted to replace the rtf tags by these.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

taviafAuthor Commented:
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myFont As String = activityUnit.Font.Name

        Dim thisString As String = rtftotext(activityUnit.Rtf)

        If activityUnit.Rtf.Contains(vbCrLf) Then
            Dim removeLineFeed = activityUnit.Rtf.Replace(vbCrLf, "")
            Dim rtfHeader As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 " & myFont & ";}}\viewkind4\uc1\pard\b\f0\fs20"
            Dim rtfHeader2 As String = "{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 " & myFont & ";}}\viewkind4\uc1\pard\lang1033\b\f0\fs20"

            If removeLineFeed.Contains(rtfHeader) Or removeLineFeed.Contains(rtfHeader2) Then
                Dim formattedText = removeLineFeed.Replace(rtfHeader, "")
                formattedText = formattedText.Substring(0, formattedText.Length - 1) 'removes last bracket
                Dim removeHangIndent = formattedText.Replace("\par", "")
            End If
        End If

    End Sub

Open in new window


My final string is = "U/mg\dn7 7\up0"
then I can save it as: U/mg {%format sub%} 7 {%endformat%}

Now... How can I read U/mg {%format sub%} 7 {%endformat%} and convert it back to RTF?!?!?!
0
taviafAuthor Commented:
I tore this string apart. Is there a more simple way to do this? My text file needs the format of this example U/mg {%format sub%} 7 {%endformat%} but I need the formatting of the RTB. However, I need to be able to read this U/mg {%format sub%} 7 {%endformat%} back but get the RTB text markup....
0
Jacques Bourgeois (James Burger)PresidentCommented:
I still have problem understanding what you are trying to achieve, and for what purpose (which could explain a lot of things).

Why not keep a copy of the rtf in one file (RichTextBox.SaveFile) and the text in another?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.