|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub SAPLogin()
Dim SapApp
SAPConnString_TEST = " /SAP_CODEPAGE=1100 /FULLMENU /M/cn-s-sapq08d.cn.abb.com/S/sapmsQ08/G/OsA_Q08 /UPDOWNLOAD_CP=8400" '换成你的SAP测试系统登陆字符串
SAPConnString = " /SAP_CODEPAGE=1100 /FULLMENU /M/cn-s-sapp06d.cn.abb.com/S/sapmsP06/G/OsA Accounting /UPDOWNLOAD_CP=8400" '换成你的SAP正式系统登陆字符串
UserName ="你的SAP登录用户名"
PassWord = "你的SAP登录密码"
'检查是测试模式或是正式模式,设置相应字符串
If IsTestMode = True Then
Call GetAccount_test(UserName, Password)
SAPConnStr = SAPConnString_TEST
Else
Call GetAccount(UserName, Password)
SAPConnStr = SAPConnString
End If
If CheckExeIsRun("SAPLogon.exe") Then '判断SapGUI是否已经打开
GoTo 11: '如已经打开,直接跳转到登录程序
Else
Call OpenSapGUI '否则,先打开SapGUI,再登录
End If
11: If Not IsObject(SapApp) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SapApp = SapGuiAuto.GetScriptingEngine
End If
Set SAPConn = SapApp.OpenConnectionByConnectionString(SAPConnStr)
Sleep (500)
If Not IsObject(Connection) Then
Set Connection = SapApp.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(wscript) Then
wscript.ConnectObject session, "on"
wscript.ConnectObject Application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = UserName
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = Password
session.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
session.findById("wnd[0]/tbar[0]/btn[0]").press
'加上你自己录制的功能代码就可以了
End Sub
Function CheckExeIsRun(exeName As String) As Boolean '判断某个进程是否运行
On Error GoTo Err
Dim WMI
Dim Obj
Dim Objs
CheckExeIsRun = False
Set WMI = GetObject("WinMgmts:")
Set Objs = WMI.InstancesOf("Win32_Process")
For Each Obj In Objs
If (InStr(UCase(exeName), UCase(Obj.Description)) <> 0) Then
CheckExeIsRun = True
If Not Objs Is Nothing Then Set Objs = Nothing
If Not WMI Is Nothing Then Set WMI = Nothing
Exit Function
End If
Next
If Not Objs Is Nothing Then Set Objs = Nothing
If Not WMI Is Nothing Then Set WMI = Nothing
Exit Function
Err:
If Not Objs Is Nothing Then Set Objs = Nothing
If Not WMI Is Nothing Then Set WMI = Nothing
End Function |
|