cancel
Showing results for 
Search instead for 
Did you mean: 

Attaching documents in FB03

Former Member
0 Kudos

Hello Dears,

I hope you can help me with my problem.

I am trying to do a VBA macro in Excel and goal is to attach a file in a document number.

I have recorded a script in SAP and edited it, but the pop-up box won't direct to my file path and I have to click the attachment everytime the macro runs.

Here's the script of my macro.

______

Sub Picture1_Click()

'

Dim application

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 = Connect

ion.Children (0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session, "on"

   WScript.ConnectObject application, "on"

End If

LG = 3

Do While Range("A" & LG) > ""

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 = "/nfb03"

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

session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = Range("A" & LG)

session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = Range("B" & LG)

session.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = Range("C" & LG)

session.findById("wnd[0]/usr/txtRF05L-GJAHR").SetFocus

session.findById("wnd[0]/usr/txtRF05L-GJAHR").caretPosition = 4

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"

LG = LG + 1

Loop

MsgBox UCase(Environ("username")) & ": Done attaching."

End Sub

___

I hope you can help me so I won't have to attach manually in SAP.

Thanks!

Accepted Solutions (0)

Answers (1)

Answers (1)

script_man
Active Contributor
0 Kudos

Hi Liliane and welcome in the forum,

If you are using VBA, you would first need to adjust your code a little. I don't know where is the document number, but I suppose as in column D.

for example:

Sub Picture1_Click()

'

Set SapGuiAuto = GetObject("SAPGUI")

Set SapApplication = SapGuiAuto.GetScriptingEngine

Set Connection = SapApplication.Children(0)

Set session = Connection.Children (0)

Set WShell = CreateObject("WScript.Shell")

LG = 3

Do While Range("A" & LG) > ""

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

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nfb03"

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

session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = Range("A" & LG)

session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = Range("B" & LG)

session.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = Range("C" & LG)

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

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

WShell.Run "C:\tmp\Script1.vbs" & " " & Range("D" & LG), 1, False

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

LG = LG + 1

Loop

MsgBox UCase(Environ("username")) & ": Done attaching."

End Sub

Script1.vbs:

Set Wshell = CreateObject("WScript.Shell")

Do

bWindowFound = Wshell.AppActivate("Import file")

WScript.Sleep 1000

Loop Until bWindowFound

bWindowFound = Wshell.AppActivate("Import file")

if (bWindowFound) Then

Wshell.appActivate "Import file"

WScript.Sleep 100

Wshell.sendkeys "{ENTER}"

WScript.Sleep 100

Wshell.sendkeys wscript.arguments(0)

WScript.Sleep 100

Wshell.sendkeys "{ENTER}"

WScript.Sleep 100

end If



Regards,

ScriptMan