Showing posts with label VBSript. Show all posts
Showing posts with label VBSript. Show all posts

Tuesday, June 4, 2019

Rename excel sheet using VBScript


VBSCript code to rename excel sheet;Rename Excel Sheet VBScript;Excel sheet rename using VBScript;Excel sheet rename programmatically VBScript




How to rename Excel worksheet from VBScript


1) The following code renames an excel worksheet in VBScript.

To rename an excel sheet we need to open the excel file, rename the sheets, save and close it. Now we can start coding to open and rename the first excel sheet from "Sheet1" to "NewName".


1. Create a file ExcelRename.vbs and paste the below VBScript  Code

':: How to rename Excel worksheet from VBScript
'----------------------------------------------

'create the excel object
	Set objExcel = CreateObject("Excel.Application") 

'view the excel program and file, set to false to hide the whole process
	objExcel.Visible = True

'turn off screen alerts
        objExcel.DisplayAlerts = False

'open an excel file (make sure to change the location) .xls for 2003 or earlier
	Set objWorkbook = objExcel.Workbooks.Open("C:\Viji\Test.xlsx")

'Use objWorkbook.Worksheets.count to get the count of sheets
'Get the first sheet
        Set objWorksheet = objWorkbook.Worksheets(1) 
'Rename the sheet
       objWorksheet.Name = "NewName"

'save the existing excel file. use SaveAs to save it as something else
	objWorkbook.Save

'close the workbook
	objWorkbook.Close 

'exit the excel program
	objExcel.Quit

'release objects
	Set objExcel = Nothing
	Set objWorkbook = Nothing

2. Double click ExcelRename.vbs to execute. The sheet has been renamed from "Sheet1" to "NewName"


After rename



It's so simple !!!! Happy Coding.

Monday, May 27, 2019

VBScript rename file names in folder

VBScript code rename files;find and replace file names windows;find and replace file names VBScript;VBScript Find and Replace in File Names










1) Create a file rename_script.vbs and paste the below code. VBScript can be written using Windows Notepad or any other plain text editor.

 Set objFso = CreateObject("Scripting.FileSystemObject")  
 Set Folder = objFSO.GetFolder("C:\Users\MyName\Documents\MyDirectory\")  
 For Each File In Folder.Files  
   sNewFile = File.Name  
   sNewFile = Replace(sNewFile,"find","_")  
 if (sNewFile<>File.Name) then  
   File.Move(File.ParentFolder+"\"+sNewFile)  
 end if  


2) Save the file and double click to execute.

Note: "Swim at your own risk", the code doesn't have extensive error handling. Just be sure to replace the "C:\Users\MyName\Documents\MyDirectory" with the path to your directory.

Friday, April 16, 2010

Log off Remote Desktop Users

There are serveral DOS commands to forcibly log off the remote user. But we need to be an administrator of the target machine.

How to query for users on a machine?

First we can query to find out what users have a session on a remote machine? Windows provides the "quser" command which we can use to query for the sessions running on that machine.

The format is as follows:

quser /server:ServerName

Here is a screenshot running this command against a remote machine. Notice that it shows the User Name, state and ID of the session.


Query Users on a machine



How to log off a user of a machine?

How can we force the  user to log off. There is a handy little command called "logoff" that we can use to force a user to log off a machine based on their Session ID.

The format is as follows:

logoff SessionId /Server:ServerName


Log off User

Notice that I have used the Session Id which I got from the above.

Note: If you remotely log off a user, their log session goes away which could mean the unsaved data is lost, or if the user in the middle of an activity, we might be in trouble.


Of course some things are easier to do using a script:


Here is the VBScript code, which allows the user to remotely log off.

strComputer = ""

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

ObjOperatingSystem.Win32Shutdown(0)

Next

Save the code in .vbs format and execute it.