PDA

View Full Version : Hide CMD Console when running cscript



Fallen Hero
04-01-2016, 11:21 AM
Hey guys,

I've got the following code, mostly borrowed from
https://blogs.msdn.microsoft.com/cristib/2012/02/29/vba-how-to-programmatically-enable-access-to-the-vba-object-model-using-macros/ (sorry I don't have 5 posts yet lol), that uses some VBS to change the registry settings of users to allow access to the VBA for my template:


' ==============================================================
' * Please note that Microsoft provides programming examples
' * for illustration only, without warranty either expressed or
' * implied, including, but not limited to, the implied warranties of
' * merchantability and/or fitness for a particular purpose. Any of
' * the code provided use by you in this blog is at your own risk.
'===============================================================
Sub CheckIfVBAAccessIsOn()


'[HKEY_LOCAL_MACHINE/Software/Microsoft/Office/10.0/Excel/Security]
'"AccessVBOM"=dword:00000001

Dim strRegPath As String
strRegPath = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & Application.Version & "\Word\Security\AccessVBOM"


If TestIfKeyExists(strRegPath) = False Then
' Dim WSHShell As Object
' Set WSHShell = CreateObject("WScript.Shell")
' WSHShell.RegWrite strRegPath, 3, "REG_DWORD"
MsgBox _
"If you are running this template for the first time, Word must first" & vbNewLine & _
"restart in order to change certain system settings. Please open the" & vbNewLine & _
"template again once you see the message ""SETUP PROCESS COMPLETE""."
Call WriteVBS
ActiveDocument.AttachedTemplate.Saved = True
ActiveDocument.Saved = True
Application.Quit
End If


Dim VBAEditor As Object 'VBIDE.VBE
Dim VBProj As Object 'VBIDE.VBProject
Dim tmpVBComp As Object 'VBIDE.VBComponent
Dim VBComp As Object 'VBIDE.VBComponent

Set VBAEditor = Application.VBE
Set VBProj = Application.ActiveDocument.VBProject

Dim counter As Integer


For counter = 1 To VBProj.References.Count
Debug.Print VBProj.References(counter).FullPath
'Debug.Print VBProj.References(counter).Name
Debug.Print VBProj.References(counter).Description
Debug.Print "----------------------------------"
Next


Exit_This:


End Sub
Function TestIfKeyExists(ByVal path As String)
Dim WshShell As Object
Set WshShell = CreateObject("WScript.Shell")
On Error Resume Next
WshShell.RegRead path

If Err.Number <> 0 Then
Err.Clear
TestIfKeyExists = False
Else
TestIfKeyExists = True
End If
On Error GoTo 0
End Function
Sub WriteVBS()
Dim objFile As Object
Dim objFSO As Object
Dim sSavePath As String
Dim sCodeToRun As String


sSavePath = CreateObject("WScript.Shell").specialfolders("Desktop") & "\reg_setting.vbs"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(sSavePath, 2, True)


objFile.WriteLine (" On Error Resume Next")
objFile.WriteLine ("")
objFile.WriteLine ("Dim WshShell")
objFile.WriteLine ("Set WshShell = CreateObject(""WScript.Shell"")")
objFile.WriteLine ("")
objFile.WriteLine ("MsgBox ""SETUP PROCESS COMPLETE""")
objFile.WriteLine ("")
objFile.WriteLine ("Dim strRegPath")
objFile.WriteLine ("Dim strRegPath2")
objFile.WriteLine ("Dim Application_Version")
objFile.WriteLine ("Application_Version = """ & Application.Version & """")
objFile.WriteLine ("strRegPath = ""HKEY_CURRENT_USER\Software\Microsoft\Office\"" & Application_Version & ""\Word\Security\AccessVBOM""")
objFile.WriteLine ("strRegPath2 = ""HKEY_CURRENT_USER\Software\Microsoft\Office\"" & Application_Version & ""\Word\Security\VBAWarnings""")
objFile.WriteLine ("WScript.echo strRegPath")
objFile.WriteLine ("WshShell.RegWrite strRegPath, 1, ""REG_DWORD""")
objFile.WriteLine ("WScript.echo strRegPath2")
objFile.WriteLine ("WshShell.RegWrite strRegPath2, 1, ""REG_DWORD""")
objFile.WriteLine ("")
objFile.WriteLine ("If Err.Code <> o Then")
objFile.WriteLine (" MsgBox ""Error"" & Chr(13) & Chr(10) & Err.Source & Chr(13) & Chr(10) & Err.Message")
objFile.WriteLine ("End If")
objFile.WriteLine ("")
objFile.WriteLine ("WScript.Quit")
objFile.Close


Set objFile = Nothing
Set objFSO = Nothing


'run the VBscript code
' > The macro will fail to execute the VB script if you use a
' [codepath] which contains blanks!
'
' > To fix this issue, we add a pair of double quotes (" ") around
' [codepath];
Shell "cscript " & Chr(34) & sSavePath & Chr(34), vbNormalFocus


End Sub

I'm ok at VBA but don't have much experience with VBS. What I want to knowis, how can I run the cscript from this VBS so the command console is hidden while it runs?

I've tried rewriting the code using:


WshShell = Null


Which didn't do anything

Then I tried writing all the VBS code to a string and using:


objFile.WriteLine ("WshShell.Run ""cmd /c" & sMyString & """,0,True")

And that didn't work either. However, I'm not entirely certain I did it right. I was trying to write the VBS code to the string to include the & signs for the VBS file to use. So in my VBA it looked like:


sMyString = _
"""On Error Resume Next"" & " & _
""""" & " & _
"""Dim WshShell"" & " & _

etc.

But not sure that's right.

Any help would be very appreciated! Thanks

gmayor
04-01-2016, 09:01 PM
If the user's macro security prevents the running of your code, how is it going to run the code that purports to change the security to enable it to do so?
It is a fundamental tenet of basic security that users can block macro code.

Fallen Hero
04-04-2016, 08:07 AM
This code works as intended. It requires the user to choose to enable the content the first time it runs. Then it changes the users settings so that they will no longer receive any warnings upon subsequent use of the file.

The way it works is by creating a .VBS file on the user's desktop which then runs separately from the Word file to access the relevant registry settings and write or change the key values.

The code works fine. I'm not asking for help to get it to work. I just want to know if it is possible to hide the command console window (black box) that pops up when running the .VBS file.