【问题标题】:How can I run a VBScript file silently in the background?如何在后台静默运行 VBScript 文件?
【发布时间】:2017-01-12 11:05:35
【问题描述】:

我想静默运行一个 VBScript 文件,因为它只是隐藏脚本的一部分。

我正在使用 VBScript 从 SAP 中自动导出文档,效果很好,除非在 SAP-GUI 中显示每个步骤。

VBScript 文件在 PowerShell 中启动,我已经尝试在其中隐藏进程,例如:

$vbsPDPPath = "$env:userprofile\AppData\Roaming\KPIReport"  
$vbsPDPName = "SAP-ExportPDP.vbs"
$processNamePDP = $vbsPDPPath + "\" + $vbsPDPName
Start-Process $processNamePDP -WindowStyle Hidden

虽然没有成功。

我正在寻找类似 VBA 的解决方案,您可以在其中添加:

Application.ScreenUpdating = False

仍然不知道如何解决它。我认为让您看到vbs-code会有所帮助,一定有问题。

我注意到我没有提到隐藏 SAP GUI 以及 Excel 应用程序。

Dim Number_PDP
Dim testNode
Dim WshShell
Dim profile

Set WshShell = WScript.CreateObject("WScript.Shell")
profile = WshShell.ExpandEnvironmentStrings("%USERPROFILE%")

'read XML file

Set xmlDoc = CreateObject("MSXML.DomDocument")
xmlDoc.Load profile & "\AppData\Roaming\KPIReport\DIS.xml" 

For Each testNode In xmlDoc.selectNodes("/Reports/Report")

   Number_PDP = testNode.SelectSingleNode("DIS_PDP").Text

               'connect to SAP GUI

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.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

SapGuiAuto.Visible = false 
              'not working, thought it is possible to hide SAP

session.findById("wnd[0]").resizeWorkingPane 132,31,false
session.findById("wnd[0]/tbar[0]/okcd").text = "/n cv04n"
session.findById("wnd[0]").sendVKey 0
...
              'cutted a couple of rows, just opens a document in Excel with SAP


              'after SAP opens Excel I used this code to save the documents 
set objExcel = getobject(,"Excel.Application")

if err.number<>0 then
    err.clear
end if
              'this part should be hidden as well, not working
objExcel.Visible = false     
objExcel.ActiveWorkbook.SaveAs profile & "\AppData\Roaming\KPIReport\" & Number_PDP
objExcel.ActiveWorkbook.Close
objExcel.Quit

Next

【问题讨论】:

  • 尝试在批处理模式下使用cscript解释器。cscript //B $processNamePDP
  • 你现在看到了什么? CMD 屏幕还是 Powersheel 屏幕?目前是如何启动的?
  • @VincentG 我试过了,但没用
  • @Nick.McDermaid 我看到 SAP-GUI 脚本是如何运行的,它是在 powershell 脚本中启动的。
  • 所以在 VBS 中一定有一些代码可以操作 SAP GUI。我想这就是你需要做出改变的地方。

标签: powershell vbscript sap


【解决方案1】:

我会在这两种情况下使用解决方法。

例如:

. . .
'session.findById("wnd[0]").resizeWorkingPane 132,31,false
session.findById("wnd[0]").iconify
. . .

. . .
'objExcel.Visible = false     
objExcel.WindowState = 2
. . .

问候, 脚本人

【讨论】:

  • 完美运行!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多