Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Use Automation to Get and Set Office Document Properties from a Managed C++ Client


View products that this article applies to.

Summary

This article illustrates how to automate Word with Visual C++ .NET to retrieve and manipulate document properties. Although the sample in this article is specifically written to automate Word, the same concepts can be applied to Excel and PowerPoint.

↑ Back to the top


More information

To automate Word with Visual C++ .NET to retrieve and manipulate document properties, follow these steps:
  1. Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project.
  2. Under Project Types, click the Visual C++ Projects folder, then click Managed C++ Applicationfor Visual Studio .NET 2002 or Console Application (.NET) for Visual Studio .NET 2003 under Templates. Give the project a name and make a note of the folder in which the project will be created.
  3. Open the source file that contains the definition of _tmain in the source editor, and then replace the contents of the file with the following code:
    #include "stdafx.h"
    #include <tchar.h>
    
    #using <mscorlib.dll>
    #using "Office.dll"
    //This is the path to the Strong-Named Microsoft.Office.Interop.Word DLL.  
    //Following path may change based on the version and strong name of the DLL
    #using <C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Word\10.0.4504.0__31bf3856ad364e35\Microsoft.Office.Interop.Word.dll>
    
    using namespace System;
    using namespace System::Reflection;
    using namespace Microsoft::Office::Core;
    using namespace Microsoft::Office::Interop;
    
    // This is the entry point for this application.
    int _tmain(void)
    {
       try{
          System::Object* pMissing = System::Reflection::Missing::Value;
          Object* pDocBuiltInProps;
          Object* pDocCustomProps;
          Object* pAuthorProp;
          Object* pValue;
    
          //Launch Word and make it Visible
          Console::WriteLine("\nStarting Microsoft Word...");
          Word::ApplicationClass* pWord = new Word::ApplicationClass;
          pWord->Visible = true;
    
          //Create a new document and get the BuiltInDocumentProperties collection.
          Word::Documents* pDocs = pWord->Documents;
          Word::Document* pDoc = pDocs->Add(&pMissing,&pMissing,&pMissing,&pMissing);
    
          //Get the Author property
          Console::WriteLine("\n\nRetrieving the Author Property...");
          pDocBuiltInProps = pDoc->BuiltInDocumentProperties;
          Object* pArgs[] = { S"Author" }; 
          pAuthorProp = pDocBuiltInProps->GetType()->InvokeMember(S"Item",
             BindingFlags::GetProperty, NULL, pDocBuiltInProps, pArgs);
          pValue = pAuthorProp->GetType()->InvokeMember(S"Value", 
             BindingFlags::GetProperty, NULL,pAuthorProp,NULL);
    
          //Display the Author property
          Console::WriteLine(S"\n\tAuthor is: {0}",pValue->ToString());
          Console::WriteLine("\nPress ENTER to continue.");
          Console::ReadLine();
    
          //Set the Subject property.
    	  Console::WriteLine("\n\nSetting the Subject Property...");
          pArgs = new Object*[2];
          pArgs[0] = S"Subject";
          pArgs[1] = S"A sample subject";
          pDocBuiltInProps->GetType()->InvokeMember("Item", 
             BindingFlags::SetProperty, NULL, pDocBuiltInProps, pArgs );
    
          //Add a property/value pair to the CustomDocumentProperties collection.
          Console::WriteLine("\n\nSetting properties...");
          pDocCustomProps = pDoc->CustomDocumentProperties;		
          pArgs = new Object*[4];
          pArgs[0] = S"Knowledge Base Article";
          pArgs[1] = false;
          pArgs[2] = __box(MsoDocProperties::msoPropertyTypeString);
          pArgs[3] = S"Q308408";
          pDocCustomProps->GetType()->InvokeMember("Add",
             BindingFlags::InvokeMethod, NULL, pDocCustomProps, pArgs );
          Console::WriteLine(S"\n\t1. Select Properties from the File menu."
                S"\n\t2. Go to the Summary tab to view the Subject property."
                S"\n\t3. Go to the Custom tab to view the custom property.");
          Console::WriteLine("\nPress ENTER to end.");
          Console::ReadLine();
    
          }
          catch(Exception* e)
          {
             Console::WriteLine(S"Error automating Word...");
             Console::WriteLine(e->get_Message());
          }
    
       return 0;
    } 
    					
  4. Press F5 to run the application.

↑ Back to the top


References

For more information, see the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx
For more general information about Visual C++ .NET, see the following Usenet newsgroup: Visit the Visual C++ .NET Support Center at:

↑ Back to the top


Keywords: kbautomation, kbhowto, kbnewsgrouplink, KB308408

↑ Back to the top

Article Info
Article ID : 308408
Revision : 11
Created on : 3/29/2007
Published on : 3/29/2007
Exists online : False
Views : 543