Solved

Printing from a VB.NET console application

Posted on 2009-05-14
10
1,447 Views
Last Modified: 2012-05-07
Can anybody tell me how to open a window, given a URL, and then print the contents of that window to the default printer from a VB.NET console application?  I have been able to open, edit and print a word document and I have also been able to open a window and display a URL, but I can't figure out how to print the contents of that window once I get it opened.  Perhaps I am using the wrong method for opening the window and supplying the URL.  Any help would be greatly appreciated.  THanks.
0
Comment
Question by:LLCurtis
  • 5
  • 5
10 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24385226
What window are you opening? IE? How are you supplying the URL? You could possible use SendKeys to first send the CTRL + P and then {Enter}.
0
 

Author Comment

by:LLCurtis
ID: 24388053
For complete lack of knowledge, I used the following command because it worked . . . in that it opened the window and I can see the page that I want to print (the Loandata array contains the URL).  I can subsequently choose File/Print from the IE menu manually and get the output to the printer that I desire.  I am sure that there are other ways to open the window and display the URL.  Perhaps one or more of these would be more conducive to subsequent printing.

System.Diagnostics.Process.Start("IExplore.exe", LoanData(i, 10))
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 24390295
After executing this statement, you could insert some delay waiting for Internet Explorer to load (its damn slow). Use this statement to pause the current thread
System.Threading.Thread.Sleep(2000)
 
Then following command would bring up the print dialog (given that IE has the focus)
SendKeys.Send("^p")

Then
SendKeys.Send("{Enter}")
would have the effect of pressing the ok button on print dialog.
0
 

Author Comment

by:LLCurtis
ID: 24394424
I think you may be on the right track.  The timing is a pain, but I'm not sure that it is a big concern here since the documents that I will be printing will take perhaps 30 seconds to come off the printer anyway so if the printing process is slow it should be no big deal.  The issue I'm having now, however, is that Sendkeys requires an object and I don't know how to initialize an object for it to use.  If I just define an object and use that, I get the standard "Object variable or With block variable not set" error.  Also, reading about the Sendkeys.Send method, I see it is part of the System.Windows.Form namespace.  Will that be a problem since this application is running from a console application rather that a Windows application?  If so, I may be able to simply convert it to a Windows application and place the code into the Open_Form event.  THanks in advance for your continued efforts to solve this problem for me.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 24394499
>The issue I'm having now, however, is that Sendkeys requires an object and I don't know how to initialize an object for it to use.

I do not understand what you mean. The SendKeys.Send is a shared method which means you could access it without creating any object.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:LLCurtis
ID: 24394912
Is that true even in a console application because if I remove an object from in front of it (e.g. objIE.SendKeys.Send("^p")), then I get a compile error "name Sendkeys is not declared".
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 24394953
Being a console application is the difference i think. In WinForms, it could be used without object. But using the object is a good point because the keys would be sent to that object i think. Try this
System.Windows.Forms.SendKeys.Send("")
0
 

Author Comment

by:LLCurtis
ID: 24395326
Well, again I think you/we are getting closer.  This time it gave an error saying that Sendkeys could not run inside this application because the application is not handling Windows messages.  It suggested that I either change the application to handle messages or use SendWait method instead of Send.  I chose the latter (since THAT I knew how to do) and it ran without erors . . . however, nothing came out of the printer.  I decided to set a breakpoint on the "send enter key" line and when I did that, I could see the print dialog box come up (perhaps I need to put another SLEEP even after the "^p"?), but when I single-stepped the "press ENTER" line it did not go away nor did it print.  When I pressed the ENTER key on the keyboard (after giving that window focus), it DID print.  Could there be something wrong with my "press ENTER" line?  I looked it up on MSDN and I think I have it coded exactly as they did there.

                System.Windows.Forms.SendKeys.SendWait("^p")
                System.Windows.Forms.SendKeys.SendWait("{ENTER}")
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 24395495
I think you should make the thread sleep for some time after send the ^p command because IE takes some time to show the print dialog.
0
 

Author Closing Comment

by:LLCurtis
ID: 31581414
With all due respect, it "slept" quite a while when I was sitting at the breakpoint so I didn't think that timing was the issue in this case.  That having been said, I placed a loooong sleep between the two commands anywas just to verify my thinking . . .  and it worked!  I guess we're not supposed to be able to explain EVERYTHING.  THanks for all of your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ADO.NET ENTITY DATA MODEL 3 32
Connection String 16 43
Advice on Xojo as a development tool over VB. 4 40
Advice in Xamarin 21 57
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now