Microsoft KB Archive/152070

= How To Retrieve Dialog Info from Word Using an MFC App =

Article ID: 152070

Article Last Modified on 3/21/2005

-

APPLIES TO


 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 2.2
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription

-



This article was previously published under Q152070



SUMMARY
This article describes how to retrieve values from Word's dialog boxes using OLE Automation in an MFC-based application. To retrieve values, the client application must create a CurValues object and an object that will represent the data in one of Word's dialogs.



MORE INFORMATION
Using Visual C++, it is possible to create a wrapper class for a Word type library. This wrapper class represents a WordBasic object. Using the WordBasic class, it is possible to call many methods of the WordBasic object from within an MFC application. However, it is not possible to get the settings of any of the Word dialogs using the WordBasic object's methods directly. You can get information such as Summary Info and Word Count from Word's dialogs by using a CurValues object. To accomplish this in an MFC application, you must take the following steps:  Use Class Wizard to create an MFC-based application to serve as the Automation client. Ensure that OLE has been properly initialized for your MFC application. This is usually done by calling AfxOleInit in the CWinApp-derived class's InitInstance. This example also assumes that you have correctly installed Word 6.0 or 7.0 on your system and that it is registered correctly with the system registry. Create a WordBasic class by using Class Wizard to create a new class from a type library. Class libraries available for Word automation are:  wb60en.tlb for Word 6.0 wb70en32.tlb for Word 7.0.   In your code somewhere, instantiate a WordBasic object and attach it to a COleDispatchDriver. The following code shows how to do this for the WordBasic class created by Class Wizard from one of the Word type libraries: WordBasic wb; wb.CreateDispatch("Word.Basic");   Use the WordBasic object to get the DISPID for its CurValues object. For example, the following code gets the DISPID for the CurValues object using the WordBasic object from step 2: OLECHAR* lpszCurValues = L"CurValues"; DISPID dispidCurValues; wb.m_lpDispatch->GetIDsOfNames( IID_NULL, &lpszCurValues, 1,                                 LOCALE_SYSTEM_DEFAULT, &dispidCurValues ) </li>  Use the CurValues DISPID to get the CurValues property of the WordBasic object. This property is of type VT_DISPATCH and can be attached to a COleDispatchDriver as shown below: COleDispatchDriver dispdrvrCurValues; LPDISPATCH resultDispatch; wb.GetProperty(dispidCurValues, VT_DISPATCH,                      (void*)&resultDispatch); dispdrvrCurValues.AttachDispatch(resultDispatch); </li>  Use the COleDispatchDriver from step 4 to get the DISPID for the dialog whose information you wish to retrieve. For example, to get the document's Summary Info, use code similar to the following: OLECHAR* lpszFileSummaryInfo = L"FileSummaryInfo"; DISPID dispidFileSummaryInfo;

dispdrvrCurValues.m_lpDispatch->GetIDsOfNames( IID_NULL,     &lpszFileSummaryInfo, 1,      LOCALE_SYSTEM_DEFAULT,&dispidFileSummaryInfo) </li>  Use the CurValues object to get a property of type VT_DISPATCH that represents the dialog containing the information you wish to retrieve. The LPDISPATCH you retrieve can be attached to a COleDispatchDriver as shown below: COleDispatchDriver dispdrvrFileSummaryInfo; LPDISPATCH resultDispatch;

dispdrvrCurValues.GetProperty(dispidFileSummaryInfo, VT_DISPATCH,        (void*)&resultDispatch); dispdrvrFileSummaryInfo.AttachDispatch(resultDispatch); </li>  You can now use the COleDispatchDriver from step 6 to get values from the dialog. To get the document title from the Summary Info dialog from step 6, you would use code similar to the following: OLECHAR* lpszTitle = L"Title"; DISPID dispidDocProperty; CString Result;

dispdrvrFileSummaryInfo.m_lpDispatch->GetIDsOfNames( IID_NULL,         &lpszTitle, 1, LOCALE_SYSTEM_DEFAULT, &dispidDocProperty);

dispdrvrFileSummaryInfo.GetProperty, (dispidDocProperty,VT_BSTR,&Result); </li></ol>

<div class="references_section">