SUMMARY This article describes how to create a Microsoft Visual C # .NET automation client that can operate Microsoft Word document properties. Although the code example is specifically for Word, these techniques can also be used when Microsoft Excel and Microsoft PowerPoint are automatically run.
More information
Create an automated client for Microsoft Word
Start Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. FORM1 is created by default. Add a reference to the Microsoft Word object library. To do this, follow these steps:
On the project menu, click Add Reference. On the COM tab, find the Microsoft Word object library, and then click Select. Note: Microsoft Office 2003 contains the main intero set (PIA). Microsoft Office XP does not contain PIA, but you can download PIA. For additional information about Office XP PIA, click the article number below to view the article in the Microsoft Knowledge Base: 328912 Info: Microsoft Office XP PIA is available for download in the Add Reference dialog box Click OK to accept your choice . If the system prompts you to generate a package for the selected library, click Yes. On the View menu, select the Toolbox to display the toolbox, then add a button to Form1. Double click on Button1. The code window of the form appears. In the code window, put the following code private void button1_click (Object Sender, System.EventArgs E)
{
}
Replace with: 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 it is visible.
OWORD = new word.application ();
Oword.visible = true;
// Create a new document and get the buildindocumentproperties collection.
Odoc = Oword.documents.add (Ref Omissing, Ref Omissing, Ref Omiissing,
Ref omissing;
Odocbuiltinprops = odoc.builtindocumentproperties;
TYPE TYPEDOCBUILTINPROPS = odocbuiltinprops.gettype ();
// Get The Author Property and Display IT.
String strinderx = "author";
String Strvalue;
Object OdocAuthorprop = TypedocBuillingProps.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,
MsodoProperties.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;
}
Scroll to the top of the code window, then add the following row to the end of the Using command list: use Microsoft.Office.core;
Using Word = Microsoft.Office.Interop.Word;
Using system.reflection;
Press the F5 key to run the app.
note:
DocumentProperties and
The DocumentProperty interface is an advanced binding interface. To use these interfaces, you must be treated
Treat them as the IDispatch interface.
Refer to more information, please visit the following Microsoft Developer Network Web site: Microsoft Office Development with Visual Studio (Microsoft Office development using Visual Studio) http://msdn.microsoft.com/library/en-us/dnoffdev/html /VSOfficeDev.asp For additional information, click the article number below to see the article in the Microsoft Knowledge Base:
303294 HOWTO: USE Automation To Get and To Set Office Document Properties with Visual Basic .NET
The information in this article applies to:
Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002) Microsoft Office Excel 2003 Microsoft Excel 2002 Microsoft Office PowerPoint 2003 Microsoft PowerPoint 2002 Microsoft Office Word 2003 Microsoft Word 2002
Recent Updated: 2004-2-13 (5.0) Keywords: kbpia kbautomation Kbhowto KB303296