Microsoft KB Archive/303296

= How To Use Automation to Get and to Set Office Document Properties with Visual C# .NET =

Article ID: 303296

Article Last Modified on 1/30/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Office PowerPoint 2003
 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q303296



SUMMARY
This article demonstrates how to create a Microsoft Visual C# .NET Automation client that manipulates the properties of a Microsoft Word document. Although the sample code is specific to Word, the same techniques can be applied when automating Microsoft Excel and Microsoft PowerPoint.



Create an Automation Client for Microsoft Word
 Start Visual Studio .NET. On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default. Add a reference to Microsoft Word Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, locate Microsoft Word Object Library, and then click Select.

Note Microsoft Office 2003 includes Primary Interop Assemblies (PIAs). Microsoft Office XP does not include PIAs, but they can be downloaded. For additional information about Office XP PIAs, click the article number below to view the article in the Microsoft Knowledge Base:

328912 INFO: Microsoft Office XP PIAs Are Available for Download

 Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click Yes.</ol> </li> On the View menu, select Toolbox to display the Toolbox, and then add a button to Form1.</li> Double-click Button1. The code window for the form appears.</li>  In the code window, replace the following code private void button1_Click(object sender, System.EventArgs e) { } 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 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 = &quot;Author&quot;; string strValue; object oDocAuthorProp = typeDocBuiltInProps.InvokeMember(&quot;Item&quot;,                              BindingFlags.Default |                               BindingFlags.GetProperty,                               null,oDocBuiltInProps,                               new object[] {strIndex} ); Type typeDocAuthorProp = oDocAuthorProp.GetType; strValue = typeDocAuthorProp.InvokeMember(&quot;Value&quot;,                              BindingFlags.Default |                              BindingFlags.GetProperty,                              null,oDocAuthorProp,                              new object[] {} ).ToString; MessageBox.Show( &quot;The Author is: &quot; + strValue,&quot;Author&quot; );

//Set the Subject property. strIndex = &quot;Subject&quot;; strValue = &quot;The Subject&quot;; typeDocAuthorProp.InvokeMember(&quot;Item&quot;,                              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 = &quot;Knowledge Base Article&quot;; strValue = &quot;Q303296&quot;; object[] oArgs = {strIndex,false, MsoDocProperties.msoPropertyTypeString, strValue};

typeDocCustomProps.InvokeMember(&quot;Add&quot;,BindingFlags.Default |                              BindingFlags.InvokeMethod, null,                               oDocCustomProps, oArgs );

MessageBox.Show(&quot;Select \&quot;Properties\&quot; from the File menu &quot;       + &quot;to view the changes.\nSelect the Summary tab to view &quot;        + &quot;the Subject property and the Custom tab to view the Knowledge&quot;           + &quot;Base Article property.&quot;, &quot;Check File Properties&quot;,        MessageBoxButtons.OK,MessageBoxIcon.Information); }                   </li>  Scroll to the top of the code window, and then add the following lines to the end of the list of using directives: using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; using System.Reflection; </li> Press F5 to run the application.</li></ol>

Note The DocumentProperties and the DocumentProperty interfaces are late bound interfaces. To use these interfaces, you must treat them like you would an IDispatch interface.

<div class="references_section">