To Handle Ad Blocker

Showing posts with label VBScript. Show all posts
Showing posts with label VBScript. Show all posts

Enhance Your Computer's Performance with VBScript

I bought my laptop on 1st November 2013. I started feeling that my laptop is getting slower as I was installing heavy programming software like dot net , java , SQL Server etc. It’s inevitable that all computers will experience slower speeds at some point. As software becomes more demanding, the way in which your PC reacts to these changes can have a big impact on its performance.

I tried below major cost-effective alterations to my 
laptop to make it run faster. These major alterations may not seem like they will speed up your computer performance, but combined they can make it possible to keep your PC functional for some time:
  • Uninstall unnecessary bloatware
  • Limit the programs at startup
  • Add more RAM to your PC
  • Check for spyware and viruses
  • Use Disk Cleanup and de-fragmentation
  • Consider a startup SSD
  • Take a look at your web browser
I've used all the above alterations but I feel that they are the temporary solution. For a permanent solution, Today I'm sharing my custom solution that i had prepared myself for my laptop. After deploying this custom solution, I've never faced the performance related issue.

How to create VBScript

Open a Notepad file. Copy and paste below code in the notepad file and save the program with .vbs extension anywhere or with any name (I'll prefer SystemRefresh.vbs). To test, Double click on the file to run the program. As you will double click, it will start running in the back end.

 On Error Resume Next
 Dim vRefreshableMemoryFolderLocation(3)
 Dim objFSO	: Set objFSO = CreateObject("Scripting.FileSystemObject")
 Dim objShell	: Set objShell = CreateObject("WScript.Shell")

 vKeepRefreshing = "TRUE"
 vRefreshableMemoryFolderLocation(0) = "C:\Windows\Prefetch"
 vRefreshableMemoryFolderLocation(1) = "C:\Windows\Temp"
 vRefreshableMemoryFolderLocation(2) = "C:\Windows\Downloaded Program Files"
 vRefreshableMemoryFolderLocation(3) = "%USERPROFILE%\AppData\Local\Temp"
 
 Do While vKeepRefreshing = "TRUE"
 	For I = LBound(vRefreshableMemoryFolderLocation) To UBound(vRefreshableMemoryFolderLocation)
 		vCurrFolderPath = vRefreshableMemoryFolderLocation(I)
 		If InStr(vCurrFolderPath,"%") > 0 Then
 			vCurrFolderPath = objShell.ExpendEnvironmentStrings(vCurrFolderPath)
 		End If
 		For Each objFile In objFSO.GetFolder(vCurrFolderPath).Files
 			objFSO.DeleteFile(vCurrFolderPath & "\" & objFile.Name)
 		Next
 		For Each objFolder In objFSO.GetFolder(vCurrFolderPath).SubFolders
 			objFSO.DeleteFolder(vCurrFolderPath & "\" & objFolder.Name)
 		Next
 	Next
 	WScript.Sleep 15000
 Loop

How to run VBScript

If you will run the program after saving anywhere on the system. It will stop running as you will restart the system. Solution of this problem is that we place this Script file on the Startup folder location so that whenever system restart then it will start running itself. Perform the below steps to do so:
  1. Open Run Command by pressing Win + R
  2. In Run Command Window, type anyone of the below line and press Enter

    %USERPROFILE%\AppData\Roaming\Microsoft\Start Menu\Programs\Startup
    OR
    shell:startup

  3. Copy and Paste the SystemRefresh.vbs in the Startup Folder and close the folder.

It might be possible that anti-virus or any third party tool installed in your system block this script from executing. In this case, you have to mark this file safe or you have to unblock it. I hope this post is helpful. If you have any questions, feel free to comment it below.


Happy Learning.. :-)

VBScript to send email using Outlook

In this Article, we will learn how to send email using VBScript programs using Outlook.

During RPA Development for a process, we requires to send Automated emails response/notifications to notify the user for a processing start or it's end. We also requires to send output of the RPA Execution. For this purpose most of the Automation Tools (like Automation Anywhere) requires SMTP Host name and its port number.


It happens that sometimes IT team do not provide you the SMTP host name and port number due to security purpose. In such cases, you have two solutions as options:


1- Automated Outlook (Not Recommended): Mimic all the action of human to send email from Outlook. Such as open a new email message in outlook, write recipient email address in "To" field, write some silly text in body and then click on sends button.

2- Use Custom Program (Recommended): It will be a script or it could be a .Dll file as well. By the time you open a new email message in outlook, such custom programs shoots mail to recipient.

If you have Outlook configured with an email address in your machine then you can use below script for sending automated email as Custom Program Solution.

On Error Resume Next
vEmailFrontUserID = WScript.Arguments.Item(0)
vEmailAddressTo = WScript.Arguments.Item(1)
vEmailAddressCC = WScript.Arguments.Item(2)
vEmailSubject = WScript.Arguments.Item(3)
vEmailAttachment = WScript.Arguments.Item(4)
vEmailBody = WScript.Arguments.Item(5)
vEmailSignature = WScript.Arguments.Item(6)

vEmailBody = Replace(vEmailBody, vbLf, "<br>")
vEmailBody = vEmailBody & "<br>"

Dim vObjOutlookApp : Set vObjOutlookApp = Wscript.CreateObject("Outlook.application")
Dim vObjOutlookAppNameSpace : Set vObjOutlookAppNameSpace = vObjOutlookApp.getNamespace("MAPI")
Dim vObjOutlookAppNewMailItem : Set vObjOutlookAppNewMailItem = vObjOutlookApp.CreateItem(olMailItem)
Dim vObjFSO : Set vObjFSO = CreateObject("Scripting.FileSystemObject")

vObjOutlookAppNewMailItem.SentOnBehalfOfName = vEmailFrontUserID
vObjOutlookAppNewMailItem.To = vEmailAddressTo
vObjOutlookAppNewMailItem.CC = vEmailAddressCC
vObjOutlookAppNewMailItem.Subject = vEmailSubject

If vEmailAttachment <> "" Then
    vAttachmentsArr = Split(vEmailAttachment,";")
    For i = LBound(vAttachmentsArr) To UBound(vAttachmentsArr)
        If vAttachmentsArr(i) <> "" Then
            If vObjFSO.FileExists(Trim(vAttachmentsArr(i))) = True Then
                vObjOutlookAppNewMailItem.Attachments.Add Trim(vAttachmentsArr(i))
            End If
        End If
    Next
End If

If vObjFSO.FileExists(vEmailSignature) = True Then
    vObjOutlookAppNewMailItem.Attachments.Add vEmailSignature , 1, 0
    vEmailBody = vEmailBody & "<BR> <IMG src=""" & StrReverse(Mid(StrReverse(vEmailSignature),1,(InStr(StrReverse(vEmailSignature),"\")-1))) & """><BR>"
End If

vEmailBody = "<HTML><BR>" & vEmailBody & "<BR></HTML>"
vObjOutlookAppNewMailItem.HTMLBody = vEmailBody
vObjOutlookAppNewMailItem.Send

Set vObjOutlookApp = Nothing
Set vObjOutlookAppNameSpace = Nothing
Set vObjOutlookAppNewMailItem = Nothing
Set vObjFSO = Nothing

If Err.Number <> 0 Then
    Set vObjOutlookApp = Nothing
    Set vObjOutlookAppNameSpace = Nothing
    Set vObjOutlookAppNewMailItem = Nothing
    Set vObjFSO = Nothing
End If


I hope this post is helpful. If you have any questions, feel free to comment it below.

Happy Learning.. 😏

Prevent Desktop Lock Or Screensaver

Due to administrative rights or group policy, sometimes system gets locked or screensaver comes up after timeout. 

In this situation, most of the automation tools fails to perform the action on the application. There is a little utility (Script Program) that runs in your system tray and prevents your computer from locking or going into screensaver mode as it normally would when system is idle or no automation is running.

Even many worker of the Big company has a problem with auto logoff screensaver and they use this little utility to break rules, to make a system works all time, even he goes pi. 😄 I have found it very useful and I thought I would share it.

How to create VBScript

Microsoft VBScript (Visual Basic Script) is a general-purpose, lightweight and active scripting language developed by Microsoft that is modeled on Visual Basic. Let's create a simple program for preventing desktop lock or screensaver:

Step 1: We will create an object of WScript.Shell which is an instance of the windows shell allowing us to run commands against the windows shell from within our VBScript program. It is a powerful object that enables us to query and interact with various aspects of the Windows shell. We can display information to the user, run applications, create shortcuts, work with the Registry, and control Windows' environment variables.

 Dim objWshell : Set objWshell = CreateObject("WScript.Shell")


Step 2: We will create a Boolean type variable and assign it default value false

 vFlagStopScreensaver = False


Step 3: We will create Do Until loop having vFlagStopScreensaver - False as its condition for executing its block of code.

 Do Until vFlagStopScreensaver = False
          
 Loop     

Step 4: We will place the below two line of the code in the above loop's execution block. First line of code will make our VBScript program to wait/sleep for 60000 milliseconds (1 minutes = 60 Seconds = 60000 milliseconds). Second line of code send/press F15 Key of the keyboard which has no function assign.

 WScript.Sleep 50000
 objWshell.SendKeys "{F15}"    


Complete code will look like as appearing in the below image. Now save the program with .vbs extension anywhere or with any name (I'll prefer StopScreenSaver.vbs). To test, Double click on the file to run the program. As you will double click, it will start running in the back end.

 Dim objWshell	:	Set objWshell = CreateObject("WScript.Shell")
 vFlagStopScreensaver = False
 Do Until vFlagStopScreensaver = False
	 WScript.Sleep 50000
	 objWshell.SendKeys "{F15}"
 Loop


How to run VBScript

If you will run the program after saving anywhere on the system. It will stop running as you will restart the system. But our requirement is that it run when Robot will login the system and perform its actions/activity.

Solution of this problem is that we place this Script file on the Startup folder location so that whenever system restart then it will start running itself. Perform the below steps to do so:

  1. Open Run Command by pressing Win + R
  2. In Run Command Window, type anyone of the below line and press Enter

    %USERPROFILE%\AppData\Roaming\Microsoft\Start Menu\Programs\Startup
    OR
    shell:startup

  3. Copy and Paste the StopScreenSaver.vbs in the Startup Folder and close the folder.

It might be possible that anti-virus or any third party tool installed in your system block this script from executing. In this case, you have to mark this file safe or you have to unblock it. I hope this post is helpful. If you have any questions, feel free to comment it below.


Happy Learning.. :-)