Microsoft Knowledge Base Email Alertz

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

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: 308408 - Last Review: March 29, 2007 - Revision: 6.4

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

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://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
For more general information about Visual C++ .NET, see the following Usenet newsgroup:
Microsoft.public.dotnet.languages.vc (http://msdn.microsoft.com/newsgroups/default.aspx?query=Microsoft.public.dotnet.languages.vc&dg=&cat=en-us-msdn&lang=en&cr=US&pt=&catlist=774F24A2-F71F-425F-AC2B-DC48AB0DA5C9&dglist=&ptlist=&exp=&sloc=en-us)
Visit the Visual C++ .NET Support Center at:
Visual C++ .NET (2002) Support Center (http://support.microsoft.com/default.aspx?xmlid=fh%3ben-us%3bvcnet)

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
Keywords: 
kbautomation kbhowto kbnewsgrouplink KB308408
       

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