PDA

View Full Version : How to get the default printer name



Sonny Falk
24-Jan-2005, 01:57 PM
I googled this one and it seems like this is a known behavior of the
ActivePrinter property in Word. See
http://support.microsoft.com/kb/q216026/.

They seem to refer to the old obsolete interface in Word to work around
this problem. Word97 used to have one massive flat interface instead of
the more object oriented interface you see in more recent versions of
Word. It's still available for backwards compatibility, so you can use
that. This obsolete interface is marked as such in the type library, so
the Studio doesn't generate the code for that class. However, you can
easily generate the code for this deprecated class using an older
version of the Word type library.

I'm attaching an older version of the Word type library here. Just save
it somewhere and Select Import Automation Library in the Studio. Then
select the Browse button, and browse to the type library file. Then
place a Use statement in your code for this pkg. This will bring in the
old interface. In your regular Word Application object, there's a
function called ComWordBasic. This function will return an object of the
old deprecated interface.

I haven't tried this, so I'm not sure it actually works, but it should
be something like this:

Procedure PrintTheDoc
Handle hoWdBasic
Variant vWdBasic
Get Create U_cComWordBasic to hoWdBasic
Get ComWordBasic oMyWordAppObj to vWdBasic
Set pvComObject of hoWdBasic to vWdBasic

Send ComFilePrintSetup of hoWdBasic ...
...
Send Destroy to hoWdBasic
End_Procedure

Hope this helps.

-Sonny Falk (DAC)


> -----Original Message-----
> From: tbrannen [mailto:tbrannen@ate.net]
> Posted At: Monday, January 24, 2005 1:16 PM
> Posted To: visual-dataflex
> Conversation: How to get the default printer name
> Subject: How to get the default printer name
>
>
> Greetings,
> I have a way to select a printer using the VDF print
> dialog, but when I set the printer in Word using our
> Word8.pkg (FlexOle), it becomes the default printer for the
> computer. If I knew the original default printer, I could
> easily set it back to that on exiting, but I don't know how
> to get that information. There is a command
> "getglobalactiveprinter" in the Word8.pkg but it does not
> return anything. Neither does "getApplicationActivePrinter".
> Is there a way in just VDF to get that information? Maybe a
> call to windows API? Then I could get the current default
> printer as my view opens and save it as a property and reset
> it when the view closes.
>
> All help appreciated.
>
> Thanks,
>
> Tod Brannen
>
>

Tod Brannen
24-Jan-2005, 05:31 PM
Sonny,
Thanks for the reply. I am using the old Word8.pkg and MSO97.pkg and not
the new COM interface. (I haven't had time to learn COM yet.) The problem I
have with the link to MS support, is that I don't know how to convert their
Visual Basic code into Visual Dataflex code. I am using VDF 8.3 for this
project.

Tod Brannen

Mark Hullin
24-Jan-2005, 07:04 PM
I recently solved this problem in a .net app, but I think the same method
will work in VDF:

Before you set the ActivePrinter in Word, get the ActivePrinter to a local
variable, then set it to your new printer, print, and then set it back to
the original.

My only concerns with the solution were:

- What happens if you have another app that wants to print at the same
time as Word is playing around with the default printer.

- None of my code has ever crashed before (yea, right), but what if it
did? What if Word crashes after I've set the printer but before I reset it
to the original?

Hope this helps.

Mark Hullin
M2 Information Systems, Inc.


"tbrannen" <tbrannen@ate.net> wrote in message
news:JswkbRmAFHA.1240@dacmail.dataaccess.com...
> Sonny,
> Thanks for the reply. I am using the old Word8.pkg and MSO97.pkg and
> not
> the new COM interface. (I haven't had time to learn COM yet.) The problem
> I
> have with the link to MS support, is that I don't know how to convert
> their
> Visual Basic code into Visual Dataflex code. I am using VDF 8.3 for this
> project.
>
> Tod Brannen
>
>

Garret Mott
24-Jan-2005, 09:45 PM
PMJI, but I've done this a # of times in VB.

[MH] - Before you set the ActivePrinter in Word, get the ActivePrinter to a
local
> variable, then set it to your new printer, print, and then set it back to
> the original.

Exactly right, but in VB, the variable needs to be defined as a "Printer"
variable (VB Code snippet - note that ' is the comment character - like //
in VDF):

Dim X As Printer

'Set Printer to the user's label printer
'ALL the label printers start with "ESI_LABEL"
For Each X In Printers
If InStr(UCase(X.DeviceName), "ESI_LABEL") > 0 Then
Set Printer = X
Exit For
End If
Next

Not sure how this would translate to VDF. I guess I'd try a Variant first.
Another point - the above code does _not_ change the default, just sets what
the app will use. Maybe you can do the same?

[MH] - What happens if you have another app that wants to print at the same
> time as Word is playing around with the default printer.

The other app will print to whatever is set as the current default printer,
unless the app specifies one. However, unless the doc the VDF app is
printing is a big one, it shouldn't be a major problem. I found that I
could change the default printer, send a 5-7 page document to the printer &
then immediately change the printer back in about a second. The default
printer _can_ be set back before spooling is done.

[MH] - None of my code has ever crashed before (yea, right), but what if it
> did?

Hey - none of mine has either!!!!! <vbg>. If the Default has been set & not
changed back, it'll still be what was set.

[MH] - What if Word crashes after I've set the printer but before I reset
it
>to the original?

Setting the default printer is independent of Word, so this shouldn't be an
issue.

HTH,

Garret


"Mark Hullin" <mhullin@m2is.com> wrote in message
news:rrIWcGnAFHA.1952@dacmail.dataaccess.com...
>I recently solved this problem in a .net app, but I think the same method
>will work in VDF:
>
> Before you set the ActivePrinter in Word, get the ActivePrinter to a local
> variable, then set it to your new printer, print, and then set it back to
> the original.
>
> My only concerns with the solution were:
>
> - What happens if you have another app that wants to print at the same
> time as Word is playing around with the default printer.
>
> - None of my code has ever crashed before (yea, right), but what if it
> did? What if Word crashes after I've set the printer but before I reset
> it to the original?
>
> Hope this helps.
>
> Mark Hullin
> M2 Information Systems, Inc.
>
>
> "tbrannen" <tbrannen@ate.net> wrote in message
> news:JswkbRmAFHA.1240@dacmail.dataaccess.com...
>> Sonny,
>> Thanks for the reply. I am using the old Word8.pkg and MSO97.pkg and
>> not
>> the new COM interface. (I haven't had time to learn COM yet.) The problem
>> I
>> have with the link to MS support, is that I don't know how to convert
>> their
>> Visual Basic code into Visual Dataflex code. I am using VDF 8.3 for this
>> project.
>>
>> Tod Brannen
>>
>>
>
>

Focus
23-Nov-2011, 08:27 AM
Bump !

Once the dust (cough cough) has settled has anyone got this to work ?

ComWordBasic is in the MSWORD.PKG but I cant see a WordBasic class to create an object of

ie how does this work .... Get Create U_cComWordBasic to hoWdBasic

I though if I looked down the list of importable activex/com objects in VDF I would see Word 6.0 legacy or something like that but no

There seems a fundamental flaw with ActivePrinter in that it chnages the default printer and a very crude work around shown here

http://support.microsoft.com/kb/209722

But using the WordBasic trick gets around this. Has anyone got it to work ?

A VB/C# example is here

http://www.codeproject.com/KB/office/WordPrint.aspx

Cheers

Focus
23-Nov-2011, 08:37 AM
OK .... RTFP first .... I'll have a go at this ...

Focus
23-Nov-2011, 09:41 AM
OK I've got a bit further with this

Import trhe TLB as Sonny says

This works so long as it is a local printer



Procedure PrintTheDoc
Handle hoWdBasic
Variant vWdBasic vPrinterName vNotDefault

Get Create U_cComWordBasic to hoWdBasic
Get ComWordBasic of oMyWordAppObj to vWdBasic
Set pvComObject of hoWdBasic to vWdBasic

Move "Microsoft XPS Document Writer" to vPrinterName
Move 1 to vNotDefault
Send ComFilePrintSetup of hoWdBasic vPrinterName Nothing Nothing vNotDefault
...
Send Destroy to hoWdBasic
End_Procedure


However if it is a network printer all of the combinations i've tried just result in an error

I've even found the Word 95 WordBasic help file ! ....

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7035

.... which I was hoping would explain what the Network parameter is expecting ... but alas it didn't

Focus
23-Nov-2011, 10:01 AM
Actually this does work with a network printer I must have just mistyped the \\network\printer (file://\\network\printer)name

Perhaps someone else would like to try to see if it works for them and solve this age old prob

Cheers

Ola Eldoy
24-Nov-2011, 02:01 AM
It should also be possible to set the default printer using the Windows API directly. I suspect this is demonstrated in Vincent's "Printing" workspace (http://www.dataaccess.com/kbasepublic/KBPrint.asp?ArticleID=2322).

Focus
24-Nov-2011, 05:01 AM
Hi Ola

The title of the originial post is confusing!. This has NOTHING to do with setting the windows default printer

It's a back door hack that still seems to be required even in Word2010 if you want to change IT'S printer and NOT have to change the windows default ie do the same action of the user selecting a printer in the print dialog in Word which has no effect on the default printer.