Hi,
I am using a Task Scheduler to open Excel Workbook at desired date/time. When open I use option Private Sub workbook_open() to run my macros. When all is done workbook should close (can be w/o saving) and be ready for new scheduled time execution. Problem is I cannot make it close... Below I am attaching code. Can anyone help?
Private Sub workbook_open()
Application.DisplayAlerts = False
Dim SapGui
Dim Applic
Dim connection
Dim session
Dim WSHShell
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WSHShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Applic = SapGui.GetScriptingEngine
On Error Resume Next
Set connection = Applic.OpenConnection("SG00 - P34: SHAPE Production ERP (with SSO)", True)
Set connection = Applic.OpenConnection("SP01 - P34 : SHAPE ERP Production (with SSO)", True)
Dim App
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
Set connection = App.Children(0)
Set session = connection.Children(0)
Dim Datex As String
Workbooks("Test2.xlsm").Activate
Worksheets("Sheet1").Select
Datex1 = Range("B4").Text
Datex2 = Range("B5").Text
Datex3 = Range("B6").Text
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nXXXX"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").Text = Datex1
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").caretPosition = 8
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").Text = Datex2
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtPA_STIDA").Text = Datex3
session.findById("wnd[0]/usr/ctxtPA_STIDA").SetFocus
session.findById("wnd[0]/usr/ctxtPA_STIDA").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[2]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "xxxx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "xxxx.txt"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 20
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set session = SAPCon.Children(0)
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n/sapapo/rrp1"
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/mbar/menu[4]/menu[12]").Select
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
Workbook.Close SaveChanges:=False
End Sub
I am using a Task Scheduler to open Excel Workbook at desired date/time. When open I use option Private Sub workbook_open() to run my macros. When all is done workbook should close (can be w/o saving) and be ready for new scheduled time execution. Problem is I cannot make it close... Below I am attaching code. Can anyone help?
Private Sub workbook_open()
Application.DisplayAlerts = False
Dim SapGui
Dim Applic
Dim connection
Dim session
Dim WSHShell
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon ")
Application.Wait Now + TimeValue("0:00:01")
Loop
Set WSHShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Applic = SapGui.GetScriptingEngine
On Error Resume Next
Set connection = Applic.OpenConnection("SG00 - P34: SHAPE Production ERP (with SSO)", True)
Set connection = Applic.OpenConnection("SP01 - P34 : SHAPE ERP Production (with SSO)", True)
Dim App
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
Set connection = App.Children(0)
Set session = connection.Children(0)
Dim Datex As String
Workbooks("Test2.xlsm").Activate
Worksheets("Sheet1").Select
Datex1 = Range("B4").Text
Datex2 = Range("B5").Text
Datex3 = Range("B6").Text
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nXXXX"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").Text = Datex1
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").caretPosition = 8
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").Text = Datex2
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtPA_STIDA").Text = Datex3
session.findById("wnd[0]/usr/ctxtPA_STIDA").SetFocus
session.findById("wnd[0]/usr/ctxtPA_STIDA").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[2]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "xxxx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "xxxx.txt"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 20
session.findById("wnd[1]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[12]").press
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set session = SAPCon.Children(0)
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n/sapapo/rrp1"
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/mbar/menu[4]/menu[12]").Select
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
Workbook.Close SaveChanges:=False
End Sub