Forum Discussion
Download attachments from SAP FB03
Hello Group,
I have created a code to extract attachments from SAP FB03 T-code. I am facing issue when I click on display attachment the excel attachment doesn't open until there is manual click on Excel VBA workbook. Below is my code. Looking forward for your inputs and guidance.
My code identifies the count of attachments per document and loops through all attachments ( Email,Excel etC)
Public Function ExtractDocuments()
Dim Arr() As Variant
Dim DocNum As String
Dim Company As String
Dim FY As String
Dim AttCnt As Integer
Dim i As Long
Dim j As Long
Dim HWnd2 As Long
Dim lCurHwnd As Long
Dim sText As String * 255
' When session is Nothing then we need to create a new session
' else assume we can re-use the session
If session Is Nothing Then
Set session = StartSAPSession
End If
' Load the table as an Array, this will be faster
Arr = Range("Table1").ListObject.DataBodyRange
' Cycle through each row of the table (Arr)
For i = 1 To UBound(Arr, 1)
' Start by loading the row you will enter
DocNum = Arr(i, 1)
Company = Arr(i, 2)
FY = Arr(i, 3)
With session
.findById("wnd[0]").maximize
.StartTransaction "FB03" ' Load the transaction you are after
.findById("wnd[0]/usr/txtRF05L-BELNR").Text = DocNum
.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = Company
.findById("wnd[0]/usr/txtRF05L-GJAHR").Text = FY
.findById("wnd[0]").sendVKey 0 ' Execute transaction
' The query runs and you select context menu and attachments
.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
' How many attachments are there? If 1 or more then save each.
AttCnt = .findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").RowCount
If AttCnt > 0 Then
For j = 0 To AttCnt - 1
.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = j
.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").currentCellColumn = "BITM_DESCR"
.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"
.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").doubleClickCurrentCell -
The issue arises at this stage as the excel attachment dosent open until we click on VBA file
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[0]/shellcont").Close
.findById("wnd[0]").sendVKey 12
Application.SendKeys "~"
Next j
End If
End With
Next i
End Function
3 Replies
- PascalKTeamIron Contributor
Do you mean that you have to activate the macros first? If yes, this has to do with your macro security settings. You could try to set it to "enable all macros" WHICH I DON'T RECOMMEND.
- Chandrakanth KCopper Contributor
PascalKTeam No...its not asking to enable macros. The attachment in SAP is excel. It dosent open until I manually click on VBA file.
Regards,
Chandrakanth.K
- Masa2020Copper ContributorHello
I have experience of preparing similar macro. You use "doubleclick " to open and get the attachment.
How about "export" without opening the attachment? You can record the code to export in SAP function and use it in your macro as you may know.