Search code examples
vbasap-gui

IF function depending on Order Type


   Set SapGuiAuto = GetObject("SAPGUI")
   Set SapApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = SapApp.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject SapApp, "on"
End If

Dim SO_ref As String
Dim PO_order As String
Dim ship_cond As String
Dim antal As Integer
Dim i As Integer

antal = Range("F8").Value

Range("E11:E40").Clear

If OPT_Cust1.Value = True Then 
    SO_ref = Range("F2").Value
    PO_order = Range("G2").Value
    ship_cond = Range("H2").Value
    Else
End If

If OPT_Cust2.Value = True Then 
    SO_ref = Range("F3").Value
    PO_order = Range("G3").Value
    ship_cond = Range("H3").Value
End If

If Cust3.Value = True Then 
    SO_ref = Range("F4").Value
    PO_order = Range("G4").Value
    ship_cond = Range("H4").Value
End If

If Cust4.Value = True Then
    SO_ref = Range("F5").Value
    PO_order = Range("G5").Value
    ship_cond = Range("H5").Value
End If

i = 1

Do While i < antal + 1

session.findById("wnd[0]").resizeWorkingPane 160, 38, False
'session.createSession
session.findById("wnd[0]/tbar[0]/okcd").Text = "va01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtVBAK-AUART").Text = "ABC1" ' Order Type
session.findById("wnd[0]/usr/ctxtVBAK-VKORG").Text = "xxxx"
session.findById("wnd[0]/usr/ctxtVBAK-VTWEG").Text = "xx"
session.findById("wnd[0]/usr/ctxtVBAK-SPART").Text = "xx"
session.findById("wnd[0]/usr/ctxtVBAK-VKBUR").Text = "xxxx"
session.findById("wnd[0]/usr/ctxtVBAK-VKGRP").Text = "xxx" 

I got this script for creation of SO "headers". Currently it working fine but I need to add one more customer for which Order type will be different. Say if the Cust4 is selected then the Order Type should be ABC2 otherwise it should always choose ABC1.

How can this function be added to the script?


Solution

  • Try the next approach, please:

    Dim boolType2 as Boolean
    
    '...your existing code
    If Cust4.Value = True Then
        boolType2 = True
        SO_ref = Range("F5").Value
        PO_order = Range("G5").Value
        ship_cond = Range("H5").Value
    End If
    '...your existing code
    session.findById("wnd[0]/usr/ctxtVBAK-AUART").Text = _
                        IIf(boolType2 = True, "ABC2", "ABC1")
    '...your existing code
    

    You can create such boolean variables for many other types, if necessary... IIf supports imbricate functions.

    Edited:

    If your above used controls are check boxes, your code must check if only one of them would be checked. Otherwise, the code will take the values from the last (ticked) one...

    If the check boxes are ActiveX type, you can use each of them Change event, in order to un-tick the others. If not, try adapting your code in the next way, which will do the same thing, but less elegant (on my taste)...

    If OPT_Cust1.Value = True Then
        If OPT_Cust2.Value = True Or _
               Cust3.Value = True Or _
               Cust4.Value = True Then _
            MsgBox "Another check box is also ticked!": Exit Sub
            
        SO_ref = Range("F2").Value
        PO_order = Range("G2").Value
        ship_cond = Range("H2").Value
    ElseIf OPT_Cust2.Value = True Then
        If Cust3.Value = True Or Cust4.Value = True Then _
            MsgBox "Another check box is also ticked!": Exit Sub
            
        SO_ref = Range("F3").Value
        PO_order = Range("G3").Value
        ship_cond = Range("H3").Value
    ElseIf Cust3.Value = True Then
        If Cust4.Value = True Then _
            MsgBox "Cust4 check box is also ticked!": Exit Sub
            
        SO_ref = Range("F4").Value
        PO_order = Range("G4").Value
        ship_cond = Range("H4").Value
    ElseIf Cust4.Value = True Then
        boolType2 = True
        SO_ref = Range("F5").Value
        PO_order = Range("G5").Value
        ship_cond = Range("H5").Value
    End If