Skip to Content
0
Dec 23, 2021 at 12:16 PM

SAP Script not running when exporting to excel

110 Views

def saplogin(username, password):

# Importing the Libraries

import win32com.client

import sys

import subprocess

import time

import datetime as DT

from datetime import date

# This part will Login to SAP from the SAP Logon window

try:

path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"

subprocess.Popen(path)

time.sleep(10)

SapGuiAuto = win32com.client.GetObject('SAPGUI') # Opening sap gui from *path set above

if not type(SapGuiAuto) == win32com.client.CDispatch:

return

application = SapGuiAuto.GetScriptingEngine

if not type(application) == win32com.client.CDispatch:

SapGuiAuto = None

return

connection = application.OpenConnection("(1-A) Production ERP System", True) # Inputing connection name

if not type(connection) == win32com.client.CDispatch:

application = None

SapGuiAuto = None

return

session = connection.Children(0)

if not type(session) == win32com.client.CDispatch:

connection = None

application = None

SapGuiAuto = None

return

#Entering transaction code to SAP

session.findById("wnd[0]/tbar[0]/okcd").text = "Y_DIST_LIST" #'t-code

session.findById("wnd[0]").sendVKey(0) #'Enter

session.findById("wnd[0]").maximize

session.findById("wnd[0]").sendVKey(8)

session.findById("wnd[0]/tbar[1]/btn[8]").press

session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").select

except:

print(sys.exc_info())

finally:

session = None

connection = None

application = None

SapGuiAuto = None