Click here to Skip to main content
15,885,309 members

Key simulation without pressing keys

Member 9655372 asked:

Open original thread
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?
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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