cancel
Showing results for 
Search instead for 
Did you mean: 

Send Keys on Windows / SAP / saving pdf file

aleksandra-0601
Explorer
0 Kudos

Hello! I need to save the generated pdf from SAP. My code is doing everything until this moment - then SAP is "giving back" the role to Windows and I cannot do anything (via VBA) with this window

aleksandra0601_0-1708006158712.png

 

Can someone help me?

View Entire Topic
stefan_schnell
Active Contributor

Hello @aleksandra-0601 

try this to handle native Windows Dialogs:

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA"  ( _
  ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function SetForegroundWindow Lib "user32" ( _
  ByVal hwnd As Long) As Long

Dim hwnd As Long

hwnd = FindWindow(vbNullString, "Descarga de archivos")
SetForegroundWindow hwnd
SendKeys %(b)

At first it is necessary to be sure that the dialog window is in foreground. Here we use two Windows API functions, FindWindow and SetForegroundWindow. Now it is possible to use the SendKeys statement with which you can send one or more keystrokes to the active window. In the example above %(b) means Alt is pressing with b to save the document.

Best regards
Stefan

aleksandra-0601
Explorer
0 Kudos

Hello @stefan_schnell, many thanks for answering my question! I think we are almost there, but there is a problem with sendkeys command, VBA considers % as invalid character - compile error

It is the same if I use { } 

stefan_schnell
Active Contributor
0 Kudos

Hello @aleksandra-0601,

sorry, my fault, the keys must be quoted.

SendKeys "%(b)"

Best regards
Stefan

aleksandra-0601
Explorer
0 Kudos

Hello @stefan_schnell, I tried this combination as well. It wont generate the error, but doesn`t save the file. I think the code till this line works fine, because the window seems to be active, but the biggest obstacle is to move it further with any action. I also tried with sendkeys b and tab, to watch if it opens/changes but it didn`t. So I think something is wrong with sendkeys method

stefan_schnell
Active Contributor
0 Kudos

Hello @aleksandra-0601,

I tried it and in my case it works without any problems, here my approach.

At first I execute in one thread the following code:

 

Sub Test()
  Dim result
  result = MsgBox( _
    "Desea abrir o guardar este archivo?", _
    vbAbortRetryIgnore + vbInformation + vbApplicationModal, _
    "Descarga de archivos" _
  )
  Debug.Print result
End Sub

 

This code opens the following dialog:

dialog.png

 

 

 

 

 

Then I use the described approach in another thread:

image002.png

And it works as expected:

image003.png

3 = vbAbort = Abbruch

Maybe you should just try an additional Sleep.

Best regards
Stefan