Microsoft KB Archive/308408

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

Article ID: 308408

Article Last Modified on 3/29/2007

-

APPLIES TO


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

-



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:  Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. 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.  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:
 * 1) include &quot;stdafx.h&quot;
 * 2) include 

//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
 * 1) using 
 * 2) using &quot;Office.dll&quot;
 * 1) using 

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(&quot;\nStarting Microsoft Word...&quot;); 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(&quot;\n\nRetrieving the Author Property...&quot;); pDocBuiltInProps = pDoc->BuiltInDocumentProperties; Object* pArgs[] = { S&quot;Author&quot; }; pAuthorProp = pDocBuiltInProps->GetType->InvokeMember(S&quot;Item&quot;,        BindingFlags::GetProperty, NULL, pDocBuiltInProps, pArgs); pValue = pAuthorProp->GetType->InvokeMember(S&quot;Value&quot;,         BindingFlags::GetProperty, NULL,pAuthorProp,NULL);

//Display the Author property Console::WriteLine(S&quot;\n\tAuthor is: {0}&quot;,pValue->ToString); Console::WriteLine(&quot;\nPress ENTER to continue.&quot;); Console::ReadLine;

//Set the Subject property. Console::WriteLine(&quot;\n\nSetting the Subject Property...&quot;); pArgs = new Object*[2]; pArgs[0] = S&quot;Subject&quot;; pArgs[1] = S&quot;A sample subject&quot;; pDocBuiltInProps->GetType->InvokeMember(&quot;Item&quot;,         BindingFlags::SetProperty, NULL, pDocBuiltInProps, pArgs );

//Add a property/value pair to the CustomDocumentProperties collection. Console::WriteLine(&quot;\n\nSetting properties...&quot;); pDocCustomProps = pDoc->CustomDocumentProperties; pArgs = new Object*[4]; pArgs[0] = S&quot;Knowledge Base Article&quot;; pArgs[1] = false; pArgs[2] = __box(MsoDocProperties::msoPropertyTypeString); pArgs[3] = S&quot;Q308408&quot;; pDocCustomProps->GetType->InvokeMember(&quot;Add&quot;,        BindingFlags::InvokeMethod, NULL, pDocCustomProps, pArgs ); Console::WriteLine(S&quot;\n\t1. Select Properties from the File menu.&quot;           S&quot;\n\t2. Go to the Summary tab to view the Subject property.&quot;            S&quot;\n\t3. Go to the Custom tab to view the custom property.&quot;); Console::WriteLine(&quot;\nPress ENTER to end.&quot;); Console::ReadLine;

}     catch(Exception* e)      { Console::WriteLine(S&quot;Error automating Word...&quot;); Console::WriteLine(e->get_Message); }

return 0; }                     Press F5 to run the application.

<div class="references_section">