F. Michal
F. Michal

Reputation: 19

How can I run a VBScript file silently in the background?

I want to run a VBScript file silently, because it is just a part of a hidden script.

I'm using the VBScript to export automatically documents out of SAP, that is working perfectly, unless showing each step in the SAP GUI.

The VBScript file is started in a PowerShell, where I already tried to hide the process like:

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

Didn't work out though.

I'm looking for a solution like in VBA, where you can just add:

Application.ScreenUpdating = False

Still have no idea how to solve it. I thought it would be helpful to let you see the VBS code, there must be the fault.

I noticed that I haven't mentioned to hide the SAP GUI as well as the Excel Application.

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

Upvotes: 0

Views: 3178

Answers (1)

ScriptMan
ScriptMan

Reputation: 1625

I would use a workaround in both cases.

For example:

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

and

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

Regards, ScriptMan

Upvotes: 1

Related Questions