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 dosent 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)
Code
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
Display More