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:
Ok Sir But if yoc can share Your Whats app or Personal email id i can send the screen shot to get your Help..

Please note that it is against forum rules to take problems offline. If you are found to be doing this you may have your forum privileges revoked.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello Sir Good DAy

Sir i have recorded the SAP script and its running Good but if i give same macro to my friend and if he is running then we are getting an error so how solve this problem if i record the SAP script macro then same have to work in every system please help me on this below are my codes

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

.findById("wnd[0]/usr/ctxtSO_SAKNR-LOW").Text = "12011000"
.findById("wnd[0]/usr/ctxtSO_BUKRS-LOW").Text = "fr40"
.findById("wnd[0]/usr/txtGP_GJAHR").Text = "2016"
.findById("wnd[0]/usr/ctxtSO_GSBER-LOW").SetFocus
.findById("wnd[0]/usr/ctxtSO_GSBER-LOW").caretPosition = 0
.findById("wnd[0]").sendVKey 19
.findById("wnd[0]/usr/ctxtGP_CURTP").Text = "30"
.findById("wnd[0]/usr/ctxtGP_CURTP").SetFocus
.findById("wnd[0]/usr/ctxtGP_CURTP").caretPosition = 2
.findById("wnd[0]/tbar[1]/btn[8]").press
.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
.findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell").selectContextMenuItem "&PC"
.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_PATH").Text = "C:\Users\YOUR USERID HERE\12011000"
.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "12011000 30 _" & Format(Now(), "mm_dd_yyyy") & ".xls"
.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
.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



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

End Sub
 
Upvote 0
Make sure that SAP is completely closed on the machine and try to run it. When SAP opens a new window (screen) it gives it a number if the user has 2 windows open and you run this code, it will open a third window and in the code, where it has ("wnd[0]/) this is calling window 0 and ("wnd[1]/) is calling window 1.

Let me know if this works
 
Upvote 0
Ok sir thanks Now its working

And i need VBA code to Extract the text DATA PDF text to excel One bye one pdf file..
Like i have 50 PDF files in a folder if i run macro one PDF have to open extract the data into EXCEL the close next open 2nd PDF

Can you Please Help me..?
 
Upvote 0
Hi sir

Please Help in vba code to extract the data into text one by one PDF automatically
I have 50 PDF files in a folder it should open one by one please help me
 
Upvote 0
As I stated prior, I am not sure how to extract PDF files into Excel. I did a little internet research and found this link, perhaps the code/information there can help you...
 
Upvote 0
Hey i need a help as i have recorded the SAP script i need one more line code to pick last month ending date..
For Example if Im in August the Macro have to Put July month ending date like "20160731" like this every month it has to be done

This line "session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20160731" as you can see below

Below are my codes please help me to run it

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "z1araging"
session.findById("wnd[0]").sendVKey 0
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 = "20160731"
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 LC.xls"
session.findById("wnd[0]/usr/txtP_FILE1").setFocus
session.findById("wnd[0]/usr/txtP_FILE1").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press
 
Upvote 0
Hello Ahmed.

Add the following at the top of your code,
Code:
stLastDay = Format(DateSerial(Year(Date), Month(Date), 0), "yyyymmdd")

Then change this
Code:
[COLOR=#333333]session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20160731"[/COLOR]

to this
Code:
[COLOR=#333333]session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = stLastDay[/COLOR]
 
Upvote 0
The correct code will be like this as i mentioned below sir or this line i have to put beaning stLastDay = Format(DateSerial(Year(Date), Month(Date), 0), "yyyymmdd")

session.findById("wnd[0]/tbar[0]/okcd").text = "z1araging"
session.findById("wnd[0]").sendVKey 0
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


stLastDay = Format(DateSerial(Year(Date), Month(Date), 0), "yyyymmdd")
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 LC.xls"
session.findById("wnd[0]/usr/txtP_FILE1").setFocus
session.findById("wnd[0]/usr/txtP_FILE1").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press
 
Last edited:
Upvote 0
Paste your ENTIRE code in the forum so I can see it please... Use the CODE tags as well, it makes it ALOT easier to decipher... (click the # above your reply box and paste your code between the ] and [)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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