Kamis, 04 Oktober 2012

automation berkaitan dengan fungsi aritmatika dan penggunaan reference (Word/Excel) di Visual Basic.net



Automation ini menghubungkan data - data yang diinput ketika program .vb dijalankan menuju .docx (Ms. Word) dan .xlsx (Ms. Excel) secara otomatis, sehingga pengguna akan dengan mudah menginput data tanpa perlu membuka aplikasi Ms. Word atau Ms. Excel.

1. Penjelasan

Aplikasi "Menghitung Berat Badan Ideal"
Dengan memasukkan data: Nama, Tinggi Badan dan Berat Badan Anda, aplikasi ini secara otomatis akan menghitung berapa berat badan ideal anda dan berapa berat badan yang harus anda kurangi atau anda tambahkan.
Kemudian dengan meng-klik tombol "Cetak ke Ms. Word" atau "Cetak ke Ms. Excel", aplikasi ini akan membuka file Ms. Word atau Ms. Excel (read-only) sehingga bisa di simpan bahkan di Print.

2. Screenshot
Aplikasi yang dijalankan
output ke Ms. Word
Output ke Ms. Excel



 3. Code

Imports word = Microsoft.Office.Interop.Word
Imports excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim beratideal As New Integer

        beratideal = 0.9 * (TextBox2.Text - 100)

        Label5.Text = " Berat badan ideal " & TextBox1.Text & " adalah " & beratideal & " kilogram "
        If TextBox3.Text > beratideal Then
            Label6.Text = TextBox1.Text & " harus MENGURANGI berat badan sebesar " & TextBox3.Text - beratideal & " kilogram "
        ElseIf TextBox3.Text < beratideal Then
            Label6.Text = TextBox1.Text & " harus MENAMBAH berat badan sebesar " & beratideal - TextBox3.Text & " kilogram "
        ElseIf TextBox3.Text = beratideal Then
            Label6.Text = TextBox1.Text & " sudah memiliki berat badan yang ideal "
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myWordApp As New word.Application
        Dim myWordDoc As New word.Document

        myWordDoc = myWordApp.Documents.Open("C:\Users\ACER\Documents\Visual Studio 2010\Projects\AnnisaMufidhaniPamungkas--1106095681\menghitung.docx")
        myWordDoc.Bookmarks("nama").Select()
        myWordApp.Selection.TypeText(TextBox1.Text)
        myWordDoc.Bookmarks("tinggi").Select()
        myWordApp.Selection.TypeText(TextBox2.Text)
        myWordDoc.Bookmarks("berat").Select()
        myWordApp.Selection.TypeText(TextBox3.Text)
        myWordDoc.Bookmarks("ideal").Select()
        myWordApp.Selection.TypeText(Label5.Text)
        myWordDoc.Bookmarks("harus").Select()
        myWordApp.Selection.TypeText(Label6.Text)
        myWordDoc.SaveAs("E:\menghitung_new.docx")
        myWordApp.Visible = True
        myWordDoc.Close()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim myExcelApp As New excel.Application
        Dim myExcelBook As New excel.Workbook
        Dim beratideal As New Integer

        beratideal = 0.9 * (TextBox2.Text - 100)

        myExcelApp.Visible = True
        myExcelBook = myExcelApp.Workbooks.Open("E:\menghitung.xlsx")
        myExcelApp.Range("B4").Value = TextBox1.Text
        myExcelApp.Range("B5").Value = TextBox2.Text
        myExcelApp.Range("B6").Value = TextBox3.Text
        myExcelApp.Range("B9").Value = beratideal
        myExcelBook.SaveAs("E:\menghitung_new.xlsx")
        myExcelApp.Quit()

    End Sub
End Class


Tidak ada komentar:

Posting Komentar