Minggu, 16 Oktober 2016

Automation ( VB.net terhubung oleh Ms. Excel dan Ms.Word, )





Selamat Malam semua!

Kali ini saya akan share bagaimana cara membuat VB.Net yang terhubung  oleh Ms. Excel dan Ms.Word, kali ini form yang saya buat adalah Table nilai untuk ms. excel dan Form Nilai Mahasiswa untuk ms.word.
Dalam Form VB sendiri nanti nya harus tereferensi oleh Ms. excel&word itu sendiri, agar dapat saling terhubung. berikut adalah bagaimana saya membuat Progam ini









Public Class Form1




    Dim AppWord As New Microsoft.Office.Interop.Word.Application
    Dim DocWord As Microsoft.Office.Interop.Word.Document


    Dim excelApp As New Microsoft.Office.Interop.Excel.Application
    Dim MyExcelBook As Microsoft.Office.Interop.Excel.Workbook


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub




    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


      
        DocWord = AppWord.Documents.Open("C:\Users\Farah\Documents\farahpembis.docx")

        DocWord.Bookmarks("Nama").Select()
        AppWord.Selection.TypeText(TextBox1.Text)

        DocWord.Bookmarks("npm").Select()
        AppWord.Selection.TypeText(TextBox2.Text)
        DocWord.Bookmarks("ujian1").Select()
        AppWord.Selection.TypeText(TextBox3.Text)
        DocWord.Bookmarks("ujian2").Select()
        AppWord.Selection.TypeText(TextBox4.Text)
        DocWord.Bookmarks("ujian3").Select()
        AppWord.Selection.TypeText(TextBox5.Text)
        DocWord.Bookmarks("keterangan").Select()
        AppWord.Selection.TypeText(TextBox6.Text)
        AppWord.Selection.Font.Name = "Arial"
   


        'menghitung nilai akhir
        Dim hasil As New Integer
        hasil = ((Convert.ToInt32(TextBox3.Text)) + (Convert.ToInt32(TextBox4.Text)) + (Convert.ToInt32(TextBox5.Text))) / 3


        'memunculkan tulisan lulus/tidak lulus pada textbox keterangan
        If hasil >= 65 Then
            TextBox6.Text = "Lulus"
        Else
            TextBox6.Text = "Tidak Lulus"
        End If


        DocWord.SaveAs2("C:\Users\Farah\Documents\utsfrh1")

        AppWord.Visible = True
     
        AppWord.Quit()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        MyExcelBook = excelApp.Workbooks.Open("C:\Users\Farah\Documents\frhuts2.xlsx")

        excelApp.Range("C4").Value = TextBox3.Text
        excelApp.Range("C5").Value = TextBox4.Text()
        excelApp.Range("C6").Value = TextBox5.Text

        excelApp.Range("C8").Value = TextBox6.Text


        MyExcelBook.SaveAs("C:\Users\Farah\Documents\frhuts4.xlsx")



        'menghitung nilai akhir
        Dim hasil As New Integer
        hasil = ((Convert.ToInt32(TextBox3.Text)) + (Convert.ToInt32(TextBox4.Text)) + (Convert.ToInt32(TextBox5.Text))) / 3

        'memunculkan tulisan lulus/tidak lulus pada textbox keterangan
        If hasil >= 65 Then
            TextBox6.Text = "Lulus"
        Else
            TextBox6.Text = "Tidak Lulus"
        End If

        excelApp.Visible = True
     



    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        TextBox1.Text = " "
        TextBox2.Text = " "
        TextBox3.Text = " "
        TextBox4.Text = " "
        TextBox5.Text = " "
        TextBox6.Text = " "
      

        TextBox1.Focus()

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        End
    End Sub
End Class