Results 1 to 10 of 10

Thread: Printing Question

  1. #1

    Thread Starter
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Resolved Printing Question

    I have done the searches but as I have never controlled Word or Excell with VB before I need a BASIC explanation for the following if possible.

    I would like to be able to print a word document or an excell document or a PDF from my application programatically without the user having to click on any boxes or popups. The excell spreadsheet would be sheet 1 only.

    Automation is the way I feel but I have no idea on syntax or methods to use.

    Thanks in advance.
    Last edited by David.Poundall; Dec 26th, 2004 at 02:03 PM.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  2. #2

    Thread Starter
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Printing Question

    BUMP - Just a relevant link would do. I couldn't find one.

    Thanks in advance.

    .
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

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

    Re: Printing Question

    David, just use the ShellExecute API with the "Print" verb and it will print only
    using the associated program.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    4. ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
    5. ByVal nShowCmd As Long) As Long
    6.  
    7. Private Const SW_SHOWMAXIMIZED As Long = 3
    8. Private Const SW_SHOWMINIMIZED As Long = 2
    9. Private Const SW_SHOWNORMAL As Long = 1
    10. Private Const SW_HIDE As Long = 0
    11.  
    12. Private Sub Command1_Click()
    13.     ShellExecute Me.hwnd, "Print", "D:\My Documents\Document1.doc", vbNullString, App.Path, SW_HIDE
    14. End Sub
    15.  
    16. Private Sub Command2_Click()
    17.     ShellExecute Me.hwnd, "Print", "D:\My Documents\Book1.xls", vbNullString, App.Path, SW_HIDE
    18. End Sub
    19.  
    20. Private Sub Command3_Click()
    21.     ShellExecute Me.hwnd, "Print", "D:\My Documents\ReadMe.pdf", vbNullString, App.Path, SW_HIDE
    22. End Sub
    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 David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Printing Question

    Thanks for that RobDog. Just what I needed. Quick question though ... how does shell know which app to print with, Word/Excell or PDF ? Is that where App.Path comes in or does the operating system use the default program called by the file association?
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Printing Question

    RobDog - how would you specify a specific printer - maybe even a network printer with this technique?

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

    Re: Printing Question

    Shell uses the default application that is associated with the files extension.
    .doc = MS Word, .xls = MS Excel, etc.

    App.Path is only to specify the working directory. For ex. if you wanted to
    use relative paths for the files but shellexecute a program that is not located
    in the app.path.

    VB Code:
    1. ShellExecute Me.hwnd, "Print", "C:\Windows\Notepad.exe", "Text.txt", App.Path, SW_HIDE
    Text.txt is located in the app.path, but since we specify the working directory
    as app.path it will find the file with no errors.

    szlamany, it uses the default printer. To specify a different printer you would
    need to use the Printer Dialog first to allow the user to select the printer.
    Then use the Printer onbject to actually do the print out using the selected
    device.

    Ps, I think I'm on a roll here!

    HTH
    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

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Printing Question

    Quote Originally Posted by RobDog888
    szlamany, it uses the default printer. To specify a different printer you would
    need to use the Printer Dialog first to allow the user to select the printer.
    Then use the Printer onbject to actually do the print out using the selected
    device.
    Not sure what you mean by using the PRINTER object - are you saying that if I simply set PRINTER=PRINTERS(x) [where x = the printer in the collection that I want] that the SHELLEXECUTE would recognize that my VB app had a different printer? Or would I need to set the default printer first?

    The reason that I ask is that a customer of mine was having a hard time directing output to a specific printer in a .BAT file that was running scheduled - and I thought I might be able to help with this.

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

    Re: Printing Question

    Yes and no, yes because you can set the active printer using something like
    Printer.Name = x, but no because shellexecute only uses the default printer.
    Now if you changed the default printer, saving the original default, to the one
    you want and then shellexecute it would print using the new default printer.
    Then after the job is done, you could revert back to the original printer
    default.
    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

  9. #9

    Thread Starter
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Printing Question

    Many thanks RobDog. One more satisfied customer to add to your extensive collection
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

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

    Re: Printing Question

    Thanks
    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

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