Solved

Printing from a VB.NET console application

Posted on 2009-05-14
10
1,437 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:LLCurtis
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

11 Experts available now in Live!

Get 1:1 Help Now