PDA

View Full Version : Display HTML code in Form textbox



Etude
05-22-2008, 04:05 PM
Hello:

I just joined today while searching for an answer to my problem. I'm looking for a way to display an HTML string in a Form Textbox. The string is already in HTML format when I extract it from a Quality Center database. The problem is that it displays all the HTML tatgs in the textbox on the form. Ideally, I want to convert the HTML tags (actually, the text between the tags) to its formatted equivalent (ex. "<b>This</b>" to "This"). If that's not possible, I'd just like to rid the tags all-together. Does anyone have any suggestions?

Thanks.

Here's a sample of the string to display in a form textbox:


<html>
<body>
<font color="#000080"><b>MARY ANNE&lt;maryann&gt;, 5/15/2008:</b></font> &gt;&gt;&gt; YAN
5/14/2008 4:44 PM &gt;&gt;&gt;<br>
current query is<br>
select term_id, mtid, sum(tran_amt), count(*), SUM(check_nbr)<br>
Please call me.<br>
<br>
Mary<br>
<font color="#000080"><b>________________________________________</b></font><br>
<font color="#000080"><b>YAN&lt;yan&gt;, 5/15/2008:</b></font> fixed and staged in
clearcase.<br>
<font color="#000080"><b>________________________________________</b></font><br>
<font color="#000080"><b>DAVID&lt;david&gt;, 5/20/2008:</b></font> In QC 05/20/08<br>
<font color="#000080"><b>________________________________________</b></font><br>
</body>
</html>


Added line-breaks to code to prevent pagefrom side-scrolling
~Oorang

Oorang
06-09-2008, 12:48 PM
The options available to you depend on the version of Office you using?

Ken Puls
06-09-2008, 10:42 PM
Are you talking about a userform textbox?

I don't think you'll be able to have formatted text, as text boxes are pretty plain animals. Stripping the HTML can be done though...

When I copied and pasted your HTML, it showed up in cells A1:A5 in my worksheet, so I built this code around it. If you're receiving it from a function, you could pass it to this routine through a variable or some other manner.

Steps I took to make this happen:
-Set up a new userform called UserForm1 (the default)
-Added a button called cmdClose
-Added a button called cmdRetrieve
-Added a textbox called TextBox1 (the default)
-Added a reference to the Microsoft VBScript Regular Expressions x.x library (I used 5.5, but your version may be lower.)

Paste the following code into the userform, and give it a run...


'Set Reference to:
'Microsoft VBScript Regular Expressions 5.5

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub cmdRetrieve_Click()
Dim objRegex As Object
Dim sWorkOn As String
'Create a Regular Expressions object for later use
Set objRegex = New RegExp
objRegex.Global = True
'Read all the HTML into a text string
sWorkOn = Range("A1").Value & _
Range("A2").Value & _
Range("A3").Value & _
Range("A4").Value & _
Range("A5").Value
'Replace <br> with line feed
sWorkOn = Replace(sWorkOn, "<br>", vbNewLine)
'Strip all remaining tags
objRegex.Pattern = "[<]{1}[^<]{1,}[>]{1}"
sWorkOn = objRegex.Replace(sWorkOn, "")
'Strip all instances of _ characters
objRegex.Pattern = "[_]{1,}"
sWorkOn = objRegex.Replace(sWorkOn, "")
'Replace HTML characters with text characters
sWorkOn = Replace(sWorkOn, "&lt;", "<")
sWorkOn = Replace(sWorkOn, "&gt;", ">")
'Feed back the modified string to the text box
With Me.TextBox1
.MultiLine = True
.WordWrap = True
.Text = sWorkOn
End With
End Sub

File attached as well.