Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How to use "Sendkeys" to press ENTER on a Windows dialog?

Hi, everybody.

Our team is working on an SDK add-on which will print a PLD Report linked to the  Inventory Transfer Screen in SAP B1.

We have successfully done the following steps using UI API (i.e ActivateMenuItem)

  • Opened the Inventory Transfer Screen in SAP B1
  • Pressed the Print button in SAP B1 (SBO_Application.ActivateMenuItem("520")

This opens the Windows Dialog. We must now press "ENTER" to print the PLD Report.

How to do this?

-------------

We found the answer here. It is exactly our problem.

How to print a invoice in silent mode

Answer provided by:

@Janos Nagy

It seems to work for the person who asked the question, but it does not work for us.

We have no idea what went wrong. Any suggestions  to help us find what's wrong?

Where must we put this 3rd section in our VB.NET Program?

--------------

' // Now implement the timer eventhandler (thread safe, you can send any keys like ENTER which closes the print dialog box and ESC when you  would like to cancel

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)

        Dim lHwnd As Integer = GetForegroundWindow

        If lCurHwnd = lHwnd Then Exit Sub

        ' Send enter key to Commit the dialog window

        System.Windows.Forms.SendKeys.SendWait("{ENTER}")

        'Stop timer

        _timer.Enabled = False

    End Sub

--------------

Thanks

Leon

Added:

If there is another way to print the PLD Report without popping up the Windows Print Dialog, this will simplify my coding:

I am thinking of the following alternatives:

  • Press the Printer Icon on the toolbar
  • Use shortcut Ctrl + Shift + P

Could anyone give me the SDK coding to effect these (or give me an indication how to do these)

Thanks

Leon

======================================================================

For convenience, we copy the full solution provided by Janos Nagy below

======================================================================

Logic:

You should implement a multi-threaded program, which is looking for the operating system actual window, and compares with the SAP B1 Window ID. If differs, then you can send an enter key. (click on default button).

You can build up thread safe procedure with custom code, but i have used there a timer, which starts when you send the printing, and stops when enter has been sent.

Add the following code to your addon variables declare section:

  Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr

  Dim _timer As New System.Timers.Timer

  Dim lCurHwnd As Integer

When you activating the Print Menu, add the following code

' SAP B1 Window ID via Windows API Call

        lCurHwnd = GetForegroundWindow()

        _timer.Interval = 100

        AddHandler _timer.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.Timer1_Elapsed)

        _timer.Enabled = True

        'RockAndRoll

        SBO_Application.ActivateMenuItem("520")

Now impement the timer eventhandler (thread safe, you can send any keys like ENTER which closes the print dialog box and ESC when you would like to cancel

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)

        Dim lHwnd As Integer = GetForegroundWindow

        If lCurHwnd = lHwnd Then Exit Sub

        ' Send enter key to Commit the dialog window

        System.Windows.Forms.SendKeys.SendWait("{ENTER}")

        'Stop timer

        _timer.Enabled = False

    End Sub

======================================================================

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question