Posts Tagged ‘VBS’

VBA Script to Add a Shared Calendar to Users Outlook

'You will need to disable Macro Security for this to run
'Use GPO to deploy VbaProject.OTM to %appdata%\Microsoft\Outlook folder

Private Sub Application_Startup()
 
Call OpenMeetingRoom1
 
End Sub

Sub OpenMeetingRoom1()

    Dim myNamespace As Outlook.NameSpace
    Dim myRecipient As Outlook.Recipient
    Dim CalendarFolder As Outlook.Folder
    
    Set myNamespace = Application.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("Meeting Rooom 1")
    myRecipient.Resolve
    If myRecipient.Resolved Then
        Call ShowCalendar(myNamespace, myRecipient)
    End If
 
End Sub



Sub ShowCalendar(myNamespace, myRecipient)
    Dim CalendarFolder As Outlook.Folder

    Set CalendarFolder = _
        myNamespace.GetSharedDefaultFolder _
        (myRecipient, olFolderCalendar)
    'Open up the Calendar
    'CalendarFolder.Display
End Sub
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Set objShell = CreateObject( "WScript.Shell" )
Set objFSO = CreateObject("Scripting.FileSystemObject")
appDataLocation=objShell.ExpandEnvironmentStrings("%APPDATA%")
quick_normal_location = appDataLocation & "\Microsoft\Templates\"
quick_normal_location_file = appDataLocation & "\Microsoft\Templates\Normal.dotm"
		normal_local_network = "%network localtion%Normal.dotm"

' Does the network normal exist
If objFSO.FileExists(normal_local_network) = True Then
	' Does the Local Normal Exist
	If objFSO.FileExists(quick_normal_location_file) = True Then
			' Check to see if the Local Normal is older than the newer Normal
		If CDate(objFSO.GetFile(normal_local_network).DateLastModified) > CDate(objFSO.GetFile(quick_normal_location_file).DateLastModified) Then
			intBkpNum = 1
			While objFSO.FileExists(quick_normal_location_file & intBkpNum) = True
				intBkpNum = intBkpNum + 1
			Wend
			objFSO.MoveFile quick_normal_location_file, quick_normal_location_file & intBkpNum
			objFSO.CopyFile normal_local_network, quick_normal_location_file
		End If
	End If
End If
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

 

set objShell = WScript.CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
appDataPath = objShell.ExpandEnvironmentStrings("%APPDATA%")
 
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
 
Set objNormalTemplate = objWord.Documents.Open(appDataPath & "\Microsoft\Templates\Normal.Dotm")
normalTemplateVersion = GetVersionNumber(objNormalTemplate)
objNormalTemplate.Close
 
Set objSetupWordCitrix = objWord.Documents.Open(appDataPath & "\Word\Support\SetupWord.dotm")
setupWordCitrixVersion = GetVersionNumber(objSetupWordCitrix)
If normalTemplateVersion < setupWordCitrixVersion Then
objWord.Run "Main"
End If
objWord.Quit
End If
 
 
Function CDPExists(objDoc, cdpName)
Dim cdp
CDPExists = False
For Each cdp In objDoc.CustomDocumentProperties
If cdp.Name = cdpName Then
CDPExists = True
Exit Function
End If
Next
End Function
 
Function GetVersionNumber(objDoc)
lngVersionNumber = 0
If CDPExists(objDoc, "Version") Then
lngVersionNumber = CLng(objDoc.CustomDocumentProperties("Version").Value)
End If
GetVersionNumber = lngVersionNumber
End Function

 

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

VSTOHow to uninstall vsto

“%CommonProgramFiles%\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe” /uninstall \\path\to\vsto\AddIn.vsto

How to silently install vsto

“%CommonProgramFiles%\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe” /install /silent \\path\to\vsto\AddIn.vsto

How to install via group policy

Bat Login Script

@echo off
REM Check for presence of vsto key first if not found run installer
reg query HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins\DCCWord2010 /e >nul 2>nul || ("C:\Program Files\Common Files\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe" /I "\\path\to\vsto\AddIn.vsto" /S)

Turns out using this

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Runonce

 

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

vbs file via run registry  ( The advantage of using this is it runs once for each user however they will need to log in twice if they are logged off!)

 

 Turns out using this registy value HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Runonce The user needs to be a local administrator…

So we had to put the value inside here : HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run , and let the file remove it after running

Group Policy Registry key to add to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Link as my hosting doesn’t allow these file types! http://pastebin.com/SNzSP2Xw

vbs script

Dim commandDim WshShellcommand=chr(34) & "%CommonProgramFiles%\Microsoft Shared\VSTO\10.0\VSTOInstaller.exe" & chr(34) & " /Silent /Install " & chr(34) & "\\path\to\vsto\AddIn.vsto" & chr(34)
Set WshShell = WScript.CreateObject("WScript.Shell")WshShell.Run command
'Delete the Run Key to make sure it runs once - we cannot put this in the run one registry due to users not being administrators
Const HKEY_CURRENT_USER = &H80000001
strComputer = "." Set objRegistry=GetObject("winmgmts:\\" & _  strComputer & "\root\default:StdRegProv") strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Run"strValueName = "ExternalPDatabases"
objRegistry.DeleteValue HKEY_CURRENT_USER, strKeyPath, strValueName

 

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Rebuilding profiles can sometimes be time consuming especially documenting settings. I created a script to backup the current printers the User had and wrote it to a test file ( including Default Printer ) so that this could be run to backup , then restored from this file in the Restore Script

Backup Printer List

Const ForWriting = 2
 
Set objNetwork = CreateObject("Wscript.Network")
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set DefPrinters = objWMIService.ExecQuery _
     ("Select * from Win32_Printer Where Local = FALSE AND Default = True")
 
For Each DefPrinter in DefPrinters
    strText = strText & "Default : " & DefPrinter.Name & vbCrLf
Next
 
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = FALSE AND Default = False")
 
For Each objPrinter in colPrinters
    strText = strText & objPrinter.Name & vbCrLf
Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objFile = objFSO.CreateTextFile _
    ("h:\Printers.txt", ForWriting, True)
 
 
objFile.Write strText
 
objFile.Close

 

Restore Printer List

 

Const ForReading = 1
Const TristateUseDefault=-2
Set wshnet = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile ("h:\Printers.txt", ForReading, True, TristateUseDefault)
 
Do Until objFile.AtEndOfStream
	strNextLine = objFile.Readline
	if left(strNextLine,10) = "Default : " THEN
		strNextLine = replace(strNextLine, "Default : ", "")	
		wshnet.AddWindowsPrinterConnection strNextLine
		wshnet.SetDefaultPrinter strNextLine
	END IF
	wshnet.AddWindowsPrinterConnection strNextLine
Loop
 
objFile.Close

 

 

How to reset Printers after backup then restore

 

Const ForWriting = 2
 
Set objNetwork = CreateObject("Wscript.Network")
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set DefPrinters = objWMIService.ExecQuery _
     ("Select * from Win32_Printer Where Local = FALSE AND Default = True")
 
For Each DefPrinter in DefPrinters
    strText = strText & "Default : " & DefPrinter.Name & vbCrLf
Next
 
Set colPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Local = FALSE AND Default = False")
 
For Each objPrinter in colPrinters
    strText = strText & objPrinter.Name & vbCrLf
Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strDirectory = objFSO.BuildPath(WshShell.SpecialFolders("Desktop"), "Printers.txt")
 
Set objFile = objFSO.CreateTextFile _
    (strDirectory, ForWriting, True)
 
 
objFile.Write strText
 
objFile.Close
 
 
Const HKEY_CLASSES_ROOT  = &H80000000
Const HKEY_CURRENT_USER  = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS         = &H80000003
 
' Object used to get StdRegProv Namespace
Set wmiLocator = CreateObject("WbemScripting.SWbemLocator")
 
' Object used to determine local machine name
Set wshNetwork = CreateObject("WScript.Network")
 
' Registry Provider (StdRegProv) lives in root\default namespace.
Set wmiNameSpace = wmiLocator.ConnectServer(wshNetwork.ComputerName, "root\default")
Set objRegistry = wmiNameSpace.Get("StdRegProv")
 
' Deletes Key with alle subkeys
sPath = "Printers"
 
lRC = DeleteRegEntry(HKEY_CURRENT_USER, sPath)
 
Function DeleteRegEntry(sHive, sEnumPath)
' Attempt to delete key.  If it fails, start the subkey
' enumration process.
lRC = objRegistry.DeleteKey(sHive, sEnumPath)
 
' The deletion failed, start deleting subkeys.
If (lRC <> 0) Then
 
' Subkey Enumerator
   On Error Resume Next
 
   lRC = objRegistry.EnumKey(HKEY_CURRENT_USER, sEnumPath, sNames)
 
   For Each sKeyName In sNames
      If Err.Number <> 0 Then Exit For
      lRC = DeleteRegEntry(sHive, sEnumPath & "\" & sKeyName)
   Next
 
   On Error Goto 0
 
' At this point we should have looped through all subkeys, trying
' to delete the registry key again.
   lRC = objRegistry.DeleteKey(sHive, sEnumPath)
 
End If
 
End Function
 
Const ForReading = 1
Const TristateUseDefault=-2
Set wshnet = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strDirectory = objFSO.BuildPath(WshShell.SpecialFolders("Desktop"), "Printers.txt")
 
Set objFile = objFSO.OpenTextFile (strDirectory, ForReading, True, TristateUseDefault)
 
Do Until objFile.AtEndOfStream
              strNextLine = objFile.Readline
              if left(strNextLine,10) = "Default : " THEN
                             strNextLine = replace(strNextLine, "Default : ", "")            
                             wshnet.AddWindowsPrinterConnection strNextLine
                             wshnet.SetDefaultPrinter strNextLine
              END IF
              wshnet.AddWindowsPrinterConnection strNextLine
Loop
 
objFile.Close
 
x=msgbox("Printer ReMap Complete" ,0, "Tech Services")

 

VN:F [1.9.22_1171]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)