Printing from a VB.NET console application

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.
LLCurtisAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
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
 
LLCurtisAuthor Commented:
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
 
CodeCruiserConnect With a Mentor Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LLCurtisAuthor Commented:
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
 
CodeCruiserConnect With a Mentor Commented:
>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
 
LLCurtisAuthor Commented:
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
 
CodeCruiserConnect With a Mentor Commented:
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
 
LLCurtisAuthor Commented:
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
 
CodeCruiserConnect With a Mentor Commented:
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
 
LLCurtisAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.