Results 1 to 18 of 18

Thread: [RESOLVED] Problem in sending the job to printer through API

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Resolved [RESOLVED] Problem in sending the job to printer through API

    Hi all,

    My default printer is kyosera 1000. I use following code to select the printer from a print dialog in external application:
    VB Code:
    1. hMain = FindWindow("#32770", "Print")
    2. hChild = FindWindowEx(hMain, 0, "ComboBox", vbNullString)
    3. nret = SendMessage(hChild, CB_FINDSTRINGEXACT, 0, "Kyosera 2500")
    4. If nret <> -1 Then
    5.     'To select the printer in combo
    6.     Call SendMessage(hChild, CB_SETCURSEL, nret, 0)
    7. End If
    The above code selects Kyosera 2500 without any issues. but prints on Kyosera 1000 which is my default printer.

    post me your suggestions.
    Last edited by cssriraman; May 6th, 2006 at 01:35 PM.
    CS

  2. #2

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem when selecting a printer through API

    Actually I want to print to two printers continuously one after another. First one to Kyosera 1000 next Kyosera 2500. this happens to all printers.

    I can select printers using the code i posted #1 but if i send message to click OK button (or even i tried to click manually) it prints to previous printer (first one).

    I heared that I need to notify the parent window (Print dialog) using CBN_SELCHANGE. is it true? if so, how to do that?

    Any of you faced these kind of problems?

    Please help me on this.
    CS

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problem in sending the job to printer through API

    I tried using CB_SETCURSEL before and had the same issue. It was selected but really wasnt selected. The CBN_SELCHANGE should work but I havent used it.

    I used a different approach. I used the WM_KEYDOWN and WM_KEYUP with the SendMessage API in a loop after finding out which index was currently selected with the CB_GETCURSEL message. The return is the selected index and I would SendMessage with the WM_KEYUP or WM_KEYDOWN depending on the position of the selection. You will need to pass the VK_UP or VK_KEYDOWN as the wParam parameter with the WM_KEYDOWN or UP.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    Thanks Rob! I will try and get back to you shortly.

    Anyway, thanks a lot for your idea.
    CS

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problem in sending the job to printer through API

    Yes, I wish I found CBN_SELCHANGE before going the other route.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    Yes. We should find the simplest way instead of sending keys and checking its index.

    I searched this forum as well as google a lot. but nothing found.
    CS

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem in sending the job to printer through API

    I have no doubt that sending WM_KEYDOWN/UP will work, but that could require a fair amount of calls. As you've already mentioned you should send CBN_SELCHANGE to the main window after you've made the selection. The CBN_SELCHANGE is sent as part of a WM_COMMAND message.
    VB Code:
    1. hMain = FindWindow("#32770", "Print")
    2. hChild = FindWindowEx(hMain, 0, "ComboBox", vbNullString)
    3. nret = SendMessage(hChild, CB_FINDSTRINGEXACT, 0, "Kyosera 2500")
    4. If nret <> -1 Then
    5.     'To select the printer in combo
    6.     Call SendMessage(hChild, CB_SETCURSEL, nret, 0)
    7.     'CBN_SELCHANGE is sent as the high order word of wParam. The low order word
    8.     'is supposed to have the identifier of the item. For a combo box I have never seen
    9.     'anything but 1 as the identifier.
    10.     Call SendMessage(hMain, WM_COMMAND, (CBN_SELCHANGE * &H10000& + 1&), hChild)
    11. End If

  8. #8

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    No! Joacim Andersson. It selects "Kyosera 2500" and automatically printing to "Kyosera 1000".

    I am not sure "CBN_SELCHANGE" will work for me. my friends told me that I have to use that. That's what I know.
    CS

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem in sending the job to printer through API

    Well, I can't test the code since on XP the Print dialog box doesn't show a combobox for the different printers but a ListView instead. But the above is how the CBN_SELCHANGE notification should be sent.

  10. #10

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    Joacim,

    The word print dialog box will have similar Print dialog box with combo box to select printer. You can test it with Word Print dialog. (FYI: I am not automating Word.)

    Thanks.
    CS

  11. #11

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    But in word I am unable to get handle of combo box. my third party application will have similar print dialog with combo box but we can find the handle using Findwindowx. I will try to give some other example applications.
    CS

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem in sending the job to printer through API

    Yeah, the Word print dialog doesn't seem to use a regular combo box at all. When I enumerate the child windows of the Word print dialog all I get is a bunch of RichEdit20W classes... Have you tried using Spy++ and monitor the WM_COMMAND message of the Print dialog in the application to see if it sends the CBN_SELCHANGE notification to the dialog window when you do a manual selection?

  13. #13

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    Hi,

    Here is the screen shot of WM_command messages, when we change the printer manually:

    I have changed the default printer to my desired printer.
    Attached Images Attached Images  
    CS

  14. #14

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    And I also tested, If i change the printer through code, there is no WM_COMMAND message.
    CS

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem in sending the job to printer through API

    It looks like the ID for the combo box is 101 and not 1 as it was when I used a VB combobox to do the testing. Also, you might need to send the CBN_SELENDOK notification before the CBN_SELCHANGE. You can use the code I posted earlier to do that, but change (CBN_SELCHANGE * &H10000& + 1&) to (CBN_SELCHANGE * &H10000& + 101&). That would give the following code (assuming you have all the constants declared already).
    VB Code:
    1. hMain = FindWindow("#32770", "Print")
    2. hChild = FindWindowEx(hMain, 0, "ComboBox", vbNullString)
    3. nret = SendMessage(hChild, CB_FINDSTRINGEXACT, 0, "Kyosera 2500")
    4. If nret <> -1 Then
    5.     'To select the printer in combo
    6.     Call SendMessage(hChild, CB_SETCURSEL, nret, 0)
    7.     'Notification messages is sent in the high word of wParam
    8.     Call SendMessage(hMain, WM_COMMAND, (CBN_SELENDOK * &H10000& + 101&), hChild)
    9.     Call SendMessage(hMain, WM_COMMAND, (CBN_SELCHANGE * &H10000& + 101&), hChild)
    10. End If

  16. #16

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Problem in sending the job to printer through API

    Hi Joacim,

    Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Many Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks Thanks

    Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot! Thanks a Lot!

    It works fantastically. I have no words how can I express my happiness.

    Thanks a lot. Great help.
    CS

  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem in sending the job to printer through API

    Quote Originally Posted by cssriraman
    I have no words how can I express my happiness.
    Hmmm... Well allow me to give you a little hint: ()

  18. #18
    New Member
    Join Date
    Jul 2012
    Location
    Mumbai
    Posts
    1

    Re: [RESOLVED] Problem in sending the job to printer through API

    Hi Joacim,

    Your are great. It actually resolved one of my biggest issues.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width