cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Activating the SAP window

Former Member
0 Likes
8,134

Hi All,

I have a script which I run from Excel and it works fine... it simply opens a work order, the only problem is that the SAP window is not activated?

The line Session.findByID("wnd[0]").maximize  doesn't display the SAP window?

I can Alt Tab to the window and it showing what I want but how do I do this as part of the macro.

What am I missing here?

Regards

Steve Bayliss

View Entire Topic
holger_khn
Contributor
0 Likes

Hello.

Setup two functions. ActivateWindow and DeActivateWindow:


Public Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, _
                   ByVal lpWindowName As String) As Long
                  
Public Declare Function GetWindowPlacement Lib "user32" _
    (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
   
Public Declare Function SetWindowPlacement Lib "user32" _
    (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
   
Public Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As Long) As Long
   
Public Declare Function GetForegroundWindow Lib "user32" () As Long

Public Declare Function BringWindowToTop Lib "user32" _
    (ByVal hwnd As Long) As Long   

Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2

Public Type POINTAPI
    X As Long
    Y As Long
End Type

Public Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Public Type WINDOWPLACEMENT
    Length As Long
    flags As Long
    showCmd As Long
    ptMinPosition As POINTAPI
    ptMaxPosition As POINTAPI
    rcNormalPosition As RECT
End Type

Public Function ActivateWindow(xhWnd&) As Boolean

    Dim Result&, WndPlcmt As WINDOWPLACEMENT
 
    With WndPlcmt
   
        .Length = Len(WndPlcmt)
        Result = GetWindowPlacement(xhWnd, WndPlcmt)
       
        If Result Then
       
            If .showCmd = SW_SHOWMINIMIZED Then
                .flags = 0
                .showCmd = SW_SHOWNORMAL
                Result = SetWindowPlacement(xhWnd, WndPlcmt)
              Else
                Call SetForegroundWindow(xhWnd)
                Result = BringWindowToTop(xhWnd)
            End If
           
            If Result Then ActivateWindow = True
           
        End If
       
    End With
   
  End Function

Public Function DeActivateWindow(xhWnd&) As Boolean

    Dim Result&, WndPlcmt As WINDOWPLACEMENT
 
    With WndPlcmt
   
        .Length = Len(WndPlcmt)
        Result = GetWindowPlacement(xhWnd, WndPlcmt)
       
        If Result Then
                .flags = 0
                .showCmd = SW_SHOWMINIMIZED
                Result = SetWindowPlacement(xhWnd, WndPlcmt)
                If Result Then DeActivateWindow = True
        End If
       
    End With
   
End Function

Then you can set Windows in fore- and/or background.


SessionHWND = Session.FindById("wnd[0]").Handle
ActivateWindow (SessionHWND)

'Start of your code
      'Your code
'End of your code

DeActivateWindow (SessionHWND)
ActivateWindow (Application.hwnd) 'ExcelWBInFront

Best regards

Holger

Former Member
0 Likes

Holger,

Thank you so much for your response, very much appreciated.

I will give that a go as soon as I can.

Regards

Steve.

Stefan-Schnell
Active Contributor
0 Likes

Hello Holger,

cool code, thanks for that.

Cheers

Stefan