Solved

Indent HTML Code

Posted on 1998-11-03
1
848 Views
Last Modified: 2006-11-17
Greetings Fellow VB Programmers,
I would like to know to Indent Code ( HTML ).
To turn HTML code like this...
<HTML><HEAD><TITLE></TITLE></HEAD></HTML>
to this...
<HTML>
    <HEAD>
    <TITLE></TITLE>
    </HEAD>
</HTML>
Just a basic Function would be fine.
Just show me how and I will insert the other HTML elements
myself.

Note, that your answer MUST me a function in which I
pass the RichTextBox.Text. Also,
please be kind enough to include comments on how
to add new HTML elements and the tab setting.


Thank you.
0
Comment
Question by:vienna
1 Comment
 
LVL 18

Accepted Solution

by:
deighton earned 200 total points
ID: 1443038
Here is a code indent function I've written for you.

'example call
Private Sub Command1_Click()

    richtextbox2.Text = indent(RichTextBox1.Text)
   
End Sub


Function indent(ByVal sCode As String) As String

    Dim sCommand As String
   
    Dim iPos As Long
    Dim iLen As Long
    Dim iIndent As Long
    Dim bLine As Boolean
   
   
    iLen = Len(sCode)
    iPos = 1
   
    While iPos <= iLen
   
       
       
        If Mid(sCode, iPos, 1) = "<" Then
       
            sCommand = Mid(sCode, iPos, 1)
            iPos = iPos + 1
           
            While Mid(sCode, iPos, 1) <> ">" And iPos <= iLen
           
                sCommand = sCommand & Mid(sCode, iPos, 1)
                iPos = iPos + 1
               
            Wend
            sCommand = UCase(sCommand) & Mid(sCode, iPos, 1)
           
'USE THE OR COMMAND TO EXPAND YOUR LISTS
            If sCommand = "</HTML>" Then 'EXPAND YOUR LIST OF COMMANDS TO CLOSE AN INDENT HERE
           
                iIndent = iIndent - 3  'TAB SPACE = 3 per INDENT HERE
               
            End If
           
            If bLine Then
               
                indent = indent + vbCrLf
               
            End If
           
            indent = indent & String(iIndent, " ") & sCommand
            bLine = True
           
            If sCommand = "<HTML>" Then 'EXPAND YOUR LIST OF COMMANDS TO OPEN AN INDENT HERE
           
                iIndent = iIndent + 3
               
            End If
           
            If iIndent < 0 Then iIndent = 0
           
        Else
       
            indent = indent & Mid(sCode, iPos, 1)
            bLine = True
         
        End If
       
        iPos = iPos + 1
       
    Wend
           
End Function

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question