|
-
Dec 26th, 2004, 04:23 AM
#1
Thread Starter
Frenzied Member
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.
-
Dec 26th, 2004, 12:43 PM
#2
Thread Starter
Frenzied Member
Re: Printing Question
BUMP - Just a relevant link would do. I couldn't find one.
Thanks in advance.
.
-
Dec 26th, 2004, 01:13 PM
#3
Re: Printing Question
David, just use the ShellExecute API with the "Print" verb and it will print only
using the associated program.
VB Code:
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_HIDE As Long = 0
Private Sub Command1_Click()
ShellExecute Me.hwnd, "Print", "D:\My Documents\Document1.doc", vbNullString, App.Path, SW_HIDE
End Sub
Private Sub Command2_Click()
ShellExecute Me.hwnd, "Print", "D:\My Documents\Book1.xls", vbNullString, App.Path, SW_HIDE
End Sub
Private Sub Command3_Click()
ShellExecute Me.hwnd, "Print", "D:\My Documents\ReadMe.pdf", vbNullString, App.Path, SW_HIDE
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 26th, 2004, 01:31 PM
#4
Thread Starter
Frenzied Member
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?
-
Dec 26th, 2004, 01:32 PM
#5
Re: Printing Question
RobDog - how would you specify a specific printer - maybe even a network printer with this technique?
-
Dec 26th, 2004, 01:46 PM
#6
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 26th, 2004, 01:51 PM
#7
Re: Printing Question
 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.
-
Dec 26th, 2004, 01:54 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 26th, 2004, 02:02 PM
#9
Thread Starter
Frenzied Member
Re: Printing Question
Many thanks RobDog. One more satisfied customer to add to your extensive collection
-
Dec 26th, 2004, 02:05 PM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|