HOWTO: Use Automation to Set the Printer from an MFC Project

Last reviewed: February 6, 1998
Article ID: Q180312
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Editions, version 5.0
  • Microsoft Word 97 for Windows
  • Microsoft Excel 97 for Windows

SUMMARY

This article discusses how to use Version 4.2 of the Microsoft Foundation Class (MFC) library installed with Microsoft Visual C++ version 5.0 to automate specifying and setting the printer used with a project. It shows how to use the IDispatch interface of the _Application object from the typelib of the application that you are automating. The example code specifically uses Microsoft Word, but the approach works with any other application where the _Application IDispatch interface has the appropriate member functions.

Alternatively, you can use the Windows API library functions to set the printer directly. This article does not discuss this approach.

MORE INFORMATION

You can copy the code in this article to the message handler function of an event defined in an MFC .cpp file. However, the purpose of the code is to illustrate the process of using the IDispatch interfaces and member functions defined in the Msword8.olb type library. The primary benefit comes from reading and understanding the code so that you can modify the example, or write code from scratch to automate setting the printer.

Steps to Create the Project

  1. Follow steps 1 through 12 in the following Microsoft Knowledge Base article to create a sample project that uses the IDispatch interfaces and member functions defined in the MSWord8.olb type library:

          ARTICLE-ID: Q178749
    
          TITLE     : HOWTO: Create an Automation Project Using MFC and a Type
                      Library
    
    

  2. At the top of the AutoProjectDlg.cpp, add the following line:

          #include "msword8.h"
    

  3. Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDlg.cpp file:

    Sample Code -----------

          CWaitCursor begin;     // Hourglass.
          {
           _Application objApp;  // You need an application's typelib;
                                 // therefore, you need an object.
           // You can use any application that has an IDispatch
           // interface containing SetActivePrinter()
           // and GetActivePrinter() member functions.
           objApp.CreateDispatch("Word.Application");
           // This could be objApp.CreateDispatch("Excel.Application");
    
           // For a network printer, use:
           // objApp.SetActivePrinter("\\\\PrnServr\\HP3SI@1134 on NE01");
    
           // For a local printer, use:
           objApp.SetActivePrinter("HP LaserJet IIISi");
    
           ::Sleep(10000);       // You need to provide a considerable wait if
                                 // you are using a network printer.
    
           CString thePrinter = objApp.GetActivePrinter();  // Verify the new
                                                            // setting.
           MessageBox(thePrinter, "The Active Printer Is",
                      MB_SETFOREGROUND | MB_OK);
    
           // Release the objApp object and close Word.
           objApp.Quit(COleVariant((short)FALSE),  // VARIANT* SaveChanges
                        COleVariant((short)TRUE),   // VARIANT* OriginalFormat
                        COleVariant((short)FALSE)   // VARIANT* RouteDocument
                        );
           // If you use Excel for its IDispatch interface functions,
           // objApp.Quit() takes no parameters.
          }
    
    
Keywords          : MfcOLE kbcode kbinterop
Technology        : kbMFC kbOle
Version           : win95:5.0; WINNT:5.0
Platform          : Win95 winnt
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 6, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.