Rabu, 03 Oktober 2012

Automation

Pengertian Automation :proses mengendalikan satu program dari lain program atau alat pengembangan eksternal. Anda dapat mengotomatisasi setiap program yang berisi Visual Basic object model. Model objek adalah kumpulan hirarkis program objek yang tersedia atau terkena untuk Visual Basic.

berikut contoh prgram sederhana automation :
-buat form seperti ini
 pada menu project terdapat add reference :

-Pilih COM dari cari micosoft word atau excel masing-masing(sesuiakan)
Public Class Form1
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim total As New Integer

        If RadioButton1.Checked Then
            TextBox5.Text = 2000000
        Else
            TextBox5.Text = 3000000
        End If
        total = (Convert.ToInt32(TextBox2.Text) + Convert.ToInt32(TextBox3.Text) + Convert.ToInt32(TextBox5.Text))
        TextBox4.Text = total

    End Sub excel sesuai dengan milik masing-masing.

-jangan lupa siapkan word atau excel, untuk word lakukan bookmark
perintah bookmark terdapat pada menu insert, pilih tempat sesuai keinginan.
-untuk excel tidak perlu.
-pada button Lembar perhitungan masukan coding berikut :
    Dim A As New word.Application
        Dim B As New word.Document

        B = A.Documents.Open("d:\new folder\Perhitungan Gaji Pegawai.docx")
        B.Bookmarks("Nama").Select()
        A.Selection.TypeText(TextBox1.Text)
        B.Bookmarks("Golongan").Select()
        A.Selection.TypeText(TextBox5.Text)
        B.Bookmarks("Gaji").Select()
        A.Selection.TypeText(TextBox4.Text)
        B.SaveAs("d:\new folder\Perhitungan Gaji Pegawai_new.docx")
        A.Visible = True
        A.Quit()
    End Sub
-untuk button terakhir :
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myexcelapp As New excel.Application
        Dim myexcelbook As excel.Workbook
        myexcelapp.Visible = True
        myexcelbook = myexcelapp.Workbooks.Open("D:\A.xlsx")
        myexcelapp.Range("B3").Value = TextBox1.Text
        myexcelapp.Range("C3").Value = TextBox5.Text
        myexcelapp.Range("D3").Value = TextBox2.Text
        myexcelapp.Range("E3").Value = TextBox3.Text
        myexcelapp.Range("F3").Value = TextBox4.Text
        myexcelbook.SaveAs("D:\A_new.xlsx")
        myexcelapp.Quit()
    End Sub
-jika ingin jalankan program sebaiknya hitung dahulu.
-berikut contoh hasilnya :



Tidak ada komentar:

Posting Komentar