Solved

How to make printable documents???

Posted on 2003-10-27
10
250 Views
Last Modified: 2013-11-20
I am making a program where I need to keep documents in some form where I can print them only from the program without having to open any other programs.  Sorry, I'm kinda new to MFC....Thanks!

Also, is there a way to make certain spots available for input from the program?  In other words, can these spots be populated with values the program specifies??

Thanks!
0
Comment
Question by:alexswinney
  • 4
  • 2
10 Comments
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9627576
What sort of documents? If you mean things like Word documents, you can do it with Word automation. You still need Word installed, as automation effectively runs Word in the background, but you can control it to open a document and print it. You could also use automation to insert text at certain points in your document before printing.

If this is what you are trying to do, let me know and I'll post some code up for you!

All the best
Martyn
0
 

Author Comment

by:alexswinney
ID: 9630933
The documents include:
-a contract
-a buyer's guide(for car sales
-info sheet for customer(after purchase)
-an IN state tax form(not sure if I can legally print this one, but I'm gonna go at it like I can)

I was hoping to NOT use word, as the machine they are using this on is rather old(300MHz slow) so if at ALL possible, I would rather stay away from any other programs.  I would rather use the automation part.

THANKS!!!
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9632303
Sorry - by type of document, I meant are they Word documents, PDF's, Excel documents etc? If they are Word or Excel documents, then you can definitely use automation. If they are other types of documents, then you may be able to use automation if the application has an automation interface.

However, you should be aware that when you use automation, you are actually running an instance of the application - albeit "invisibly". So, when you use MS Word automation, you are actually running an instance of the Word executable in the background - this is what is doing the work, like printing etc.

If you are sure you want to go down the Word automation route, then let me know and I'll dig out the code - I didn't want to post  code that doesn't make any sense if this is not the way you wish to go forwards.

All the best
Martyn
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:alexswinney
ID: 9641734
Well, I think I can benefit from any code that pertains to this area, as I might end up having to to do it that way, so please post if you would, Martyn.

If you would rather, you can email it to me at alexswinney@hotmail.com

Thanks again!
0
 
LVL 8

Accepted Solution

by:
martynjpearson earned 48 total points
ID: 9642013
OK, here goes! This assumes that you have an MFC application already created. The code demonstrates how to fire off Word as an automation server (i.e. it's doing the work for you), open a document, print it and close word again.

1) Using ClassWizard, click Add Class, then select "From a type library"
2) Locate your MSWord type library; it will be called MSWORDx.OLB, where x is a number that will differ according to the version of Word you have
3) ClassWizard will list a whole load of classes that can be generated - selet them all, and press OK.

4) In your source code where you want to print the document, first off include the generated header file :

#include "MSWordx.h" // x again will change according to the version you have

5) Then, you can use the following code. This will open a document, print it to the default printer and close :

COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant varTrue((short)VARIANT_TRUE, VT_BOOL), varFalse((short)VARIANT_FALSE, VT_BOOL);

    _Application application;

    // Start up Word
    if (application.CreateDispatch("Word.Application"))
    {
        try
        {
            // Get the documents collection and open the document
            Documents documents(application.GetDocuments());
            _Document document;
         
            COleVariant varFilename(strDocFilename); // Set strDocFilename accordingly
            document = documents.Open(varFilename, &varEmpty, &varTrue, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty);

            // Print the document
            document.PrintOut(&varFalse, &varFalse, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty);
            document.SetSaved(TRUE); // Necessary as printing makes the document "dirty"

            // Quit the application
            application.Quit(&varEmpty, &varEmpty, &varEmpty);
        }
        catch (COleDispatchException * e)
        {
            e->ReportError();
            e->Delete();
        }
        catch (COleException * e)
        {
            e->ReportError();
            e->Delete();
        }
        catch (CFileException * e)
        {
            e->ReportError();
            e->Delete();
        }
        catch (...)
        {
            AfxMessageBox("Unknown internal error");
        }
    }
    else
    {
        AfxMessageBox("Failed to start Microsoft Word.");
    }


Hope this helps
Martyn

0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9642968
The following code allows you to print an Excel spreadsheet :

COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant varTrue((short)VARIANT_TRUE, VT_BOOL), varFalse((short)VARIANT_FALSE, VT_BOOL);

    try
    {
        _Application appExcel;

        if (appExcel.CreateDispatch("Excel.Application"))
        {
            Workbooks workBooks(appExcel.GetWorkbooks());
            _Workbook workBook(workBooks.Open(strFilename, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty));
            workBook.PrintOut(varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty, varEmpty);

            workBook.Close(varFalse, varEmpty, varEmpty);
            appExcel.Quit();
        }
        else
        {
            AfxMessageBox("Failed to start Excel");
        }
    }
    catch (COleException * pOleException)
    {
        pOleException->Delete();
        bPrinted = false;
    }
    catch (COleDispatchException * pOleDispException)
    {
        pOleDispException->Delete();
        bPrinted = false;
    }

Note that if you use both the Word and Excel files in the same project, you'll need to wrap them both in separate namespaces so that you don't get clashes - they both use the same name for some of their objects!! To do this is really easy - just follow these steps :

1) At the top of the excel header file, add the lines (note that you can change the namespace line if you like) :

namespace ExcelFunctions
{

2) At the bottom of the excel header file, add a closing brace.
3) In the excel source file, at the top, add the line  :

using namespace ExcelFunctions;

4) Repeat steps 1 - 3 for the word files, using a different name, e.g. WordFunctions
5) In the function using the excel or word functions, add the line

using namespace ExcelFunctions; // Or WordFunctions!

If the function uses both, then you have to qualify objects as you create them, for example :

ExcelFunctions::_Application appExcel;

Hope this helps
Martyn
0
 
LVL 2

Assisted Solution

by:christophm
christophm earned 47 total points
ID: 9661826
PLEASE DO NOT SELECT MY ANSWER.

You can print 'natively' from MFC windowed apps without TOO much trouble.  For example, I wrote a DLL to generally handle all my printing needs from CDialog apps, though on a case-by-case basis just putting the printing code in a particular MFC app would be a lot easier than generalizing everything into a DLL.

I found that rather than mess around with the MFC printing classes you can find a VERY STRAIGHTFORWARD explanation of printing in "Programming Windows" by Charles Petzold.  Petzold's discussion, indeed the whole book, is about doing Windows stuff with C (not C++, not MFC).  The book is available from Microsoft Press, and every serious computer book store.  Even if you don't choose to use his printing discussion I think the book is a 'must have' for Windows programmers.

I found two disadvantages of using the C printing described by Petzold.  (1) there is no print preview.  Print Preview is something you get with the print classes in MFC, and, (2) I was forced to print on the user's default printer; i.e., I couldn't seem to get the 'select printer' dialog to work with network printers.  Let me add that a VB acquaintance was trying to get the 'select printer' dialog to work with network printers and failing on the same API calls.  ((Probably an indication we are/were both out to lunch!!!).

Good Luck - christophm  (please do not select my note as an answer)

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now