Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 26-Dec-12 1:35am
v2

1 solution

Try System.Windows.Forms.SendKeys.Send("^(p)")
Not capital P but small p.
That should work :)
 
Share this answer
 
v2
Comments
Member 9655372 26-Dec-12 10:27am    
I have tried this, same error
_Vitor Garcia_ 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900