Konversi File Teks ke HTML

File teks merupakan file yang berisi karakter tanpa adaformat apapun. Sedangkan fi le HTML adalah tipe file standar yang ditampilkan pada sebuah halaman web. Jika file teks diperlakukan sebagai file HTML (dibuka pada sebuah web browser), maka pemenggalan paragraf file teks tersebut menjadi kacau.

Tip kali ini adalah mengonversi sebuah file teks menjadi fi le HTML. Teknik yang digunakan adalah membaca file teks masukan, lalu file teks tersebut diolah untuk ditambahkan format-format HTML sehingga dapat ditampilkan pada       web browser, tanpa merusak pemenggalan paragrafnya. Langkah-langkah untuk membuat aplikasi konversi ini adalah sebagai berikut:

  1. Buat sebuah command button form.dengan 4 buah textbox,
  2. Buat sebuah modul baru untuk membuka file. Simpan dengan namamodule1.bas:

Public Declare Function     GetOpenFileName Lib

“comdlg32.dll” Alias _

“GetOpenFileNameA” (pOpenfilename As

OPENFILENAME) As Long

Public Declare Function CommDlgExtendedError

Lib “comdlg32.dll” () As Long

Public FileOP As String

Public Const OFN_FILEMUSTEXIST = &H1000

Public Const OFN_HIDEREADONLY = &H4

Public OFN As OPENFILENAME

Public Type OPENFILENAME

lStructSize As Long: hwndOwner As Long

hInstance As Long: lpstrFilter As String

lpstrCustomFilter As String:

nMaxCustFilter As Long

nFilterIndex As Long: lpstrFile As String

nMaxFile As Long: lpstrFileTitle As String

nMaxFileTitle As Long: lpstrInitialDir

As String

lpstrTitle As String: flags As Long

nFileOffset As Integer: nFileExtension

As Integer

lpstrDefExt As String: lCustData As Long

lpfnHook As Long: lpTemplateName As String

End Type

Public Function fileExist(fileName As String)

As Boolean

Dim l As Long

On Error Resume Next

l = FileLen(fileName)

fileExist = Not (Err.Number > 0)

On Error GoTo 0

End Function

  1. Deklarasi yang diperlukan untuk aplikasi ini adalah:

Private Declare Function ShellExecute Lib

“shell32.dll” _

Alias “ShellExecuteA” (ByVal hWnd As Long,

ByVal lpOperation As String, _

ByVal lpFile As String, ByVal lpParameters

As String, _

ByVal lpDirectory As String, ByVal        nShowCmd

As Long) As Long

Dim num As Long, q$, lastN As Long, N As Long,

W$, msg$, Result, canExit As Boolean

  1. Subrutin serta event yang diperlukan adalah:

Private Sub Command1_Click()

On Error GoTo ex

off_off

Open Text2.Text For Input As #1

q = Input$(LOF(1), 1)

Close #1

If fileExist(Text3.Text) = True Then

msg = “File “ & Text3.

Text & “ sudah ada!” _& vbCrLf & “Replace file lama?”

Result = MsgBox(msg, 4 +

48, “Warning!”)

If Result = vbYes Then

GoTo continue Else

GoTo stp

End If

continue:

canExit = False

Label3.Caption =

“Proses…”

For i = 1 To Len(q)

If Mid$(q, i, 2) = vbCrLf

Then num = num + 1

Next

Command3.Enabled = True

lastN = 1

W = “<html>” & vbCrLf &

“<head>” & vbCrLf _

& “<meta http-

equiv=””Content-Type””

content=””text/html;

charset=windows-1252””>” _

& vbCrLf & “<title>” & Text4.Text & “</title>” _

& vbCrLf & “</head>” & vbCrLf & “<body

leftmargin=64 “ _

& “style=””border-left: medium solid

rgb(111,0,221); “ _

& “border-top: medium solid rgb(84,0,168)””>” _

& vbCrLf & “<br><font color=””navy””><H2>” _

& Text4.Text & “</H2></font>” _

& vbCrLf & “<p align=””justify””>” & Text1.

Text

Do

If canExit Then GoTo stp

DoEvents

N = InStr(lastN, q, vbCrLf)

If N Then

If N <> lastN Then W = W & Mid$(q, lastN,

N – lastN) & vbCrLf & “<br>” & Text1.Text

lastN = N + 2

Else

W = W & Right$(q, Len(q) – lastN + 1) &

“</p>” _

& vbCrLf & “<hr color=rgb(84,0,168)>” &

vbCrLf _

& “<br><br><br><br>” _

& vbCrLf & “</body>” &

vbCrLf & “</html>”

End If

num = num – 1

Caption = “0 – “ & num

Loop While N <> 0

Open Text3.Text For Output

As #1

Print #1, W

Close #1

canExit = True

stp:

Label3.Caption =

“Selesai…”

Caption = “txt2html”

num = 1

on_on

Beep

Exit Sub

ex:

If Err.Number = 53 Then

MsgBox “File tidak ditemukan!” & vbCrLf &

Text2.Text, vbExclamation, “Error”

Else

MsgBox Error$ & “ Error “ & Err,

vbExclamation, “Error”

End If

Close #1

on_on

End Sub

Private Sub Command2_Click()

Open_file

End Sub

Private Sub Form_Load()

ChDir App.Path

Text2.Text = App.Path & “\input.txt”

Text3.Text = App.Path & “\output.html”

num = 1: canExit = True

End Sub

Private Sub Open_file()

Dim cderr As Long

OFN.lStructSize = 76&

OFN.hwndOwner = Form1.hWnd

OFN.lpstrFilter = “Text documents (*.txt)” +

Chr(0) + “*.txt” + Chr(0) + Chr(0)

OFN.lpstrCustomFilter = String(256, Chr(0))

OFN.nMaxCustFilter = 256

OFN.lpstrFile = “” + String(512, Chr(0))

OFN.nMaxFile = 512

OFN.lpstrFileTitle = String(256, Chr(0))

OFN.nMaxFileTitle = 256

OFN.flags = OFN_FILEMUSTEXIST + OFN_

HIDEREADONLY

If GetOpenFileName(OFN) Then

Exdo = True

OFN.lpstrFile = Mid(OFN.lpstrFile, 1,

InStr(OFN.lpstrFile, Chr(0)) – 1)

Text2.Text = OFN.lpstrFile

Text3.Text = Left$(OFN.lpstrFile, Len(OFN.

lpstrFile) – 4) & “.html”

OFN.lpstrFileTitle = Mid(OFN.

lpstrFileTitle, 1, InStr(OFN.lpstrFileTitle,

Chr(0)) – 1)

InitialDir = Left(OFN.lpstrFile, Len(OFN.

lpstrFile) – Len(OFN.lpstrFileTitle))

Else

cderr = CommDlgExtendedError

End If

End Sub

Private Sub on_on()

Command1.Enabled = True:  Command2.Enabled =

True:  Command3.Enabled = False

Text1.Enabled = True:  Text2.Enabled = True

Text3.Enabled = True:  Text4.Enabled = True

q = vbNullString: W = vbNullString

End Sub

Private Sub off_off()

Command1.Enabled = False:  Command2.Enabled =

False

Text1.Enabled = False:  Text2.Enabled = False

Text3.Enabled = False:  Text4.Enabled = False

End Sub

  1. Simpan program ini, lalu jalankan.

~ oleh wahyututorial pada 8 April 2010.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

 
%d blogger menyukai ini: