Link to home
Start Free TrialLog in
Avatar of alexswinney
alexswinney

asked on

How to make printable documents???

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!
Avatar of martynjpearson
martynjpearson

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
Avatar of alexswinney

ASKER

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!!!
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
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!
ASKER CERTIFIED SOLUTION
Avatar of martynjpearson
martynjpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial