I have been trying to work on creating a script that will shut down a
computer when ran. Only problem is i managed to code it in VBA.

Take into account i have never learned VBS before and figured they
would be similar, unfortunately running the script directly doesnt
work...however if i copy the code into Excels macro section...the
computer shuts down correctly.

I would be appreciative if someone could help me recode this to run
through VBS

The coding is as follows:

Sub Shutdown()

Dim osObj, osColl
Const nForcePowerDown = 12

Set osColl = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select *
from Win32_OperatingSystem")

For Each osObj In osColl
osObj.Win32Shutdown (nForcePowerDown)

Next

End Sub

Re: Need Help Converting VBA to VBS by Tom

Tom
Mon Oct 16 10:09:29 CDT 2006

VBS requires a main program. The only thing you need to do to THIS
script to make it work is either remove the code from within the
subroutine OR write a one line main program to call the subroutine, as
in ...

' *** main ***
shutdown
' *** end main ***

Sub Shutdown()
...
end sub

There are some other differences, such as scripts not supporting early
binding of objects, nor named arguments in subroutine calls and the
need to explicitely define VBA constants to use them. None of those
apply here. In general, Scripts are fairly easily translated between
VBA and VBS (and vice versa).

Tom Lavedas
=============
http://members.cox.net/tglbatch/wsh

Benmorgan86uk@hotmail.co.uk wrote:
> I have been trying to work on creating a script that will shut down a
> computer when ran. Only problem is i managed to code it in VBA.
>
> Take into account i have never learned VBS before and figured they
> would be similar, unfortunately running the script directly doesnt
> work...however if i copy the code into Excels macro section...the
> computer shuts down correctly.
>
> I would be appreciative if someone could help me recode this to run
> through VBS
>
> The coding is as follows:
>
> Sub Shutdown()
>
> Dim osObj, osColl
> Const nForcePowerDown = 12
>
> Set osColl = GetObject("winmgmts:{(Shutdown)}").ExecQuery("Select *
> from Win32_OperatingSystem")
>
> For Each osObj In osColl
> osObj.Win32Shutdown (nForcePowerDown)
>
> Next
>
> End Sub


Re: Need Help Converting VBA to VBS by Benmorgan86uk

Benmorgan86uk
Mon Oct 16 16:14:51 CDT 2006

Ah i see, so quite literally all that was needed to convert the coding
from VBA to VBS was the removal of the bits which declare it a
subroutine (sub and end sub).

The script appears to be working now, just ran it on my computer and it
shut everything down correctly...didnt actually turn the thing off
which is a shame but it cud me more to do with the hardware. Will test
it at work tomorrow

Thanx loads


Re: Need Help Converting VBA to VBS by Benmorgan86uk

Benmorgan86uk
Mon Oct 16 16:15:08 CDT 2006

Ah i see, so quite literally all that was needed to convert the coding
from VBA to VBS was the removal of the bits which declare it a
subroutine (sub and end sub).

The script appears to be working now, just ran it on my computer and it
shut everything down correctly...didnt actually turn the thing off
which is a shame but it cud be more to do with the hardware. Will test
it at work tomorrow

Thanx loads


Re: Need Help Converting VBA to VBS by mr_unreliable

mr_unreliable
Fri Oct 20 15:31:20 CDT 2006

hi Ben,

You might try this if you want to "turn off":

CreateObject("WScript.Shell").Run "%comspec% /c C:\WINDOWS\RUNDLL.EXE
user.exe,exitwindows"

Or, at least that works on win98. USER.EXE is a 16-bit utility
and so may not exist on winXP.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)


Benmorgan86uk@hotmail.co.uk wrote:
> The script appears to be working now, just ran it on my computer and it
> shut everything down correctly...didnt actually turn the thing off
> which is a shame

Re: Need Help Converting VBA to VBS by VS

VS
Sat Oct 21 14:08:50 CDT 2006


The 'shutdown' utility from the windows resource kit.

--
VS