Artikel ini menggambarkan cara mengotomatisasi Microsoft Word dengan Microsoft Visual
Dasar.NET untuk mengambil dan memanipulasi dokumen properti. Meskipun sampel
dalam artikel ini secara khusus ditulis untuk mengotomatisasi kata, konsep-konsep yang sama dapat
diterapkan ke Microsoft Excel dan Microsoft PowerPoint.
Membuat klien otomasi untuk Microsoft Word
- Mulai Visual Studio.NET.
- Pada Berkas menu, klik Baru, lalu klik Project. Pilih Aplikasi Windows dari jenis proyek Visual Basic. Form1 dibuat oleh
default.
- Menambahkan referensi ke Perpustakaan objek Microsoft Word. Untuk melakukannya, ikuti langkah-langkah berikut:
- Pada Project menu, klik Menambahkan referensi.
- Pada COM tab, Cari Perpustakaan objek Microsoft Word, lalu klik Pilih.
Catatan Microsoft Office 2003 mencakup Interop utama Majelis (PIAs). Microsoft Office
XP tidak mencakup PIAs, tetapi mereka dapat di-download.Untuk informasi lebih lanjut tentang Office XP PIAs, klik nomor artikel di bawah ini untuk melihat artikel di dalam Basis Pengetahuan Microsoft:328912
(http://support.microsoft.com/kb/328912/
)
Microsoft Office XP utama interop Majelis (PIAs) tersedia untuk di-download
- Klik Oke dalam Menambahkan referensi kotak dialog untuk menerima pilihan Anda. Jika Anda diminta untuk
menghasilkan pembungkus untuk perpustakaan yang Anda pilih, klik Ya.
- Pada Lihat menu, pilih Toolbox untuk menampilkan toolbox, dan menambahkan tombol ke Form1.
- Klik dua kali Button1. Jendela kode untuk bentuk muncul.
- Dalam jendela kode, mengganti kode berikut
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
End Sub
dengan:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oBuiltInProps As Object
Dim oCustomProps As Object
Dim oProp As Object
Dim strValue As String
'Create an instance of Word and make it visible.
oWord = CreateObject("Word.Application")
oWord.Visible = True
'Create a new document
oDoc = oWord.Documents.Add()
'Get the Built-in Document Properties collection.
oBuiltInProps = oDoc.BuiltInDocumentProperties
'Get the value of the Author property and display it
strValue = oBuiltInProps.Item("Author").Value
MsgBox("The author of this document is " & strValue)
'Set the value of the Subject property.
oBuiltInProps.Item("Subject").Value = _
"Knowledge Base article Q303294"
'Get the Custom Document Properties collection.
oCustomProps = oDoc.CustomDocumentProperties
'Add a property named Knowledge Base Article
'and give it a value of Q303294.
oCustomProps.Add("Knowledge Base article", False, _
Office.MsoDocProperties.msoPropertyTypeString, "Q303294")
'Display a message box to give the user a chance to verify the
'properties.
MsgBox("Select Properties from the File menu " _
& "to view the changes." & Chr(10) _
& "Select the Summary tab to view " _
& "the Subject and the Custom tab to view the Custom " _
& "properties.", MsgBoxStyle.Information, _
"Check File Properties")
'Clean up. We'll leave Word running.
oCustomProps = Nothing
oBuiltInProps = Nothing
oDoc = Nothing
oWord = Nothing
End Sub
- Menambahkan kode berikut ke atas Form1.vb:
Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
- Tekan F5 untuk menjalankan aplikasi.
- Klik Button1 untuk memulai Microsoft Word.
Kode ini menunjukkan membaca dan menulis properti built-in dokumen
dan sifat-sifat kustom dokumen. Ketika menjalankan, kode ini menampilkan nilai fungsi built-in
Penulis properti, perubahan
Subjek properti nilai untuk "Artikel Basis Pengetahuan Q303294", dan menciptakan
properti kustom dokumen baru yang bernama "Artikel Basis Pengetahuan." Bila Anda adalah
diminta untuk melihat perubahan, beralih ke kata, dan klik
Properti pada
Berkas menu.
Untuk informasi lebih lanjut, kunjungi berikut Microsoft Developer
Jaringan Web site:
Untuk informasi selengkapnya, klik nomor artikel berikut untuk melihat artikel di Pangkalan Pengetahuan Microsoft:
303296
(http://support.microsoft.com/kb/303296/
)
Cara menggunakan otomatisasi untuk mendapatkan dan menetapkan kantor dokumen properti dengan Visual C#.NET
ID Artikel: 303294 - Kajian Terakhir: 24 September 2011 - Revisi: 2.0
Berlaku bagi:
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Excel 2002 Standard Edition
- Microsoft Office PowerPoint 2003
- Microsoft PowerPoint 2002 Standard Edition
- Microsoft Word 2002
| kbpia kbautomation kbhowto kbmt KB303294 KbMtid |
Penerjemahan MesinPENTING: Artikel ini diterjemahkan menggunakan perangkat lunak mesin penerjemah Microsoft dan bukan oleh seorang penerjemah. Microsoft menawarkan artikel yang diterjemahkan oleh seorang penerjemah maupun artikel yang diterjemahkan menggunakan mesin sehingga Anda akan memiliki akses ke seluruh artikel baru yang diterbitkan di Pangkalan Pengetahuan (Knowledge Base) dalam bahasa yang Anda gunakan. Namun, artikel yang diterjemahkan menggunakan mesin tidak selalu sempurna. Artikel tersebut mungkin memiliki kesalahan kosa kata, sintaksis, atau tata bahasa, hampir sama seperti orang asing yang berbicara dalam bahasa Anda. Microsoft tidak bertanggung jawab terhadap akurasi, kesalahan atau kerusakan yang disebabkan karena kesalahan penerjemahan konten atau penggunaannya oleh para pelanggan. Microsoft juga sering memperbarui perangkat lunak mesin penerjemah.
Klik disini untuk melihat versi Inggris dari artikel ini:
303294
(http://support.microsoft.com/kb/303294/en-us/
)