How To Retrieve Dialog Info from Word Using an MFC App (152070)



The information in this article applies to:

  • Microsoft Visual C++ 2.0
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • Microsoft Visual C++ 4.0
  • Microsoft Visual C++ 4.1

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:
  1. 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.
  2. 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.
  3. 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");
    					
  4. 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 )
    					
  5. 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);
    					
  6. 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)
    					
  7. 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);
    					
  8. 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);
    					

REFERENCES

WordBasic Help, shipped with Microsoft Word versions 6.0, 7.0

Modification Type:MinorLast Reviewed:3/21/2005
Keywords:kbhowto kbNoUpdate KB152070