HOWTO: Retrieve Dialog Info from Word Using an MFC App

Last reviewed: February 17, 1998
Article ID: Q152070
The information in this article applies to:

  - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0, 4.1

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

Keywords          : MfcOLE VCx86 kbappnote
Technology        : kbMfc kbole
Version           : WINDOWS NT:2.0,2.1,2.2,4.0,4.1
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 17, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.