Automate Excel via VB

Cara membuat file excel melalui VB 6.0:

Private Sub Command1_Click()
    On Error GoTo errH
    Dim oExcel As Object
    Dim oWB As Object
    Dim oWS As Object
    Dim oRng As Object

    Set oExcel = CreateObject("Excel.Application")
    
    Set oWB = oExcel.Workbooks.Add
    Set oWS = oWB.Worksheets("Sheet1")
    Set oRng = oWS.Range("A1")
    oRng.Value = "Hello World"

    'oWS.Cells(1, 1).Value
    oWB.SaveAs ("c:\Hello World.xls")
    oWB.Close

    GoTo Cleanup
errH:
    If Err.Number = 9 Then 'jika error pada pembukaan worksheet
        MsgBox "Tidak dapat membuka worksheet, mungkin nama worksheet salah", vbCritical, "Error"
    Else
        MsgBox Err.Description, vbCritical, "Error"
    End If
Cleanup:
    Set oWS = Nothing
    On Error Resume Next
    If Not oWB Is Nothing Then oWB.Close
    Set oWB = Nothing
    oExcel.Quit
    Set oExcel = Nothing
    On Error GoTo 0
End Sub

Iklan

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: