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:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Nice code, looks like the one I posted here for you the other day (you're welcome by the way)...

If you want to save the file on your Desktop add the following and it should work for you...

Code:
[COLOR=#333333].findById("wnd[1]/tbar[0]/btn[0]").press
[/COLOR][B][COLOR=#ff0000].findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\YOUR USERID HERE\Desktop\"[/COLOR][/B]
[COLOR=#333333].findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Muzzu.xls"[/COLOR]
[COLOR=#333333].findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9[/COLOR]
[COLOR=#333333].findById("wnd[1]/tbar[0]/btn[0]").press[/COLOR]
 
Last edited:
Upvote 0
Thanks for your support thanks you so much..
Just have doubt can i save this file with current date name..? If yes what will be the code..?
 
Upvote 0
Change the following line

From:
Code:
[COLOR=#333333].findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Muzzu.xls"[/COLOR]

To:
Code:
[COLOR=#333333].findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Muzzu_" & Format(Now(),"mm_dd_yyyy") & ".xls"[/COLOR]
 
Last edited:
Upvote 0
Please
Please Can help me in VBA coding to Open Pdf files one bye one from folder and read the some text and rename with it and go to next PDF please


As i have to open 200 pdf files each day manually one bye one copy Invoice # and have to rename with it Please help me in this..
 
Upvote 0
Sorry, I do not know how to open and review PDF files. I am sure I could figure it out, but it would take me a large amount of time. Sorry...
 
Upvote 0
I need one more help as i have to copy one line item onces i enter in FS10n in SAP on Monthly wise macro have copy the month assigned line For example if macro have copied June Line This month and if i run the macro have to copy july Line

Im trying to paste the screen shot for your reference but im not able to do it anyways please help me on this..?
 
Upvote 0
I do not have access to FS10N within our SAP system, without access I can not see what SAP field names to use and how it is structured, sorry.

Check out this link, it may help you in recording what you want within SAP and transferring that to Excel.
 
Upvote 0
Ok Sir But if yoc can share Your Whats app or Personal email id i can send the screen shot to get your Help.. As once i login it will open an page in SAP there are line Like jan,feb,mar like this and next to that only we have amount as im doing Reconciliation for the month of June in July the macro has to Copy June Month amount like this every month it has to be done by macro.. 2 Example is If Im doing Reconciliation for the Month On FEB in March month macro has to copy Feb month line Amount Like this can you please sir as im struck here please
 
Upvote 0

Forum statistics

Threads
1,215,562
Messages
6,125,546
Members
449,237
Latest member
Chase S

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