Board index » cppbuilder » Excel-OLE: change ActivePrinter

Excel-OLE: change ActivePrinter


2004-05-24 10:31:42 PM
cppbuilder84
Hello,
I'm working/interacting with Excel using pure OLE-interface functionality.
Most of the things I wanted to be executed worked fine, but I didn't menage
to change/specify a different printer. My code is like this:
...
vExcelApp = CreateOleObject("Excel.Application");
vExcelApp.OlePropertySet("ActivePrinter", szPrinterName); // with: char*
szPrinterName
...
If I proceed like this, an exception occurs (EOleSysError withe the hint "an
exception occured")
(Reading the property with
Variant vActivePrinter = vExcelApp.OlePropertyGet("ActivePrinter");
succeeeds!)
If I try to set the property inside an Excel Makro, like
Application.ActivePrinter = MyPrinterName
it succeeds.
Has anyone an idea or another solution to change the printer used currently
by code?
Thanks, Uli.
 
 

Re:Excel-OLE: change ActivePrinter

perhaps you could try something like szPrinterName being of type WideString
?
Yahia
 

Re:Excel-OLE: change ActivePrinter

Hello Uli,
First of all I would recommend that you add 'Variant.cpp' to your project
and set a breakpoint on the method Variant::OlePropertySet. You actually
will notice that it comes back to utilcls.h's TAutoDriver<DISPINTF>::Invoke.
That method is the core of all automation invocations. Fortunately, there's
good error handling mechanism there and Excel is typically very good at
filling out the information. Unfortunately, that information is not bubbled
back through the automation method of Variant. There was a strong push to
keep COM independent of VCL back when these units were written. With
hindsight, I don't think the current approach is idea, but...
Anyway, step through the Invoke method and inspect the 'm_ExcepInfo'
structure after the call to m_Dispatch->Invoke [line 4317 in my copy - this
may be slightly different in yours]. With Excel, the description field
typically contains something that could help identify the issue.
Now about setting printers in the Office Products, if it's a network printer
you're setting, there are couple of gotchas:
1. the name has the following syntax - "\\\\PrintName\\xxxx on NE##". IOW,
it's not just the name that shows up in the Printer Setup dialog. Plus make
sure backslashes are escaped with an extra '\'.
2. I suspect you're not that far yet but Network printers require a little
delay before kicking in. Sample code on the MS network show a call to
"::Sleep(10000)". See
support.microsoft.com/default.aspx
Regards,
Bruneau.
"Uli" < XXXX@XXXXX.COM >wrote in message
Quote
Hello,

I'm working/interacting with Excel using pure OLE-interface functionality.
Most of the things I wanted to be executed worked fine, but I didn't
menage
to change/specify a different printer. My code is like this:

...
vExcelApp = CreateOleObject("Excel.Application");
vExcelApp.OlePropertySet("ActivePrinter", szPrinterName); // with:
char*
szPrinterName
...

If I proceed like this, an exception occurs (EOleSysError withe the hint
"an
exception occured")
(Reading the property with
Variant vActivePrinter = vExcelApp.OlePropertyGet("ActivePrinter");
succeeeds!)

If I try to set the property inside an Excel Makro, like
Application.ActivePrinter = MyPrinterName
it succeeds.

Has anyone an idea or another solution to change the printer used
currently
by code?

Thanks, Uli.


 

{smallsort}

Re:Excel-OLE: change ActivePrinter

Hello JM,
Thanks for your detailed and precise description - THIS works: stepping
through the method and inspecting m_ExcepInfo shows (I'll try to translate
it from german) "The ActivePrinter property of the ApplicationObject can not
be assigned" - Why not? From inside Excel (VBA) it works! So ...?
What simply want to get managed is, that I want to specify a printer, which
is not the windows standard printer, on which the printout, initiated by ---
vWorksheet.OleFunction("PrintOut"); ---, should happen. So, can I do
something instead than referring to the ActivePrinter property?
Thanks,
Uli.
"Jean-Marie Babet" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote
Hello Uli,

First of all I would recommend that you add 'Variant.cpp' to your project
and set a breakpoint on the method Variant::OlePropertySet. You actually
will notice that it comes back to utilcls.h's
TAutoDriver<DISPINTF>::Invoke.
That method is the core of all automation invocations. Fortunately,
there's
good error handling mechanism there and Excel is typically very good at
filling out the information. Unfortunately, that information is not
bubbled
back through the automation method of Variant. There was a strong push to
keep COM independent of VCL back when these units were written. With
hindsight, I don't think the current approach is idea, but...

Anyway, step through the Invoke method and inspect the 'm_ExcepInfo'
structure after the call to m_Dispatch->Invoke [line 4317 in my copy -
this
may be slightly different in yours]. With Excel, the description field
typically contains something that could help identify the issue.

Now about setting printers in the Office Products, if it's a network
printer
you're setting, there are couple of gotchas:

1. the name has the following syntax - "\\\\PrintName\\xxxx on NE##". IOW,
it's not just the name that shows up in the Printer Setup dialog. Plus
make
sure backslashes are escaped with an extra '\'.

2. I suspect you're not that far yet but Network printers require a little
delay before kicking in. Sample code on the MS network show a call to
"::Sleep(10000)". See
support.microsoft.com/default.aspx


Regards,


Bruneau.


"Uli" < XXXX@XXXXX.COM >wrote in message
news:40b2074e$ XXXX@XXXXX.COM ...
>Hello,
>
>I'm working/interacting with Excel using pure OLE-interface
functionality.
>Most of the things I wanted to be executed worked fine, but I didn't
menage
>to change/specify a different printer. My code is like this:
>
>...
>vExcelApp = CreateOleObject("Excel.Application");
>vExcelApp.OlePropertySet("ActivePrinter", szPrinterName); // with:
char*
>szPrinterName
>...
>
>If I proceed like this, an exception occurs (EOleSysError withe the hint
"an
>exception occured")
>(Reading the property with
>Variant vActivePrinter = vExcelApp.OlePropertyGet("ActivePrinter");
>succeeeds!)
>
>If I try to set the property inside an Excel Makro, like
>Application.ActivePrinter = MyPrinterName
>it succeeds.
>
>Has anyone an idea or another solution to change the printer used
currently
>by code?
>
>Thanks, Uli.
>
>