printersettings papersources does not return correct values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Golebiowski

    #1

    printersettings papersources does not return correct values

    I have been trying to figure this out for quite some time and cannot
    find any examples in VB.Net or in VB that work correctly. I am working
    on an application where I want the user to be able to select a peinter
    and printer tray to print reports out. In Crystal Reports 8.5, I can
    select a printer and tray and it prints correctly.
    I did a test report that I use to tell me the value that Crystal
    Reports is using for the paper source. When I use those values in my
    program to initialize Crystal, it will print where I expect it to
    print. So I know Crystal Reports is able to select the correct printer
    and paper source, there are no problems with drivers or anything else,
    it works!
    Next, I get the values for the paper sources from printersettings .
    The values that I'm getting from printersettings are not correct. The
    values are different then what I get using Crystal Reports. I have
    tried this with a number of printers and I get the same result,
    Crystal Reports can get the correct settings but printersettings
    can't!
    I have even tried using the DeviceCapabilit ies function in the
    Windows API and it does not work either. In a lot of instances,
    DeviceCapabilit ies returned different values then the ones I got using
    PrinterSettings .
    A couple of years ago I posted a message on the news groups about
    this problem. At that time I was using an earlier version of VB.Net
    and I could see
    a private implementation field for PaperSource. This field, the "kind"
    field, showed the correct value for the PaperSource. The "Kind" field,
    which is what you can read, gave the wrong value. Microsoft's solution
    was to fix .NET so you would not see the "kind" value. For some reason
    they chose not to give the correct value in the "Kind" field.
    So, I have tried just about everything I could find on the internet
    and I still have not found any code that works correctly. Maybe I'll
    try doing it in C next! At this point I feel I have exhausted all of
    my options with VB and VB.NET.
    Has anyone out there been able to get the correct values for the
    paper source? There has to be a "correct" way to do this because I
    know it works in Crystal Reports and in Microsoft Word.
  • timpub@ohear.com

    #2
    Re: printersettings papersources does not return correct values

    This is a very ugly hack, but you can use reflection to access the
    private "kind" field, as shown in the sample below :

    For each paperSource in thePrinterSetti ngs.PaperSource s
    dim rawKind as Integer
    rawKind = paperSource.Kin d
    If paperSource.Kin d = PaperSourceKind .Custom Then
    rawKind = CInt(paperSourc e.GetType().Get Field("kind",
    Reflection.Bind ingFlags.Instan ce Or
    Reflection.Bind ingFlags.NonPub lic).GetValue(p aperSource))
    End If
    debug.WriteLine (paperSource.So urceName & " = " & rawKind)
    Next paperSource

    Comment

    Working...