Solved

Indent HTML Code

Posted on 1998-11-03
1
827 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now