Skip to Content
avatar image
Former Member

Calling a script from another script

Dear community,

I am trying to implement the solution given by Script Man in this discussion (http://scn.sap.com/message/9593362). For this, I have 2 files, both located in the folder C:\Users\u32q\Desktop.

Main script.vbs:

If Not IsObject(application) Then

     Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.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 application, "on"

End If

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/niw33"

session.findById("wnd[0]").sendVKey 0

OrdNum = "2013584402"

set Wshell = CreateObject("WScript.Shell")

session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum

session.findById("wnd[0]").sendVKey 0

Wshell.run "C:\Users\u32q\Desktop\SecondFile.vbs",1,False

session.findById("wnd[0]/titl/shellcont/shell").pressButton "%GOS_TOOLBOX"

session.findById("wnd[0]/shellcont/shell").pressContextButton "CREATE_ATTA"

session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA"

SecondFile.vbs:

Sub SecondFile

Set Wshell = CreateObject("WScript.Shell")

MsgBox "in sub"

Do

    bWindowFound = Wshell.AppActivate("Importar file")

    ' WScript.Sleep 1000

    wshell.run "C:\Users\u32q\Desktop\sleep_1000.vbs",1,true

Loop Until bWindowFound

Msgbox "Window = 'Import file' found"

bWindowFound = Wshell.AppActivate("Import file")

if (bWindowFound) Then

    Wshell.appActivate "Import file"

    ' WScript.Sleep 100

    wshell.run "C:\Users\u32q\Desktop\sleep_100.vbs",1,true

    Wshell.sendkeys "C:\Users\u32q\Desktop\teste anexo ordem.txt"

    ' WScript.Sleep 100

    wshell.run "C:\Users\u32q\Desktop\sleep_100.vbs",1,true

    Wshell.sendkeys "{ENTER}"

    ' WScript.Sleep 100

    wshell.run "C:\Users\u32q\Desktop\sleep_100.vbs",1,true

end If

End sub

I have executed the main script from both SAP and explorer, but it does not causes the second script to be executed. In fact, I never got the msgbox "in sub".

Am I missing something? Does anyone know what I am doing wrong?

Also, is it possible to implement this solution in vba code, so It can be executed from excel?

Any help would be much appreciated.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Dec 30, 2015 at 08:02 AM

    Very do-able in excel I create and launch the VBS from Excel but you could create another instance of Excel and run the code from there,

    VBS Write and Execute Function

    Function Attach(filename As String)
    Dim Q As Long
    ' i have dropped the lines of code required for this script into the  Hidden Sheet VBS CODE
    'This function builds the VBS script required to attach a file into the Sap window and executes it
    
    
    For Q = 1 To 20
            VBS = VBS & SH_VBS.Cells(Q, 1) & vbCrLf
        Next Q
         
            VBS = VBS & "filnam = " & Chr(34) & "C:\Test\" & filename & Chr(34) & vbCrLf
    
        For Q = 24 To 28
            VBS = VBS & SH_VBS.Cells(Q, 1) & vbCrLf
        Next
           
        'Write and Run Vbs
           
        Set FEXIST = CreateObject("Scripting.FileSystemObject")
    
        'checks to see if there is a folder called C:Test
    
        If FEXIST.FolderExists("C:\Test") = False Then
            FEXIST.CreateFolder ("C:\Test")
        End If
    
    
    
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    
        outFile = "c:\test\Attach.VBS"
    
    
        Set objFile = objFSO.CreateTextFile(outFile, True)
        objFile.Write VBS
        objFile.Close
    
    
        Set objshell = CreateObject("WScript.Shell")
        objshell.Run ("c:\test\attach.vbs")
    
    
    
    End Function
    

    Top Left Cell is SH_VBS.Cells(Q, 1)

    Dim FileNam Set Wshell = CreateObject("WScript.Shell") Dim I I = 1 Do bWindowFound = Wshell.AppActivate("Export File") WScript.Sleep 1000 if I = 35 then exit do I = 1+ I Loop Until bWindowFound WScript.Sleep 4000 Wshell.sendkeys "+{TAB}" wscript.sleep 500 Wshell.sendkeys "+{TAB}" wscript.sleep 500 Wshell.sendkeys "+{TAB}" wscript.sleep 500 Wshell.sendkeys "+{TAB}" wscript.sleep 500 WScript.Sleep 2000 filnam = "C:\MDGT\Help\MDGT General.txt" for i = 1 to len(filnam) Wshell.sendkeys mid(filnam,I,1) wscript.sleep 200 next Wshell.sendkeys "{ENTER}"

    Section in Main Code

    Call Attach ("NameOfFileToExport.PDF")
    
    Session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarButton "%ATTA_EXPORT"
    

    Line 3 is where the main program hangs until the pop up box has been closed

    i'm using native windows boxes

    I could get your second VBS file to work by removing the sub end sub tags that might be all you need

    Add comment
    10|10000 characters needed characters exceeded

  • Dec 11, 2015 at 08:54 AM

    Hi Renan,

    welcome on board. I think you can omit the workaround in this case.

    for example:

    If Not IsObject(application) Then

         Set SapGuiAuto  = GetObject("SAPGUI")

       Set application = SapGuiAuto.GetScriptingEngine

    End If

    If Not IsObject(connection) Then

       Set connection = application.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 application, "on"

    End If

    session.findById("wnd[0]").maximize

    session.findById("wnd[0]/tbar[0]/okcd").text = "/niw33"

    session.findById("wnd[0]").sendVKey 0

    OrdNum = "2013584402"

    set Wshell = CreateObject("WScript.Shell")

    session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum

    session.findById("wnd[0]").sendVKey 0

    'Wshell.run "C:\Users\u32q\Desktop\SecondFile.vbs",1,False

    session.findById("wnd[0]/titl/shellcont/shell").pressButton "%GOS_TOOLBOX"

    session.findById("wnd[0]/shellcont/shell").pressContextButton "CREATE_ATTA"

    session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA"

    session.findById("wnd[1]/usr/ctxtDY_PATH").text = ""

    session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "C:\Users\u32q\Desktop\teste anexo ordem.txt"

    session.findById("wnd[1]/tbar[0]/btn[0]").press

    session.findById("wnd[0]/shellcont").close

    Regards,

    ScriptMan

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Script Man

      Script Man,

      I have checked again: the option show native windows dialog is disabled in my system too.

      When I execute the script from SAP, the problem happens as I described before.

      When I double click the vbs file directly from desktop, the problem is different: the Windows Script Host says that the object WScript could not be connected (error code 80020009). This error is originated from command WScript.CreateObject.

      I do not have administrator privileges on my machine. Could this be the problem?