Click here to Skip to main content
13,554,611 members
Rate this:
 
Please Sign up or sign in to 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 26-Dec-12 1:28am
Updated 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 :)
  Permalink  
v2
Comments
User-9617189 26-Dec-12 10:27am
   
I have tried this, same error
_VitorGarcia_ 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
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03-2016 | 2.8.180515.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2018
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