Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Reference Windows Script Host

Status
Not open for further replies.

AccessGuruCarl

Programmer
Jul 3, 2004
471
US
Does anyone have a sample piece of code showing how to use Windows Script in VBA

I've added the reference...
Windows Script Host Object Module
But don't know how to properly code the declares.

If i run the script as .vbs it works, but I'd like to do this in code, and delete the .vbs script file.

I pasted the .vbs script behind a command button, but it doesn't seem to work.

The script requires an argument to run, and I don't want to give this info out if possible....

I can pull the argument from a table, and would like to do everything on the fly.

Thanks in advance...

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
You'll get the best answer if you can post the vbs code. Change any security sensitive values to something else of course.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
You may also reference the Microsoft Scripting Runtime.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
TomThumbKP,

Here is the code I'm working with straight from MS
Code:
Dim WScript As WshEnvironment	' ? Not sure if this is correct
Dim WshShell, Obj, result
Dim VOL_PROD_KEY

VOL_PROD_KEY = sqlResult
VOL_PROD_KEY = Replace(VOL_PROD_KEY, "-", "") 'remove hyphens if any
set WScript = ' ? I think I need to set this to something
'Error occurs here if I remove Dim WScript As WshEnvironment at Wscript in the Set WshShell =
'Added the Dim... Code appears to run correctly, stepping through code, but no change occurs.
Set WshShell = WScript.CreateObject("WScript.Shell")
For Each Obj In GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation")

   result = Obj.SetProductKey(VOL_PROD_KEY)
   If Err <> 0 Then
      MsgBox Err.Description, "0x" & Hex(Err.Number)
      Err.Clear
   End If

Next

As mentioned, you can see why I don't want to give the user the info. I have roughly 100 pc's that the company needs updated. And thought it would be much easier if I could just create a form, then pull it up at each pc when I go in to do the updates for them. This is a side job, and I'd like to make it as fast and painless a possible.

Thanks in advance...
Carl


AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Remove the Windows Script Host Object Module reference
and add the Microsoft Scripting Runtime reference.
Get rid of Dim WScript and Set WScript
Replace this:
Set WshShell = WScript.CreateObject("WScript.Shell")
By this:
Set WshShell = CreateObject("WScript.Shell")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV: I did as reccommended.

New Error now, at line result = Obj.SetProductKey(VOL_PROD_KEY)

Here is the modified code. With a reference to to the Microsoft Scripting Runtime only.

Code:
Dim WshShell, Obj, result
Dim VOL_PROD_KEY
VOL_PROD_KEY = sqlResult
VOL_PROD_KEY = Replace(VOL_PROD_KEY, "-", "") 'remove hyphens if any
Set WshShell = CreateObject("WScript.Shell")
For Each Obj In GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("win32_WindowsProductActivation")
'Error now occurs here, Invalid Operation
'Runtime Error '-2147217386(80041016)':
   result = Obj.SetProductKey(VOL_PROD_KEY)
MsgBox Err.nunber & " : " & Err.Description 'testing
   If Err <> 0 Then
      MsgBox Err.nunber & " : " & Err.Description
      Err.Clear
   End If

Next


Thanks...

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
I don't think you need anything special at the top level other than a link to the library - which you seem to have achieved. You then use your objects as required, something like this VBA code :-
Code:
Set FSO = CreateObject("Scripting.FileSystemObject")

Regards
BrianB
Use CupOfCoffee to speed up all windows applications.
It is easy until you know how.
================================
 
PS.
It seems that while I was writing my reply others were doing the same.

I have only used scripting a couple of times and never had to do anything like suggested. The line of code I have supplied is the only thing required in the macro to get things going. Have a look here to see what I mean - which was the source for a module I created.


Regards
BrianB
Use CupOfCoffee to speed up all windows applications.
It is easy until you know how.
================================
 
Thanks for the links,

PHV: Your link answered my problem.

I was testing this on my PC, which is already SP updated & activated.

I'll have to try it on one of the actual machines that needs the update.

We'll consider this one solved for now!

Thanks again,
Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top