#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;
}