如何强制Logon script被执行

Windows Server 2003 环境中, 在你登入 Domain , 虽然网络联机尚未完全建立, 但其会先用其 Cache 中的认证来进行登入. 在这种状况下, 由于其登入后, 网络联机尚未完全建立, 因此 Logon scripts 有可能不会被执行.

 

下述做法, 可强迫该机器必须完成网络联机后, 才能进行登入.

I. Computer Configuration Administrative Templates System Scripts

Run Startup scripts asynchronously Disabled

Run logon scripts synchronously Enabled

II. Computer Configuration Administrative Templates System Logon

    Always wait for the network at computer startup and logon Enabled

Private Sub CommandButton1_Click() Dim SapGui As Object, App As Object, Conn As Object, Session As Object Dim wbMPS As Workbook, wsBOH As Worksheet Dim wbSAP As Workbook Dim cell As Range Dim rowNumber As Long, i As Long Dim exportPath As String, exportFile As String, filePath As String Dim startTime As Double Const TIMEOUT = 60 On Error GoTo ErrHandler Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Application.EnableEvents = False ' === 路径设置 === exportPath = Environ("USERPROFILE") & "\OneDrive - BASF\Desktop" exportFile = "GRRMPM.XLSX" filePath = exportPath & "\" & exportFile ' ===================================================== ' STEP 1: 获取 SAP GUI 对象(带容错) ' ===================================================== Set SapGui = Nothing On Error Resume Next Set SapGui = GetObject("SAPGUI") If SapGui Is Nothing Then Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus Application.Wait Now + TimeSerial(0, 0, 5) Set SapGui = GetObject("SAPGUI") End If Set App = SapGui.GetScriptingEngine If App Is Nothing Then MsgBox "无法获取 SAP scripting engine.", vbCritical Exit Sub End If ' ===================================================== ' STEP 2: 获取第一个可用会话 ' ===================================================== If App.Children.Count > 0 Then Set Conn = App.Children(0) If Conn.Children.Count > 0 Then Set Session = Conn.Children(0) End If End If ' 若无现有连接,则打开新连接 If Session Is Nothing Then Set Conn = App.OpenConnection("Cobalt - Z2L (Prod) Link", True) If Conn Is Nothing Then MsgBox "无法打开 SAP 连接,请检查名称是否正确。", vbCritical Exit Sub End If Set Session = Conn.Children(0) End If If Session Is Nothing Then MsgBox "无法获取 SAP 会话。", vbCritical Exit Sub End If ' ===================================================== ' STEP 3: 执行 MB51 并导出(保持筛选条件) ' ===================================================== Session.StartTransaction "MB51" ' 展开布局选择器 → 选择第2行 → 双击进入明细 Session.FindById("wnd[0]/tbar[1]/btn[17]").Press Session.FindById("wnd[1]/tbar[0]/btn[6]").Press With Session.FindById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell") .CurrentCellRow = 2 .SelectedRows = "2" .DoubleClickCurrentCell End With Session.FindById("wnd[0]/tbar[1]/btn[8]").Press ' 执行 ' 打开菜单并导出为 Excel Session.FindById("wnd[0]/tbar[1]/btn[48]").Press Session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").ContextMenu Session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectContextMenuItem "&XXL" ' 保存对话框操作(必须四步) Session.FindById("wnd[1]/tbar[0]/btn[0]").Press ' [Choose] Session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = exportPath ' 路径 Session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = exportFile ' 文件名 Session.FindById("wnd[1]/tbar[0]/btn[11]").Press ' [Save] ' ===================================================== ' STEP 4: 等待文件生成(使用稳健检测) ' ===================================================== startTime = Timer Do While Timer - startTime < TIMEOUT DoEvents If Len(Dir(filePath)) > 0 Then If IsFileReady(filePath) Then Exit Do End If Application.Wait Now + TimeSerial(0, 0, 1) Loop If Not IsFileReady(filePath) Then MsgBox "导出失败:文件未生成或被锁定。", vbCritical GoTo Cleanup End If ' ===================================================== ' STEP 5: 更新 MPS25 数据 ' ===================================================== Set wbSAP = Workbooks.Open(filePath) Set wbMPS = Workbooks.Open(exportPath & "\MPS25.XLSX") Set wsBOH = wbMPS.Sheets("BOH") ' 清除自动筛选,防止 Find 失败 If wsBOH.AutoFilterMode Then wsBOH.AutoFilterMode = False Set cell = wsBOH.Columns("A:A").Find(What:="SAPdataend", LookIn:=xlValues, LookAt:=xlWhole) If cell Is Nothing Then MsgBox "错误:未找到 'SAPdataend' 标记!", vbExclamation GoTo Cleanup End If rowNumber = cell.Row - 1 For i = 2 To rowNumber If Not IsEmpty(wsBOH.Cells(i, "A")) Then On Error Resume Next wsBOH.Cells(i, "K").Value = Application.WorksheetFunction.SumIf( _ wbSAP.Sheets(1).Columns(5), wsBOH.Cells(i, "A").Value, _ wbSAP.Sheets(1).Columns(7)) / 2 On Error GoTo ErrHandler End If Next i wbMPS.Save ' ' ===================================================== ' ' STEP 6.1: 强化版 - 关闭 GRRMPM 并清理 SAP 窗口 ' ' ===================================================== ' On Error Resume Next ' ' ' --- 1. 关闭 GRRMPM Excel 文件 --- ' If Not wbSAP Is Nothing Then ' wbSAP.Close SaveChanges:=False ' Set wbSAP = Nothing ' End If ' ' ' 等待文件句柄释放 ' Application.Wait Now + TimeSerial(0, 0, 2) ' DoEvents ' ' ' --- 2. 关闭所有子窗口 (wnd[1], wnd[2], ...) --- ' With Session ' Do While .Children.Count > 1 ' Dim win As Object ' Set win = .FindById("wnd[1]", False) ' 不抛异常 ' If Not win Is Nothing Then ' On Error Resume Next ' .FindById("wnd[1]/tbar[0]/btn[3]").Press ' 点击取消 ' If Err.Number <> 0 Then Exit Do ' On Error GoTo 0 ' Application.Wait Now + TimeSerial(0, 0, 1) ' DoEvents ' Else ' Exit Do ' 窗口已不存在 ' End If ' Loop 'End With ' ' ' --- 3. 退出当前主事务(如 MB51)--- ' With Session ' If .Info.Transaction <> "" Then ' .FindById("wnd[0]/tbar[0]/btn[3]").Press ' Exit transaction ' Application.Wait Now + TimeSerial(0, 0, 1) ' DoEvents ' End If ' '' ' --- 4. 跳转回 SESSMAN --- ' .StartTransaction "SESSMAN" ' End With ' ' ===================================================== ' STEP 6: 静默打开 Session Manager(不弹出 Log Off 对话框) ' ===================================================== On Error Resume Next ' --- 1. 关闭导出的 Excel 文件 --- If Not wbSAP Is Nothing Then wbSAP.Close SaveChanges:=False Set wbSAP = Nothing End If Application.Wait Now + TimeSerial(0, 0, 2) DoEvents With Session ' --- 关闭所有子窗口 --- Do While .Children.Count > 1 On Error Resume Next .FindById("wnd[1]/tbar[0]/btn[3]").Press ' Cancel Application.Wait Now + TimeSerial(0, 0, 1) DoEvents Loop ' --- 强制中断当前事务 --- If .Info.Transaction <> "" Then .FindById("wnd[0]/tbar[0]/btn[3]").Press ' Exit Application.Wait Now + TimeSerial(0, 0, 1) DoEvents End If ' --- 再次检查并使用 /n 清理残留 --- If .Info.Transaction <> "" Then .SendCommand "/n" ' 强制清空 Application.Wait Now + TimeSerial(0, 0, 1) DoEvents End If ' --- 清除任何待处理的消息(防回放攻击)--- DoEvents Application.Wait Now + TimeSerial(0, 0, 1) ' --- ? 静默打开 Session Manager(无副作用)--- .SendCommand "=/i" End With 'With Session ' ' --- 2. 关闭所有子窗口 --- ' Do While .Children.Count > 1 ' Dim childWin As Object ' Set childWin = .FindById("wnd[1]", False) ' If Not childWin Is Nothing Then ' On Error Resume Next ' .FindById("wnd[1]/tbar[0]/btn[3]").Press ' Cancel ' If Err.Number <> 0 Then Exit Do ' On Error GoTo 0 ' Application.Wait Now + TimeSerial(0, 0, 1) ' DoEvents ' Else ' Exit Do ' End If ' Loop ' ' ' --- 3. 强制退出主事务 --- ' If .Info.Transaction <> "" Then ' .FindById("wnd[0]/tbar[0]/btn[3]").Press ' Exit ' Application.Wait Now + TimeSerial(0, 0, 1) ' DoEvents ' End If ' ' ' --- 4. 再次确认并清空中断状态 --- ' If .Info.Transaction <> "" Then ' .SendCommand "/n" ' 强制中断 ' Application.Wait Now + TimeSerial(0, 0, 1) ' DoEvents ' End If ' ' ' --- 5. ? 静默打开 Session Manager(无 Log Off 提示)--- ' .SendCommand "=/i" ' ?? 核心修复:加等号表示“立即执行”,不触发退出逻辑 ' Application.Wait Now + TimeSerial(0, 0, 1) ' DoEvents 'End With Cleanup: On Error Resume Next If Not wbSAP Is Nothing Then wbSAP.Close SaveChanges:=False If Not wbMPS Is Nothing Then wbMPS.Close SaveChanges:=True Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True Application.EnableEvents = True Set Session = Nothing Set Conn = Nothing Set App = Nothing Set SapGui = Nothing Exit Sub ErrHandler: MsgBox "运行时错误:" & Err.Description, vbCritical Resume Cleanup End Sub ' 辅助函数:检测文件是否可读(未被占用) Function IsFileReady(filePath As String) As Boolean On Error GoTo NotReady Open filePath For Input As #1 Close #1 IsFileReady = True Exit Function NotReady: IsFileReady = False End Function 防止在ACTIVE X 按钮上时无法获取SAP SCRIPT ENGINE
最新发布
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值