Wednesday, December 12, 2012

VB6 SMS Gateway: Contoh Mengekspor PhoneBook ke Excel

Private Sub ExportToExcel(PhoneBook As String)
Dim ExcelObj As New Excel.Application
Dim ExcelBook As Excel.Workbook
Dim ExcelSheet As Excel.Worksheet
Dim i As Integer

Set ExcelBook = ExcelObj.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1)

Dim s() As String
Dim r As String

s = Split(PhoneBook, vbCrLf & "+CPBR:")

With ExcelSheet
.Columns("A:A").ColumnWidth = 7
.Columns("B:B").ColumnWidth = 16
.Columns("C:C").ColumnWidth = 16
.Columns("D:D").ColumnWidth = 16
For i = 1 To UBound(s)
If s(i - 1) <> "" Then
r = Split(s(i - 1), ",")(0)
If InStr(1, r, "+CPBR:") > 0 Then
r = Split(Split(s(i - 1), ",")(0), ":")(1)
Else
r = Split(Split(s(i - 1), ",")(0), ":")(0)
End If
.Cells(i, 1) = r
.Cells(i, 2) = Split(s(i - 1), ",")(1)
.Cells(i, 3) = Split(s(i - 1), ",")(2)
.Cells(i, 4) = Split(s(i - 1), ",")(3)
End If
Next
End With

ExcelObj.Visible = True
End Sub