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
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: