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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Script Host Objest Model reference

Status
Not open for further replies.

Corbie

Technical User
Nov 19, 2001
21
GB
Hi Can anybody help me

Is it possible to programatically in Excel VBA to set up a reference to the "Windows Script Host Object Model". I could ask my users to do it manually, but it would all go horribly wrong.

Your advice would be much appreciated

Corbie
 
I usually use a *.vbs or a *.hta file for WSH, however you can easily use Excel to handle WSH chores.
Here's an example:

Note: If your computer is not on a network, then comment out the code for adding a network drive. If it is on a network, then change the string "\\MyServer\C$" to an appropriate public share or administrative share.

Code:
Sub Do_WSH_stuff()
   Dim wshNetwork             As Object
   Dim wshShell               As Object
   Dim wshEnvironment         As Object


Code:
   'get computername and username
Code:
   Set wshNetwork = CreateObject("WScript.Network")
   MsgBox "User Name = " & wshNetwork.UserName
   MsgBox "Computer Name = " & wshNetwork.ComputerName   
   Set wshNetwork = Nothing


Code:
   'add a network drive
Code:
   Set wshNetwork = CreateObject("WScript.Network")
   wshNetwork.MapNetworkDrive "M:", "\\MyServer\C$"
   Set wshNetwork = Nothing


Code:
   'Find the temp folder
Code:
   Set wshShell = CreateObject("WScript.Shell")
   Set wshEnvironment = wshShell.Environment("System")
   MsgBox "Tht temp folder is located at " & _
           wshShell.ExpandEnvironmentStrings(wshEnvironment("TEMP"))
   Set wshEnvironment = Nothing
   Set wshShell = Nothing


Code:
   'create a key in HKEY_CURRENT_USER
   'then add a "Test1" value to "MyTestKey"
   'then add a "Test2" value to "MyTestKey"
Code:
   Set wshShell = CreateObject("WScript.Shell")
   wshShell.RegWrite "HKCU\MyTestKey\", "value inserted by Key", "REG_SZ"
   wshShell.RegWrite "HKCU\MyTestKey\Test1", "This sample test worked", "REG_SZ"
   wshShell.RegWrite "HKCU\MyTestKey\Test2", 5, "REG_DWORD   
   MsgBox "The data for 'Default' value of the key 'MyTestKey' = " & _
           wshShell.RegRead("HKCU\MyTestKey\")
   MsgBox "The data for 'Test1' value = " & _
           wshShell.RegRead("HKCU\MyTestKey\Test1")
   MsgBox "The data for 'Test2' value = " & _
           wshShell.RegRead("HKCU\MyTestKey\Test2")
Code:
   'The 3 lines of code below are commented out so
   'you can verify that the Registry key, values and data were added.
   'To programmatically delete these entries, uncomment the 3 lines
   
   'wshShell.RegDelete "HKCU\MyTestKey\Test1"
   'wshShell.RegDelete "HKCU\MyTestKey\Test2"
   'wshShell.RegDelete "HKCU\MyTestKey\"
Code:
   Set wshShell = Nothing
End Sub
 
Thanks,

I'll give it a go and get back to you on how I got on.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top