Message Box Syntax
Int = MsgBox(prompt[,buttons][,title][,helpfile,context])
Message Box Responses / Button Values
1 = vbOK 2 = vbCancel 3 = vbAbort 4 = vbRetry 5 = vbIgnore 6 = vbYes 7 = vbNo
Message Box Types
0 = vbOKOnly 1 = vbOKCancel 2 = vbAbortRetryIgnore 3 = vbYesNoCancel 4 = vbYesNo 5 = vbRetryCancel 16 = vbCritical 32 = vbQuestion 48 = vbExclamation 64 = vbInformation ... and so on ...
Examples of Message Box
msgbox "First Line" &vbLf& "Second Line" msgbox "Message Spaced" &vbTab& "By a Tab"
Input Box
String = InputBox(String prompt, [String title], [String default], [Integer xpos], [Integer ypos], [String helpfile], [Integer context])
Examples of Input Box
Dim name name = InputBox("What is your name?", "Title", "...enter your name here...")
Declare Variable
Dim var_a, var_b,var_c
Debugging Function
Option Explicit On Error Resume Next
Comparison Operators
= <> < > <= >=
Logical Operators
And Or Not Xor (Not + Or)
Concatenation Operators
+ &
Arithmetic Operators
+ - * / % (Remainder of Division) ^ (Exponentiation)
Comment Operator
' this line will not be executed
Conditional IF
If var_a=1 Then msgbox "One", vbInformational
If var=1 Then msgbox "One." ElseIf var=2 Or var=3 Then msgbox "Two or Three." Else msgbox "Not One, Two neither Three." End If
Select Case
Select Case var Case "1" MsgBox "Banana" Case "2" MsgBox "Orange" End Select
Do Loop
Do MsgBox "Infinite Looping" Loop
Ending Do Loop
Exit Do
Do Loop Until / While / For
Do Until a = 10 MsgBox "Infinite Looping" i = a + 1 Loop
Do MsgBox "Infinite Looping" i = a + 1 Loop Until a = 10
Do While a <= 10 MsgBox "Infinite Looping" i = a + 1 Loop
For i = 1 to 10 MsgBox i Next
For Each i In array MsgBox i Next
Creating Objects
Set obj = CreateObject("wscript.shell") obj.run "cmd.exe" obj.sendkeys "exit" obj.sendkeys "{ENTER}" obj.sendkeys "(% )(N)" ' this will type the command Alt+Space then N to minimize the window
Executing Shell Commands
CreateObject("wscript.shell").run "calc.exe" CreateObject("wscript.shell").run "c:\file.txt" CreateObject("wscript.shell").run "c:\folder\" CreateObject("wscript.shell").run """c:\folder with space\""" CreateObject("wscript.shell").run "c:\users\%username%\Desktop" getPath = CreateObject("wscript.shell").specialFolders("Desktop")
Special Folders List
AllUsersDesktop AllUsersStartMenu AllUsersPrograms AllUsersStartup Desktop Favorites Fonts MyDocuments NetHood PrintHood Programs Recent SendTo StartMenu Startup Templates
Executing Internet Explorer Commands
CreateObject("InternetExplorer.Application").navigate "https://myip.com" CreateObject("InternetExplorer.Application").visible = True CreateObject("InternetExplorer.Application").statusbar = False CreateObject("InternetExplorer.Application").toolbar = False CreateObject("InternetExplorer.Application").width = 1024 CreateObject("InternetExplorer.Application").hight = 768 CreateObject("InternetExplorer.Application").top = 0 CreateObject("InternetExplorer.Application").left = 0 CreateObject("InternetExplorer.Application").resizeable = False CreateObject("InternetExplorer.Application").fullscreen = 1 Do While CreateObject("InternetExplorer.Application").busy WScript.Sleep 500 Loop CreateObject("InternetExplorer.Application").document.all.item("user").value = "username" CreateObject("InternetExplorer.Application").document.all.item("pass").value = "password" CreateObject("InternetExplorer.Application").document.all.item("form").submit
File System Commands
CreateObject("Scripting.FileSystemObject").fileExists("image.jpg") CreateObject("Scripting.FileSystemObject").folderExists("c:\users") CreateObject("Scripting.FileSystemObject").copyFile "c:\origin\*.zip", "c:\destiny\" CreateObject("Scripting.FileSystemObject").moveFolder "c:\origin\", "c:\destiny\" CreateObject("Scripting.FileSystemObject").moveFile "c:\dir\file.zip", "c:\dir\renamed.zip" CreateObject("Scripting.FileSystemObject").fileExists("c:\dir\file.zip") CreateObject("Scripting.FileSystemObject").createTextFile "c:\dir\file.txt" CreateObject("Scripting.FileSystemObject").createFolder "c:\dir\" CreateObject("Scripting.FileSystemObject").deleteFolder "c:\dir\"
Manipulating Files
Dim file, file2 Const Read = 1, Write = 2, Append = 8 Set file = CreateObject("Scripting.FileSystemObject").openTextFile("c:\dir\file.txt", Read) file.Read(5) file.ReadLine file.ReadAll file.AtEndOfStream file.Close Set file2 = CreateObject("Scripting.FileSystemObject").openTextFile("c:\dir\file.txt", Write) file2.Write "string" file2.WriteLine file2.WriteBlankLines(3) file2.Close Set file3 = CreateObject("Scripting.FileSystemObject").getFile("c:\dir\file.txt") file6.size file6.type file6.path file6.drive file6.attributes file6.attributes = file6.attributes + 1 file6.dateCreated
File Attributes
1 = Read Only 2 = Hidden 4 = System 32 = Archive
WScript Commands
WScript.Quit WScript.Sleep 500 WScript.ScriptName WScript.ScriptFullName WScript.CurrentDirectory
Commands
Boolean = IsNumeric(var) Int = Len(var) String = Right(var, 5) String = Left(var, 5) Replace("Hello All!", "All", "You") Mid("Hello All!", 7, 3) Array = Split("Hello All!", " ") Int = InStr("Hello All!", "All") IsEmpty(var) IsNumeric(var) IsObject(var) IsNull(var) IsDate(var) IsArray(var) VarType(var) TypeName(var) LCase(var) UCase(var) Trim(var) LTrim(var) RTrim(var) IsInArray(array, "A") Abs(-10) Int(1.7) CInt("30") Round(1.7) FormatNumber(999999999, 2) FormatCurrency(999999999, 2) FormatPercent(0.99, 2) FormatDateTime("31-12-2000", vbGeneralDate) FormatDateTime("31-12-2000", vbLongDate) FormatDateTime(Time(), vbLongTime) Space(15) String(20,"-") StrReverse("123456789")
Subroutines
Sub subName(var) MsgBox var End Sub Call subName("Hello!")
With Command
With CreateObject("wscript.shell") .run "calc.exe" End With
Functions
Function funcName(var1, var2) Dim sum sum = var1 + var2 funcName = sum End Function
Arguments Through Command Prompt
Dim args, arg Set args = WScript.Arguments For Each arg in args MsgBox arg Next
Arrays
array1 = Array(0,1,2,3,4,5,6,7,8,9) array2 = Array("a","1","abc","y z") array3(0) = 31 array3(1) = 12 array3(2) = 2000 join = Join(array3, "/") ' will return 31/12/2000 For i = LBound(array1) To UBound(array1) MsgBox array1(i) Next arrayFiltered = Filter(array2, "a") ' will return only the fields that contain "a"
Classes
Class className Private var1 Public var2 Dim var3 Sub Class_Initialize() ' what ever you want to be auto executed when initialized End Sub Private Sub Class_Terminate() ' what ever you want to be auto executed when terminated End Sub Property Let var var1 = var End Property Property Get var var = var1 End Property Function getAbs getAbs = abs(var1) End Function End Class Set num = Net className num.var = -123 MsgBox num.getAbs
Manipulating Windows Registry
Set obj = CreateObject("WScript.Shell") MsgBox obj.RegRead("HKEY_Current_User\Control Panel\Desktop\Wallpaper") MsgBox obj.RegWrite("HKCU\Control Panel\Desktop\Wallpaper", "c:\dir\image.jpg") MsgBox obj.RegDelete("HKEY_Current_User\Control Panel\Desktop\Wallpaper")
Regular Expressions
Dim str = "Is your name Bob?" Set name = New RegExp name.Pattern = "Bob" name.IgnoreCase = True ' the default is false name.Global = True ' the default is false If name.Test(str) Then String = name.Replace(str, "John") Else MsgBos "Bob not found on the original string" End If numOfMatches = name.Execute(str).Count valueOfTheMatch = name.Execute(str).Item(0).Value valueOfTheSubmatch = name.Execute(str).Item(0).Submatches(1)
Environmental Variables
Dim env : Set envs = CreateObject("WScript.Shell") tempFolder = env.Environment("User").Item("TEMP") pullPath = env.ExpandEnvironmentString(tempFolder) pullPath = env.ExpandEnvironmentString("%temp%") env.Environment("User").Item("newVariable") = 999 MsgBox = env.Environment("User").Item("newVariable") env.Environment("User").Remove("newVariable")