Bagaimana menggunakan otomatisasi untuk mendapatkan dan menetapkan properti dokumen Office dengan Visual C#.NET Artikel ini menunjukkan cara membuat Microsoft Visual C#.NET
Klien otomasi yang memanipulasi sifat dokumen Microsoft Word. Meskipun
contoh kode spesifik untuk kata, teknik yang sama dapat diterapkan ketika
mengotomatisasi 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 Visual C# proyek. Form1 dibuat secara 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 tambahan 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/EN-US/
)
INFO: Microsoft Office XP 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 kemudian menambahkan tombol ke Form1.
- Klik dua kali Button1. Jendela kode untuk bentuk muncul.
- Dalam jendela kode, mengganti kode berikut
private void button1_Click(object sender, System.EventArgs e)
{
}
dengan:
private void button1_Click(object sender, System.EventArgs e)
{
Word.Application oWord;
Word._Document oDoc;
object oMissing = Missing.Value;
object oDocBuiltInProps;
object oDocCustomProps;
//Create an instance of Microsoft Word and make it visible.
oWord = new Word.Application();
oWord.Visible = true;
//Create a new Document and get the BuiltInDocumentProperties collection.
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing,
ref oMissing);
oDocBuiltInProps = oDoc.BuiltInDocumentProperties;
Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
//Get the Author property and display it.
string strIndex = "Author";
string strValue;
object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocBuiltInProps,
new object[] {strIndex} );
Type typeDocAuthorProp = oDocAuthorProp.GetType();
strValue = typeDocAuthorProp.InvokeMember("Value",
BindingFlags.Default |
BindingFlags.GetProperty,
null,oDocAuthorProp,
new object[] {} ).ToString();
MessageBox.Show( "The Author is: " + strValue,"Author" );
//Set the Subject property.
strIndex = "Subject";
strValue = "The Subject";
typeDocAuthorProp.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.SetProperty,
null,oDocBuiltInProps,
new object[] {strIndex,strValue} );
//Add a property/value pair to the CustomDocumentProperties collection.
oDocCustomProps = oDoc.CustomDocumentProperties;
Type typeDocCustomProps = oDocCustomProps.GetType();
strIndex = "Knowledge Base Article";
strValue = "Q303296";
object[] oArgs = {strIndex,false,
MsoDocProperties.msoPropertyTypeString,
strValue};
typeDocCustomProps.InvokeMember("Add",BindingFlags.Default |
BindingFlags.InvokeMethod, null,
oDocCustomProps, oArgs );
MessageBox.Show("Select \"Properties\" from the File menu "
+ "to view the changes.\nSelect the Summary tab to view "
+ "the Subject property and the Custom tab to view the Knowledge"
+ "Base Article property.", "Check File Properties",
MessageBoxButtons.OK,MessageBoxIcon.Information);
}
- Gulir ke atas jendela kode, dan kemudian tambahkan berikut
garis akhir daftar menggunakan Petunjuk:
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
- Tekan F5 untuk menjalankan aplikasi.
Catatan The DocumentProperties dan DocumentProperty antarmuka adalah akhir terikat antarmuka. Menggunakan antarmuka ini,
Anda harus memperlakukan mereka seperti Anda akan IDispatch antarmuka. Untuk informasi lebih lanjut, kunjungi berikut Microsoft Developer
Jaringan Web site: Untuk informasi tambahan, klik nomor artikel berikut ini untuk melihat artikel di Pangkalan Pengetahuan Microsoft: 303294
(http://support.microsoft.com/kb/303294/
)
Bagaimana menggunakan otomatisasi untuk mendapatkan dan menetapkan properti dokumen Office dengan Visual Basic.NET ID Artikel: 303296 - Kajian Terakhir: 24 September 2011 - Revisi: 2.0 Berlaku bagi:- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .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 KB303296 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: 303296
(http://support.microsoft.com/kb/303296/en-us/
)
| |