Finding a specific window on screen (SAP and VBA)

amarokWPcom

New Member
Joined
May 30, 2019
Messages
25
I am working with SAPgui and VBA an so far everything works fine. I update my Item Notes in SAP with VBA and try to attach a file to my SAP document

But in my chain is one missing piece: In the attachment procedure at one point a "select Files:" windows opens which I cannot control (red arrow).

Is there a way to find this window with VBA or another solution to keep my code running? I just need to put the file name of the file to be attached here and choose open.

Over using the path in the Row "Title of Document (green arrow) it is not working.

2020-01-29_14-16-24.png
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
See if this code helps any I don't remember where I got it but it will find windows on screen...

VBA Code:
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
 
Private x As Integer
 
 'Used a user defined type here rather than Enum so that it works on 97
Private Type winEnum
    winHandle As Integer
    winClass As Integer
    winTitle As Integer
    winHandleClass As Integer
    winHandleTitle As Integer
    winHandleClassTitle As Integer
End Type
Dim winOutputType As winEnum
 
Public Sub GetWindows()
    x = 0
    winOutputType.winHandle = 0
    winOutputType.winClass = 1
    winOutputType.winTitle = 2
    winOutputType.winHandleClass = 3
    winOutputType.winHandleTitle = 4
    winOutputType.winHandleClassTitle = 5
     
    GetWinInfo 0&, 0, winOutputType.winHandleClassTitle
End Sub
 
 
Private Sub GetWinInfo(hParent As Long, intOffset As Integer, OutputType As Integer)
     'Sub to recursively obtain window handles, classes and text
     'given a parent window to search
     'Written by Mark Rowlinson
     'www.markrowlinson.co.uk - The Programming Emporium
    Dim hWnd As Long, lngRet As Long, y As Integer
    Dim strText As String
    hWnd = FindWindowEx(hParent, 0&, vbNullString, vbNullString)
    While hWnd <> 0
        Select Case OutputType
        Case winOutputType.winClass
            strText = String$(100, Chr$(0))
            lngRet = GetClassName(hWnd, strText, 100)
            Range("a1").Offset(x, intOffset) = Left$(strText, lngRet)
        Case winOutputType.winHandle
            Range("a1").Offset(x, intOffset) = hWnd
        Case winOutputType.winTitle
            strText = String$(100, Chr$(0))
            lngRet = GetWindowText(hWnd, strText, 100)
            If lngRet > 0 Then
                Range("a1").Offset(x, intOffset) = Left$(strText, lngRet)
            Else
                Range("a1").Offset(x, intOffset) = "N/A"
            End If
        Case winOutputType.winHandleClass
            Range("a1").Offset(x, intOffset) = hWnd
            strText = String$(100, Chr$(0))
            lngRet = GetClassName(hWnd, strText, 100)
            Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
        Case winOutputType.winHandleTitle
            Range("a1").Offset(x, intOffset) = hWnd
            strText = String$(100, Chr$(0))
            lngRet = GetWindowText(hWnd, strText, 100)
            If lngRet > 0 Then
                Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
            Else
                Range("a1").Offset(x, intOffset + 1) = "N/A"
            End If
        Case winOutputType.winHandleClassTitle
            Range("a1").Offset(x, intOffset) = hWnd
            strText = String$(100, Chr$(0))
            lngRet = GetClassName(hWnd, strText, 100)
            Range("a1").Offset(x, intOffset + 1) = Left$(strText, lngRet)
            strText = String$(100, Chr$(0))
            lngRet = GetWindowText(hWnd, strText, 100)
            If lngRet > 0 Then
                Range("a1").Offset(x, intOffset + 2) = Left$(strText, lngRet)
            Else
                Range("a1").Offset(x, intOffset + 2) = "N/A"
            End If
        End Select
         'check for children
        y = x
        Select Case OutputType
        Case Is > 4
            GetWinInfo hWnd, intOffset + 3, OutputType
        Case Is > 2
            GetWinInfo hWnd, intOffset + 2, OutputType
        Case Else
            GetWinInfo hWnd, intOffset + 1, OutputType
        End Select
         'increment by 1 row if no children found
        If y = x Then
            x = x + 1
        End If
         'now get next window
        hWnd = FindWindowEx(hParent, hWnd, vbNullString, vbNullString)
    Wend
     
End Sub
 
Upvote 0
Thank you very much. Now I got further input. Do you know how to control this window or do you know a kind of tutorial?

2020-01-30_06-35-25.png
 
Upvote 0
Here is more info for you which should point you in the right direction for controlling another window with VBA.
 
Upvote 0
Here is more info for you which should point you in the right direction for controlling another window with VBA.
Thank you very much for your tip.

Looks like I made it with this:

VBA Code:
Sub controlstuff()
hwnd = FindWindow(vbNullString, "Select Files:")

Do
DoEvents
hwindow2 = FindWindow(vbNullString, "Select Files:")

Loop Until hwindow2 > 0

file_name_box = FindWindowEx(hwindow2, 0&, "ComboBoxEx32", vbNullString)
ok_button = FindWindowEx(hwindow2, 0&, "Button", "Ö&ffnen")

file_name = Worksheets("Sheet1").Range("B210")


Call SendMessageByString(file_name_box, WM_SETTEXT, 0, file_name)
Call SendMessage(ok_button, BM_CLICK, 0, ByVal 0&)

End Sub
 
Upvote 0
Thanks for the update and excellent work!
 
Upvote 0
Thanks for the update and excellent work!
To be honest, after I had to do some more stuff, but I found it at the same YouTuber. :D

The opening window in SAP to select the file to be attached blocks the VBA code, which I didn't notice before "when nothing was working" ;)

I found the solution in running a VBS script parallel with the VBA.

This script takes over as soon as the window appears, puts in the text in the window which I need (the name of the file to be attached) and sends a strg-v to close the window again.

VBA Code:
Set wshShell = CreateObject("WScript.Shell")

Do
    ret = wshShell.AppActivate("Select Files:") 
Loop until ret = True 

WScript.Sleep 50

ret = wshShell.AppActivate("Select Files:")

if ret = True then
    ret= wshShell.AppActivate("Select Files:")

    WScript.Sleep 50

    wshShell.Sendkeys "TAB 12", True

    WScript.Sleep 50

    wshShell.Sendkeys "^(v)", True

    WScript.Sleep 50

    wshShell.Sendkeys "{enter}"
End if
 
Upvote 0
I found the solution in running a VBS script parallel with the VBA.

Hi, could you please give me more details about running your VBS script parallel with the VBA? I am also trying to access the SAP window in order to attach documents. Thanks!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top