Error in Saving the SAP export report in Folder

ahmed18

Board Regular
Joined
Jul 13, 2016
Messages
72
Hi all
I have Vba code but When i run the macro it will run but im not able to find the file as its saved in computer can any one read my below codes and correct the code to save the export xls file to save in a particular folder

Below are my codes for your reference
Code:
Sub SAPLoginMacro() 'DESIGNED TO LOG IN & EXTRACT DATA FROM SAP USING USER LOGIN AND PASSWORD


Dim stSapUName As String, stSapPW As String
Dim SapguiApp As Object, connection As Object, session As Object


stSapUName = InputBox("Please enter your SAP User name", "SAP User Name")
stSapPW = InputBox("Please enter your SAP Password", "SAP Password")


On Error GoTo errFailed
    '****************************************************************************************
    'ESTABLISH CONNECTION TO SAP                                                            *
    '****************************************************************************************
    Set SapguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
    Set connection = SapguiApp.OpenConnection("LH1", True)
    Set session = connection.Children(0)
    
On Error GoTo 0


    With session
        '************************************************************************************
        'LOGON TO SAP                                                                       *
        '************************************************************************************
        .findById("wnd[0]/usr/txtRSYST-BNAME").Text = stSapUName
        .findById("wnd[0]/usr/pwdRSYST-BCODE").Text = stSapPW
        .findById("wnd[0]").sendVKey 0
    
        '************************************************************************************
        'PROCESSES THE POPUP IF MULTIPLE LOGINS DETECTED                                    *
        '************************************************************************************
        If .Children.Count > 1 Then
            .findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
            .findById("wnd[1]/tbar[0]/btn[0]").press
        End If
    
        '************************************************************************************
        'NAVIGATES TO THE TCODE                                                    *
        '************************************************************************************
            .findById("wnd[0]").maximize
            .findById("wnd[0]/tbar[0]/okcd").Text = "fbl5n"
            .findById("wnd[0]").sendVKey 0
            
        
        On Error Resume Next
        '************************************************************************************
        'SELECTS THE FILE TYPE SPREADSHEET AND SAVES FILE TO HARD DRIVE                     *
        '************************************************************************************
        .findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton _
        "&MB_EXPORT"
        
        .findById("wnd[1]/tbar[0]/btn[0]").press
        .findById("wnd[0]/usr/ctxtDD_KUNNR-LOW").Text = "WONETPR85"
        .findById("wnd[0]/usr/ctxtDD_BUKRS-LOW").Text = "PR85"
        .findById("wnd[0]/usr/ctxtDD_INDEX-HOTKEY").SetFocus
        .findById("wnd[0]/usr/ctxtDD_INDEX-HOTKEY").caretPosition = 0
        .findById("wnd[0]/tbar[1]/btn[8]").press
        .findById("wnd[0]/tbar[0]/okcd").Text = "%pc"
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
        .findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
        .findById("wnd[1]/tbar[0]/btn[0]").press
        .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Muzzu.xls"
        .findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9
        .findById("wnd[1]/tbar[0]/btn[0]").press
       


    End With


    On Error GoTo 0
    '****************************************************************************************
    'CLEAR ALL SET VARIABLES                                                                *
    '****************************************************************************************
    Set session = Nothing
    Set connection = Nothing
    Set SapguiApp = Nothing
           
Exit Sub


'********************************************************************************************
'DISPLAY ERROR HANDLING MESSAGE BOX                                                        *
'********************************************************************************************
errFailed:
    MsgBox "The connection to SAP has been halted by the user."
    End
    
End Sub
 
Last edited by a moderator:
Hello Sir Thanks It working
I need code to Create new session in SAP as im not able to record it Sir..?
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This will work in every month sir as below are the my codes please check sir is it correct i have pasted the lines
Code:
session.findById("wnd[0]").maximizesession.findById("wnd[0]/tbar[0]/okcd").text = "z1araging"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radBYSF").select
session.findById("wnd[0]/usr/chkBYCUS").selected = true
session.findById("wnd[0]/usr/chkP_SPLIT1").selected = true


stLastDay = Format(DateSerial(Year(Date), Month(Date), 0), "yyyymmdd")
session.findById("wnd[0]/usr/ctxtCOMCD-LOW").text = "FR40"
session.findById("wnd[0]/usr/ctxtAKONT-LOW").text = "0012011000"
session.findById("wnd[0]/usr/ctxtHKONT-LOW").text = "0012011000"
session.findById("wnd[0]/usr/ctxtEDATE").SetFocus
        session.findById("wnd[0]/usr/ctxtEDATE").caretPosition = 0
        session.findById("wnd[0]").sendVKey 4
        session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = stLastDay
        session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20160731,20160731"
        session.findById("wnd[0]/usr/radBYSF").Select
        session.findById("wnd[0]/usr/chkBYCUS").Selected = True
        session.findById("wnd[0]/usr/chkP_SPLIT1").Selected = True
session.findById("wnd[0]/usr/txtP_FILE1").text = "c:\1201 12011000lc 1.xls"
session.findById("wnd[0]/usr/txtP_FILE1").setFocus
session.findById("wnd[0]/usr/txtP_FILE1").caretPosition = 20
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]").sendVKey 3
 
Upvote 0
So the above code is working?

What do you mean you need code to create a new session in SAP?
 
Upvote 0
Sir this code will work every month like as this August its picking the July Last date if i run the macro next month in Sept the macro will pick August Last date ..?
Code:
[TABLE="width: 535"]
<colgroup><col></colgroup><tbody>[TR]
[TD]stLastDay = Format(DateSerial(Year(Date), Month(Date), 0), "yyyymmdd")[/TD]
[/TR]
[TR]
[TD]session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = stLastDay
session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20160731,20160731"


[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Sir i Have one more Question
as you see is below code the its a excel chat so i want macro to pick Copy Previous Month Like If Im in August the macro has to copy July Month Amount in Colo um " Cum. balanc" Below

Code:
[TABLE="width: 683"]
<colgroup><col span="2"><col span="2"><col><col></colgroup><tbody>[TR]
[TD]Month[/TD]
[TD]Period[/TD]
[TD]            Debit[/TD]
[TD]           Credit[/TD]
[TD]        Balance[/TD]
[TD]   Cum. balance[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Balance Carryforward[/TD]
[TD]Balance Carryforward[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]November[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]December[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]January[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]February[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]March[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]April[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]May[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]June[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]July[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]August[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]183,223,618.24[/TD]
[TD="align: right"]198,055,668.38[/TD]
[TD="align: right"]-14,832,050.14[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]September[/TD]
[TD="align: right"]11[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]October[/TD]
[TD="align: right"]12[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]Special period 1[/TD]
[TD="align: right"]13[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]Special period 2[/TD]
[TD="align: right"]14[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]Special period 3[/TD]
[TD="align: right"]15[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]Special period 4[/TD]
[TD="align: right"]16[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
[TR]
[TD]Total[/TD]
[TD]Total[/TD]
[TD="align: right"]2,046,766,578.12[/TD]
[TD="align: right"]2,088,579,279.00[/TD]
[TD="align: right"]-41,812,700.88[/TD]
[TD="align: right"]197,949,184.74[/TD]
[/TR]
</tbody>[/TABLE]

 
Upvote 0
SIr i need a small help as i have multiple account to run in FS10n i have created the vba code in for next loop

Can please check my below codes as if any account does not have balance it will get stop please check na help me sir

Code:
Sub SAPLoginMacro() 'DESIGNED TO LOG IN & EXTRACT DATA FROM SAP USING USER LOGIN AND PASSWORD

Dim stSapUName As String, stSapPW As String
Dim SapguiApp As Object, connection As Object, session As Object


Dim lastcouumn As Integer
Dim lastrow As Long


     
     
    
stSapUName = InputBox("Please enter your SAP User name", "SAP User Name")
stSapPW = InputBox("Please enter your SAP Password", "SAP Password")






On Error GoTo errFailed
    '****************************************************************************************
    'ESTABLISH CONNECTION TO SAP                                                            *
    '****************************************************************************************
    Set SapguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
    Set connection = SapguiApp.OpenConnection("LH1", True)
    Set session = connection.Children(0)
    
On Error GoTo 0




    With session
        '************************************************************************************
        'LOGON TO SAP                                                                       *
        '************************************************************************************
        .findById("wnd[0]/usr/txtRSYST-BNAME").Text = stSapUName
        .findById("wnd[0]/usr/pwdRSYST-BCODE").Text = stSapPW
        .findById("wnd[0]").sendVKey 0
    
        '************************************************************************************
        'PROCESSES THE POPUP IF MULTIPLE LOGINS DETECTED                                    *
        '************************************************************************************
        If .Children.Count > 1 Then
            .findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
            .findById("wnd[1]/tbar[0]/btn[0]").press
        End If
    
        '************************************************************************************
        'NAVIGATES TO THE TCODE                                                    *
        '************************************************************************************
            .findById("wnd[0]").maximize
            .findById("wnd[0]/tbar[0]/okcd").Text = "fs10n"
            .findById("wnd[0]").sendVKey 0
            
        
        On Error Resume Next
        '************************************************************************************
        'SELECTS THE FILE TYPE SPREADSHEET AND SAVES FILE TO HARD DRIVE                     *
        '************************************************************************************
        .findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton _
        "&MB_EXPORT"
            
            
            For i = 2 To Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
            On Error Resume Next
            
            
     
'       lastColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column
'       lastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
'       For i = 0
'       A1 = range(
        session.findById("wnd[0]/usr/ctxtSO_SAKNR-LOW").Text = Cells(i, 2).Value
        session.findById("wnd[0]/usr/ctxtSO_BUKRS-LOW").Text = Cells(i, 1).Value
        session.findById("wnd[0]/usr/txtGP_GJAHR").Text = Cells(i, 5).Value
        session.findById("wnd[0]/usr/ctxtSO_GSBER-LOW").SetFocus
        session.findById("wnd[0]/usr/ctxtSO_GSBER-LOW").caretPosition = 0
        session.findById("wnd[0]").sendVKey 19
        session.findById("wnd[0]/usr/ctxtGP_CURTP").Text = Cells(i, 3).Value
        session.findById("wnd[0]/usr/ctxtGP_CURTP").SetFocus
        session.findById("wnd[0]/usr/ctxtGP_CURTP").caretPosition = 2
        session.findById("wnd[0]/tbar[1]/btn[8]").press
        session.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
        session.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").selectContextMenuItem "&PC"
        session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
        session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        
        .findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\YOUR USERID HERE\12011000"
        .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i, 6).Value
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 2
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        session.findById("wnd[0]").sendVKey 3
        session.findById("wnd[0]/usr/ctxtGP_CURTP").Text = Cells(i, 4).Value
        session.findById("wnd[0]/usr/ctxtGP_CURTP").SetFocus
        session.findById("wnd[0]/usr/ctxtGP_CURTP").caretPosition = 2
        session.findById("wnd[0]/tbar[1]/btn[8]").press
        session.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
        session.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").selectContextMenuItem "&PC"
        session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
        session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        
        .findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\YOUR USERID HERE\12011000"
        .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i, 7).Value
     
        session.findById("wnd[0]").sendVKey 3
     
     
     
    Next i


    End With


    On Error GoTo 0
    '****************************************************************************************
    'CLEAR ALL SET VARIABLES                                                                *
    '****************************************************************************************
    Set session = Nothing
    Set connection = Nothing
    Set SapguiApp = Nothing
    
    
    
           


'********************************************************************************************
'DISPLAY ERROR HANDLING MESSAGE BOX                                                        *
'********************************************************************************************
errFailed:
    MsgBox "The connection to SAP has been halted by the user."
    
    
    


End Sub
 
Upvote 0
Hello sir i need one more help..

I want to make automation for importing the data from SAP Fs10n t-code to excel with different company code and account #
So please vist the link and see my excel file https://app.box.com/s/eekvctxswoija2xoi5rzhy34k4obv9nh and help me to make it happen automation

The macro have to run if its not found data in sap it has to go to next account

Please help me

Below is the link where u can find the my excel sheet and code in it

https://app.box.com/s/eekvctxswoija2xoi5rzhy34k4obv9nh

Thanks in advance
Ahmed Muzamil:)
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,631
Members
449,241
Latest member
NoniJ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top