Need help developing vbscript

madhatter5501
madhatter5501 used Ask the Experts™
on
I am trying to write a script that will check the registry if a value exists and then remove it.

Does the following code make sense, or can it be improved?

'Check for Both Office 2007 and 2010 keys and if they exist to remove them'

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
Set objRegistry2 = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover\RedirectServers\"
strKeyPath2 = "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Autodiscover\RedirectServers\"

strValueName = ""

objRegistry.GetStringValue strKeyPath, strValueName, strValue2010
objRegistry.GetStringValue strKeyPath2, strValueName, strValue2007

	If IsNull(strValue2010) Then

		'Do Nothing
		
	Else

		Set WshShell = WScript.CreateObject("WScript.Shell")
		regEntry2010 = "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover\RedirectServers\"
		WshShell.RegDelete regEntry2010

	ElseIf IsNull(strValue2007) Then

		'Do Nothing
		
	Else

		Set WshShell = WScript.CreateObject("WScript.Shell")
		regEntry2010 = "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover\RedirectServers\"
		WshShell.RegDelete regEntry2010
	
	End If

'Function to removed the script after run the one time'
	Function removeScript()
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		strScript = Wscript.RemoveOutlookAutodiscover.vbs
		objFSO.DeleteFile(strScript)
	End Function

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
You can reduce it to ...

'Check for Both Office 2007 and 2010 keys and if they exist to remove them'

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyRoot = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\VERSION\Outlook\AutoDiscover\RedirectServer\"

For Each strVer in Array("12.0","14.0")
  strValueName = ""
  strKeyPath = Replace(strKeyRoot, "VERSION", strVer)
  objRegistry.GetStringValue strKeyPath, strValueName, strValue

  If IsNull(strValue) Then
    'Do Nothing
  Else
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.RegDelete strKeyPath
  End If

Open in new window

Author

Commented:
I get an error on Line 22 Expected Next.  So I added Next and then get an error on Line 14 Char 3 Error: Type Mismatch
Top Expert 2007

Commented:
Sorry, focused more on reducing the code than checking the logic.
Goal is to delete that key and everything below it?  Or a specific value?
Top Expert 2007

Commented:
If the former, it's as simple as

'Check for Both Office 2007 and 2010 keys and if they exist to remove them'

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyRoot = "SOFTWARE\Microsoft\Office\VERSION\Outlook\AutoDiscover\RedirectServer\"

For Each strVer in Array("12.0","14.0")
  strKeyPath = Replace(strKeyRoot, "VERSION", strVer)
  objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
Next

Open in new window

Top Expert 2007

Commented:
Unless you're wanting to double-check that the key exists.
There's no need to validate that it exists - the DeleteKey method won't throw an error if it's not there...

Author

Commented:
that works good for office 2010, I need to test still on 2007.  is there a way to make it delete the script from the hard drive once it has been run?
Top Expert 2007
Commented:
at the bottom of the script use:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile wscript.ScriptFullName

Author

Commented:
thanks for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial