cancel
Showing results for 
Search instead for 
Did you mean: 

Attaching document via script

0 Kudos

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

Accepted Solutions (0)

Answers (4)

Answers (4)

0 Kudos

Hello everyone, I found several and different ways for attach a file using VBA, I looked it for inside sap and also in gpt, in all of them I get stuck in this line:

session.FindById("wnd[0]/shellcont/shell").SelectContextMenuItem "PCATTA_CREA"

And this error message:

Runtime error 619 has occurred:

The Control could not be found by id.

Any ideas? Thanks in advance

former_member872221
Discoverer
0 Kudos

Hello, can you remake this into VBA?

Former Member
0 Kudos

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

Former Member
0 Kudos

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:

IfNotIsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
EndIf
IfNotIsObject(connection) Then
   Set connection = application.Children(0)
EndIf
IfNotIsObject(session) Then
   Set session    = connection.Children(0)
EndIf
IfIsObject(WScript) Then
   WScript.ConnectObject session,     "on"
   WScript.ConnectObject application, "on"
EndIf
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)
        DoWhileNot 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))
             EndIf             
             

             session.findById("wnd[0]").maximize
            session.findById("wnd[0]/tbar[0]/okcd").text = "/niw32"
            session.findById("wnd[0]").sendVKey0
            session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum
            session.findById("wnd[0]").sendVKey0
            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]").sendVKey2
            EndIf            
            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 & "."
EndIf

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

Dataload vbs:  dataloadNM.vbs

------

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

WScript.Sleep1000


LoopUntil bWindowFound

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

WScript.Sleep100


Wshell.sendkeys"{TAB}"
Wshell.sendkeys"{TAB}"
Wshell.sendkeys"{TAB}"
Wshell.sendkeys"{TAB}"
WScript.Sleep100


FileNam2 = WScript.Arguments.Item(0)  

Wshell.sendkeys FileNam2

WScript.Sleep100


Wshell.sendkeys"{ENTER}"
WScript.Sleep100
endif

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

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

Former Member
0 Kudos

Hi Script Man

This is hemant. I would like to attach file or document  SAP Document by using excel (SAP Script)

In excel i have two columns 1 for document # & 2 for file name with path "C:\desktop\Scan1.msg

I have such 200 entries with different document # & Different file name path is same but file name get change

Can you please help me with next code after last line. I m getting suck at the last row of the script

Please help

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 = "Fb03"

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

session.findById("wnd[0]/usr/txtRF05L-BELNR").text = "3103195109" (Active Cell Text)

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

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

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

Thanks

----

Former Member
0 Kudos

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