HOWTO: Retrieve Dialog Info from Word Using an MFC App

ID: Q152070


The information in this article applies to:


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:



  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

Additional query words:


Keywords          : kbole kbMFC kbVC kbx86 
Version           : WINDOWS NT:2.0,2.1,2.2,4.0,4.1
Platform          : NT WINDOWS 
Issue type        : kbhowto 

Last Reviewed: August 3, 1999