Ringkasan
Saat mengotomatisasi produk Office dari Visual Basic, mungkin berguna untuk memindahkan bagian kode ke dalam modul Microsoft Visual Basic for Applications (VBA) yang bisa berjalan di dalam ruang proses server. Ini bisa meningkatkan kecepatan eksekusi secara keseluruhan untuk aplikasi Anda dan membantu mengurangi masalah jika server hanya melakukan tindakan saat panggilan dilakukan sedang berlangsung.
Artikel ini menunjukkan cara menambahkan modul VBA secara dinamis ke aplikasi Office yang berjalan dari Visual Basic, lalu memanggil makro untuk mengisi proses dalam lembar kerja.Informasi Selengkapnya
Contoh berikut menunjukkan cara menyisipkan modul kode ke dalam Microsoft Excel, tetapi Anda dapat menggunakan teknik yang sama untuk Word dan PowerPoint karena keduanya menggabungkan mesin VBA yang sama.
Sampel menggunakan file teks statis untuk modul kode yang disisipkan ke dalam Excel. Anda mungkin ingin mempertimbangkan untuk memindahkan kode ke dalam file sumber daya yang dapat dikompilasi ke dalam aplikasi, lalu mengekstrak ke dalam file sementara ketika dibutuhkan saat run time. Hal ini akan membuat proyek lebih mudah dikelola untuk distribusi ulang. Dimulai dengan Microsoft Office XP, pengguna harus memberikan akses ke model objek VBA sebelum kode Otomatisasi apa pun yang ditulis untuk memanipulasi VBA akan berfungsi. Ini adalah fitur keamanan baru dengan Office XP. Untuk informasi selengkapnya, silakan lihat artikel Basis pengetahuan berikut:282830 Akses Programatik untuk Office pengaturan VBA XP Project Ditolak
Langkah-langkah untuk membuat sampel
-
Pertama, buat file teks baru bernama KbTest.bas (tanpa ekstensi .txt). Ini adalah modul kode yang akan kami sisipkan Excel saat run-time.
-
Dalam file teks, tambahkan baris kode berikut:
Attribute VB_Name = "KbTest"
' Your Microsoft Visual Basic for Applications macro function takes 1 ' parameter, the sheet object that you are going to fill. Public Sub DoKbTest(oSheetToFill As Object) Dim i As Integer, j As Integer Dim sMsg As String For i = 1 To 100 For j = 1 To 10 sMsg = "Cell(" & Str(i) & "," & Str(j) & ")" oSheetToFill.Cells(i, j).Value = sMsg Next j Next i End Sub -
Simpan file teks ke direktori C:\KbTest.bas, lalu tutup file.
-
Mulai Visual Basic dan buat proyek standar. Formulir1 dibuat secara default.
-
Pada menu Project, klikReferensi, lalu pilih versi pustaka tipe yang sesuai, yang memungkinkan Anda untuk menggunakan pengikatan awal untuk Excel.
Misalnya, pilih salah satu hal berikut ini:-
Misalnya Microsoft Office Excel 2007, pilih pustaka 12.0.
-
Misalnya Microsoft Office Excel 2003, pilih pustaka 11.0.
-
Misalnya Microsoft Excel 2002, pilih pustaka 10.0.
-
Misalnya Microsoft Excel 2000, pilih pustaka 9.0.
-
Misalnya Microsoft Excel 97, pilih pustaka 8.0.
-
-
Tambahkan tombol ke Form1, lalu letakkan kode berikut dalam penanganan untuk kejadian Klik pada tombol tersebut:
Private Sub Command1_Click()
Dim oXL As Excel.Application Dim oBook As Excel.Workbook Dim oSheet As Excel.Worksheet Dim i As Integer, j As Integer Dim sMsg As String ' Create a new instance of Excel and make it visible. Set oXL = CreateObject("Excel.Application") oXL.Visible = True ' Add a new workbook and set a reference to Sheet1. Set oBook = oXL.Workbooks.Add Set oSheet = oBook.Sheets(1) ' Demo standard Automation from out-of-process, ' this routine simply fills in values of cells. sMsg = "Fill the sheet from out-of-process" MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground For i = 1 To 100 For j = 1 To 10 sMsg = "Cell(" & Str(i) & "," & Str(j) & ")" oSheet.Cells(i, j).Value = sMsg Next j Next i ' You're done with the first test, now switch sheets ' and run the same routine via an inserted Microsoft Visual Basic ' for Applications macro. MsgBox "Done.", vbMsgBoxSetForeground Set oSheet = oBook.Sheets.Add oSheet.Activate sMsg = "Fill the sheet from in-process" MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground ' The Import method lets you add modules to VBA at ' run time. Change the file path to match the location ' of the text file you created in step 3. oXL.VBE.ActiveVBProject.VBComponents.Import "C:\KbTest.bas" ' Now run the macro, passing oSheet as the first parameter oXL.Run "DoKbTest", oSheet ' You're done with the second test MsgBox "Done.", vbMsgBoxSetForeground ' Turn instance of Excel over to end user and release ' any outstanding object references. oXL.UserControl = True Set oSheet = Nothing Set oBook = Nothing Set oXL = Nothing End Sub -
Misalnya Excel 2002 dan untuk versi Excel yang lebih baru, Anda harus mengaktifkan akses proyek VBA. Untuk melakukannya, gunakan salah satu metode berikut:
-
Di Excel 2007, klik Microsoft Office Pencarian, lalu klik Excel Options. Klik Pusat Kepercayaan, lalu klik Pusat kepercayaan Pengaturan. Klik Perintah Pengaturan, klik untuk memilih kotak centang Percayai Akses ke model objek proyek VBA, lalu klik OK dua kali.
-
Di Excel 2003 dan versi Excel yang lebih lama, arahkan ke Makro pada menu Alat, lalu klik Keamanan. Dalam kotak dialog Keamanan, klik tab Sumber Tepercaya, lalu klik untuk memilih kotak centang Percayai akses Visual Basic Project pencarian.
-
-
Menjalankan proyek Visual Basic.
Referensi
Untuk informasi selengkapnya tentang Otomatisasi Office dari Visual Basic Proyek, silakan lihat situs Office Dukungan Pengembangan Situs di alamat berikut ini: