Solved

Indent HTML Code

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 47
Access Object Property from VBA Module in Excel 2010 2 27
Macro Excel - Multiple If conditions 2 64
Excel Automation VBA 19 37
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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now