Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hello,
 
As a part of my project, I need to print Active application in Vb.net. For example, if web browser is currently active Application then it should print that web page. It can be done by simulating Key Press (Ctrl + P without actually pressing it) or using Print library. This application is triggered by Shortcut using AutoHotKey, So this process runs in background. Here is code..
 Public Class Class1
 
    Public Shared Sub Main()
        Dim filePath As String = "C:\Users\AG\Desktop\SCaptur.png"
 
        CMD.ToPDFCreator()
        CMD.PrintDocument()
        CMD.ChangeBack()
 
    End Sub
 
End Class
Public NotInheritable Class CMD
 
#Region " Methods "
 
    Public Shared Function ToPDFCreator()
        Dim p As Process = New Process()
        Dim pi As ProcessStartInfo = New ProcessStartInfo()
        pi.Arguments = " " + "/C" + " " + "RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n" + " " + "PDFCreator"
        pi.FileName = "cmd.exe"
        p.StartInfo = pi
        p.Start()
    End Function
 
    Public Shared Function PrintDocument()
        System.Windows.Forms.SendKeys.Send("^P")
    End Function
 
    Public Shared Function ChangeBack()
        Dim p As Process = New Process()
        Dim pi As ProcessStartInfo = New ProcessStartInfo()
        pi.Arguments = " " + "/C" + " " + "RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n" + " " + """" + "HP DeskJet" + """"
        pi.FileName = "cmd.exe"
        p.StartInfo = pi
        p.Start()
    End Function
 
#End Region
 
End Class
This code show error:
SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method.
 
Can any one tell me, How to resolve this issue?
Is there any other method to print active application?
Posted 26-Dec-12 1:28am
Edited 26-Dec-12 1:35am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try System.Windows.Forms.SendKeys.Send("^(p)")
Not capital P but small p.
That should work Smile | :)
  Permalink  
v2
Comments
Member 9655372 at 26-Dec-12 10:27am
   
I have tried this, same error
VitorHugoGarcia at 26-Dec-12 10:45am
   
Im guessing you dont have focus on the application you need to do crtl+p. Can you confirm it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 580
1 ashok rathod 159
2 CPallini 140
3 CHill60 130
4 BillWoodruff 125
0 Sergey Alexandrovich Kryukov 9,543
1 OriginalGriff 6,984
2 CPallini 5,820
3 George Jonsson 4,017
4 Gihan Liyanage 3,266


Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100