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



The information in this article applies to:

  • Microsoft Office Excel 2003
  • Microsoft Excel 2002
  • Microsoft Excel 2000
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002
  • Microsoft PowerPoint 2000
  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Visual C# .NET (2003)

This article was previously published under Q308408

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.

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.

REFERENCES

For more information, see the following Microsoft Developer Network (MSDN) Web site:

Microsoft Office Development with Visual Studio
http://msdn.microsoft.com/library/en-us/dnoxpta/html/vsofficedev.asp

For more general information about Visual C++ .NET, see the following Usenet newsgroup: Visit the Visual C++ .NET Support Center at:

Modification Type:MajorLast Reviewed:1/19/2006
Keywords:kbAutomation kbhowto kbNewsgroupLink KB308408 kbAudDeveloper