Skip to Content

Attaching document via script

Related to my last question...

We attach scanned images to many of our SAP documents via a script.  We know the path name and the filename of the attachment, but we still have to manually find the document in a browse window and confirm it.  Is it possible to have the script attach the scanned image to a given SAP document directly in the script with no user interaction?

Also, is it possible to download all the attachments from a given SAP document to our file server without user interaction?

We are using VBA scripting in Excel for these scripts.

Thanks,

Tyler

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Oct 14, 2014 at 05:42 PM

    Hi Tyler - we used the following method to upload attachments to work orders in the past.

    Step 1: create a text file that contains 2 columns: Order number and Document filename

    Step 2: use the script below to read this file line by line and get the filename and order number. Uses the order change transaction, goes to the screen where you click append attachment

    Step 3: spawns another vbs script from this one to paste the filename and clicks OPEN

    Step 4: saves the order and continues with the nex order- filename set...

    This works well but there are a few moving parts: Main vbs, Input file, Upload vbs.

    I attached all these as samples.. you can tweak and adjust as needed. There might be better ways but this works for us.(thanks to Scriptman's idea for this approach)

    Main:

    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
    Const TristateFalse = 0
    Const ForReading = 1
    Const FILE_NAME = "C:\tmp\OrderScanNM.txt"
    Dim FSO
    Dim objStream
    Dim objFile
    '--------------------
    Dim OrdNum
    Dim FileName
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(FILE_NAME) Then
             Set objStream = FSO.OpenTextFile(FILE_NAME, _
                 ForReading, False, TristateFalse)
            Do While Not objStream.AtEndOfStream
                 strLine = objStream.ReadLine
                 tx = Left(strLine,8)
                 If tx = " " Then
                    OrdNum = Left(strLine,7)
                    FileName = "C:\tmp\" & Right(strLine, (Len(strLine) - 8))

                 Else
                    OrdNum = Left(strLine,8)
                    FileName = "C:\tmp\" & Right(strLine, (Len(strLine) - 9))
                 End If             
                 

                 session.findById("wnd[0]").maximize
                session.findById("wnd[0]/tbar[0]/okcd").text = "/niw32"
                session.findById("wnd[0]").sendVKey 0
                session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum
                session.findById("wnd[0]").sendVKey 0
                Set Wshell = CreateObject("WScript.Shell")
                Wshell.run "c:\tmp\dataloadNM.vbs " & FileName,1,False
                session.findById("wnd[0]/titl/shellcont/shell").pressButton "%GOS_TOOLBOX"
                If session.ActiveWindow.Name = "wnd[1]" Then 
                    session.findById("wnd[1]/usr/tblSAPLSWUGOBJECT_CONTROL/txtSWLOBJTDYN-DESCRIPT[0,0]").setFocus
                    session.findById("wnd[1]/usr/tblSAPLSWUGOBJECT_CONTROL/txtSWLOBJTDYN-DESCRIPT[0,0]").caretPosition = 10
                    session.findById("wnd[1]").sendVKey 2
                End If            
                session.findById("wnd[0]/shellcont/shell").pressContextButton "CREATE_ATTA"
                session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA"
                session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/subSUB_KOPF:SAPLCOIH:1102/btn%#AUTOTEXT001").press
                session.findById("wnd[1]/usr/sub:SAPLBSVA:0201[1]/chkJ_STMAINT-ANWSO[0,0]").selected = true
                session.findById("wnd[1]/usr/sub:SAPLBSVA:0201[1]/chkJ_STMAINT-ANWSO[0,0]").setFocus
                session.findById("wnd[1]/tbar[0]/btn[0]").press
                session.findById("wnd[0]/tbar[0]/btn[11]").press
                session.findById("wnd[0]/tbar[0]/btn[15]").press
            Loop
        objStream.Close
        objFile.Close
    Else
             MsgBox "Could not find input file " & FILE_NAME & "."
    End If

    --------------------

    Dataload vbs:  dataloadNM.vbs

    ------

    Dim FileNam2
    Set Wshell = CreateObject("WScript.Shell")
    Do 
    bWindowFound = Wshell.AppActivate("Open") 

    WScript.Sleep 1000


    Loop Until bWindowFound

    bWindowFound = Wshell.AppActivate("Open") 
    if (bWindowFound) Then
    Wshell.appActivate "Open"

    WScript.Sleep 100


    Wshell.sendkeys "{TAB}"
    Wshell.sendkeys "{TAB}"
    Wshell.sendkeys "{TAB}"
    Wshell.sendkeys "{TAB}"
    WScript.Sleep 100


    FileNam2 = WScript.Arguments.Item(0)  

    Wshell.sendkeys FileNam2

    WScript.Sleep 100


    Wshell.sendkeys "{ENTER}"
    WScript.Sleep 100
    end if

    -----------------

    data file is something like this (single space between order number and attachment filename) - OrderScanNM.txt file:

    9533711 filename.xls

    9533731 filename2.xls

    etc. etc....

    Good luck

    Regards

    Umur

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi I am trying this code as per my requirement which is attaching the doc to material number.

      but it is not working .Please help

      The Material has been successfully read from the input file but not attaching the document.

      When the main file runs it goes to the import screen and wait for the file to be selected manually.

      But if we select then it throws error

      line      54

      char      5

      error    " object required "

      code    800A01a8

      Source Microsoft VB script runtime error

      My Main file code is

         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

      Const TristateFalse = 0

      Const ForReading = 1

      Const FILE_NAME = "C:\Temp\OrderScanNM.txt"

      Dim FSO

      Dim objStream

      Dim objFile

      '--------------------

      Dim OrdNum

      Dim FileName

      Set FSO = CreateObject("Scripting.FileSystemObject")

      If FSO.FileExists(FILE_NAME) Then

               Set objStream = FSO.OpenTextFile(FILE_NAME, _

                   ForReading, False, TristateFalse)

              Do While Not objStream.AtEndOfStream

                   strLine = objStream.ReadLine

                   tx = Left(strLine,8)

                   If tx = " " Then

                      OrdNum = Left(strLine,7)

                      FileName = "C:\Temp\" & Right(strLine, (Len(strLine) - 8))

                   Else

                      OrdNum = Left(strLine,8)

                      FileName = "C:\Temp\" & Right(strLine, (Len(strLine) - 9))

                   End If           

                 

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

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

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

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

      session.findById("wnd[0]/usr/ctxtRMMW1-MATNR").caretPosition = 5

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

        Set Wshell = CreateObject("WScript.Shell")

              Wshell.run "C:\Temp\Upload.vbs " & FileName,1,False

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

      session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"

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

           Loop

          objStream.Close

          objFile.Close

      Else

               MsgBox "Could not find input file " & FILE_NAME & "."

      End If

      --------------------------------------------------------------

      Input File

      11214   Attach.txt

      ----------------------------------------------------------------

      upload.vbs

      Dim FileNam2

      Set Wshell = CreateObject("WScript.Shell")

      Do

      bWindowFound = Wshell.AppActivate("Open")

      WScript.Sleep 1000

      Loop Until bWindowFound

      bWindowFound = Wshell.AppActivate("Open")

      if (bWindowFound) Then

      Wshell.appActivate "Open"

      WScript.Sleep 100

      Wshell.sendkeys "{TAB}"

      Wshell.sendkeys "{TAB}"

      Wshell.sendkeys "{TAB}"

      Wshell.sendkeys "{TAB}"

      WScript.Sleep 100

      FileNam2 = WScript.Arguments.Item(0)

      Wshell.sendkeys FileNam2

      WScript.Sleep 100

      Wshell.sendkeys "{ENTER}"

      WScript.Sleep 100

      end if

  • avatar image
    Former Member
    Aug 03, 2016 at 05:12 AM

    Hi I am trying this code as per my requirement which is attaching the doc to material number.

    but it is not working .Please help

    The Material has been successfully read from the input file but not attaching the document.

    When the main file runs it goes to the import screen and wait for the file to be selected manually.

    But if we select then it throws error

    line      54

    char      5

    error    " object required "

    code    800A01a8

    Source Microsoft VB script runtime error

    My Main file code is

       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

    Const TristateFalse = 0

    Const ForReading = 1

    Const FILE_NAME = "C:\Temp\OrderScanNM.txt"

    Dim FSO

    Dim objStream

    Dim objFile

    '--------------------

    Dim OrdNum

    Dim FileName

    Set FSO = CreateObject("Scripting.FileSystemObject")

    If FSO.FileExists(FILE_NAME) Then

             Set objStream = FSO.OpenTextFile(FILE_NAME, _

                 ForReading, False, TristateFalse)

            Do While Not objStream.AtEndOfStream

                 strLine = objStream.ReadLine

                 tx = Left(strLine,8)

                 If tx = " " Then

                    OrdNum = Left(strLine,7)

                    FileName = "C:\Temp\" & Right(strLine, (Len(strLine) - 8))

                 Else

                    OrdNum = Left(strLine,8)

                    FileName = "C:\Temp\" & Right(strLine, (Len(strLine) - 9))

                 End If            

                

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

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

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

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

    session.findById("wnd[0]/usr/ctxtRMMW1-MATNR").caretPosition = 5

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

      Set Wshell = CreateObject("WScript.Shell")

            Wshell.run "C:\Temp\Upload.vbs " & FileName,1,False

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

    session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"

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

         Loop

        objStream.Close

        objFile.Close

    Else

             MsgBox "Could not find input file " & FILE_NAME & "."

    End If

    --------------------------------------------------------------

    Input File

    11214   Attach.txt

    ----------------------------------------------------------------

    upload.vbs

    Dim FileNam2

    Set Wshell = CreateObject("WScript.Shell")

    Do

    bWindowFound = Wshell.AppActivate("Open")

    WScript.Sleep 1000

    Loop Until bWindowFound

    bWindowFound = Wshell.AppActivate("Open")

    if (bWindowFound) Then

    Wshell.appActivate "Open"

    WScript.Sleep 100

    Wshell.sendkeys "{TAB}"

    Wshell.sendkeys "{TAB}"

    Wshell.sendkeys "{TAB}"

    Wshell.sendkeys "{TAB}"

    WScript.Sleep 100

    FileNam2 = WScript.Arguments.Item(0) 

    Wshell.sendkeys FileNam2

    WScript.Sleep 100

    Wshell.sendkeys "{ENTER}"

    WScript.Sleep 100

    end if

    Add comment
    10|10000 characters needed characters exceeded