Microsoft Knowledge Base Email Alertz

KBAlertz.com: (303296) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 303296 - Last Review: January 30, 2007 - Revision: 6.3

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

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.

MORE INFORMATION

Create an Automation Client for Microsoft Word

  1. Start Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default.
  3. Add a reference to Microsoft Word Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. 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  (http://kbalertz.com/Feedback.aspx?kbNumber=328912/EN-US/ ) INFO: Microsoft Office XP PIAs Are Available for Download
    3. 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.
  4. On the View menu, select Toolbox to display the Toolbox, and then add a button to Form1.
  5. Double-click Button1. The code window for the form appears.
  6. 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 = "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);
    }
    					
  7. 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;
    					
  8. Press F5 to run the application.
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.

REFERENCES

For more information, visit the following Microsoft Developer Network Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
303294  (http://kbalertz.com/Feedback.aspx?kbNumber=303294/ ) How To Use Automation to Get and to Set Office Document Properties with Visual Basic .NET

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
Keywords: 
kbpia kbautomation kbhowto KB303296
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please