cancel
Showing results for 
Search instead for 
Did you mean: 

FB03 Exporting of attachment

Former Member
0 Kudos

Hi guys!

I am currently developing a macro tool wherein I can export the attachment of a document using FB03 and saving it in a specific folder.

I was able to create one but I have the following problem:

> I don't know why there is an error with the path of the file whenever I try to link specific paths to a cell from my excel. But when I input the path directly to the VBA, it works. (hope this question was clear. )

> I am a VBA beginner so I would like to loop this macro and I want it to perform the export for a list of documents and not just one.

> Is it possible for me to insert the exported file into the excel sheet next to the document number?

Hope I was clear with what intend to do...Please help.

THANK YOU SO MUCH!!

Below are the details that I have in my Excel sheet

ABC
1Destination FileC:\Users\LIX2628\Desktop\PUll outs export\
2 Document NumberCo. CodeYear
1200072996u0012014

And below is the script that I used.

Sub FB03export()

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 = Connection.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session, "on"

   WScript.ConnectObject application, "on"

End If

session.findById("wnd[0]").resizeWorkingPane 196, 15, False

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

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

session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = Cells(3, 2).Value

session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = Cells(3, 3).Value

session.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = Cells(3, 4).Value

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_VIEW_ATTA"

session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").currentCellColumn = "BITM_DESCR"

session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"

session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").contextMenu

session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectContextMenuItem "%BDS_START_BDN"

session.findById("wnd[0]/shellcont[1]/shell").selectedNode = "Doc-00000001"

session.findById("wnd[0]/mbar/menu[0]/menu[6]").Select

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = "C:\Users\LIX2628\Desktop\PUll outs export\"

session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").caretPosition = 42

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

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

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

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

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

End Sub

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi People

I have the same problem, i can't open the window explorer for atachment from fb03 with VBA, I have been reading ABAP resource but is not working yet.

Someone can reply with code? Or is not possible, thanks

Bst Regardings

Lucas

Former Member
0 Kudos

The popup to save the file is a Windows popup and can't be recorded with SAP Recorder. You'll have to use a VBscript instead (see example above).

In your VBA code you should call the VBscript just before VBA gets stuck:

Set wshShell = CreateObject("Wscript.Shell")

wshShell.Run """" & sVBSdir & """ " & """" & sFile & """"

sVBSdir = the directory of the VBscript (eg. "C:\Projects\myVBS.vbs")

sFile = directory of the file you want to save (eg. "C:\Projects\Output\myPDF.pdf")

Former Member
0 Kudos

Hi Good Day,

Can  you please help me, i have been created this vba code below, but i can't finish with the code that you sent me. Thank for your attention.

Sub Attach_DOC_FB03()

If Not IsObject(application1) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set application1 = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = application1.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 application1, "on"
End If

Dim a As Integer
a = 2
Do While Not Range("a" & a) = ""
On Error Resume Next

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/txtRF05A-BELNS").Text = Range("b" & a) & ""
'session.findById("wnd[0]/usr/txtRF05A-GJAHS").Text = Range("b" & a) & ""
'session.findById("wnd[0]/usr/txtBSIS-MONAT").Text = Range("d" & a) & ""
session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = "br04"
session.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = "2016"
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"


"Doens't works from here"

    bWindowFound = Wshell.AppActivate("Import file")

    WScript.Sleep 1000

Loop Until bWindowFound

bWindowFound = Wshell.AppActivate("Import file")

If (bWindowFound) Then

    Wshell.AppActivate "Import file"

    Wshell.SendKeys "{TAB}"

    Wshell.SendKeys "{TAB}"

    Wshell.SendKeys "{TAB}"

    Wshell.SendKeys "{TAB}"

    Wshell.SendKeys "{TAB}"

    Wshell.SendKeys fileAdd

    Wshell.SendKeys "{ENTER}"

End If

'session.findById("wnd[0]").sendVKey 0
'session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = Range("a" & a) & ""
'session.findById("wnd[0]/sbar").DoubleClick
'Range("d" & a) = session.findById("wnd[0]/sbar").Text

a = a + 1

Loop

End Sub

Former Member
0 Kudos

Hi Vedette,

I would like to ask you if you have been successful to complete your coding for this? I too have the same issue. Could you please help with the coding?

holger_khn
Contributor
0 Kudos

Hello.

May we can Support you on this when we know how far you are with your own code and what is your issue currently.

Former Member
0 Kudos

hello Holger, I hope you can help me with this dilemma Having the same problem too. I have the filepath inside my excel sheet. I got everything up and running (validations, loop, etc..) I just can't figure out how to do a code that will copy the path from my excel sheet, then paste it in the dialogbox in SAP

Public Sub AttachNow()

doc = ActiveCell.Value

attachment = ActiveCell.Offset(0, 1).Value 'this is the cell value where the filepath is copied

salesorg = Range("F3").Value

    Dim current As Workbook

    Set current = ActiveWorkbook

    Dim ARlayout As String

    Dim objExcel

    Dim objSheet, intRow, i

    Set objExcel = GetObject(, "Excel.Application")

    Set objBook = objExcel.ActiveWorkbook

  

Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object

Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI

Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected

Set session = SAPCon.Children(0) 'Get the first session (window) on that connection

  

'Start the transaction to access FB03

  

session.findById("wnd[0]").resizeWorkingPane 235, 41, False

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

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

session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = doc

session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = salesorg

session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").SetFocus

session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").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"

'put code to paste path in dialog box here

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

End Sub

Former Member
0 Kudos

Hi David,


Is this a Save As popup box? In that case, I think you should use a VB Script to enter the directory and press ok/save.


The VB Script should do the following:

1. Find the Save As window (API)

2. Bring Save As screen to forefront

3. Enter the directory

4. Press Enter

Number 2 and 3 can be done by sendkey TAB

Here is an example of a script I use elsewhere for something similar:


Set Wshell = CreateObject("WScript.Shell")

fileAdd = WScript.Arguments(0)

Do

    bWindowFound = Wshell.AppActivate("Import file")

    WScript.Sleep 1000

Loop Until bWindowFound

bWindowFound = Wshell.AppActivate("Import file")

If (bWindowFound) Then

    Wshell.appActivate "Import file"

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys fileAdd

    Wshell.Sendkeys "{ENTER}"

End If

do

    bWindowFound = Wshell.AppActivate("SAP GUI Security")

    WScript.Sleep 1000

loop Until bWindowFound

bWindowFound = Wshell.AppActivate("SAP GUI Security")

if (bWindowFound) then

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys "{TAB}"

    Wshell.Sendkeys "{ENTER}"

end if

NOTE: The VB Script should be launched from VBA with Shell. Once it's launched, it runs independently. So when VBA gets stuck on this popup, VBScript will still be running and do what it got to do.

Former Member
0 Kudos

I agree with above.  In addition, if this situation will always arise in this routine, you should be able to call the key commands directly from the VBA sub.  If your routine is entirely VBS, you can just insert it into the middle

Joe

Former Member
0 Kudos

David - Add the following at your commented section:

Set WshShell = CreateObject("WScript.Shell")

WshShell.sendkeys Attachment

WshShell.sendkeys "%S" 'If saving

WshShell.sendkeys "%Y" 'If an overwrite is needed

Good luck,

Joe