Listed below are solutions for specific scripting tasks in VBScript/JScript/MS Excel/Access VBA. We'll hope its helpful for you. These work both in 32-bit as 64-bit client/Office environments.
Common script to insert into your own script to get access to new functionality.
Dim CTX, WST
Set CTX = CreateObject("Microsoft.Windows.ActCtx")
CTX.Manifest = ScriptFolder & "\WSTools64.sxs.manifest" ' or WSTools32.sxs.manifest if using a 32-bit client
Set WS = CTX.CreateObject("WS.Tools")
Const HASH_CRC32 = 0, HASH_MD5 = 1, HASH_SHA1 = 2, HASH_SHA256 = 3, HASH_SHA512 = 4
Const SORT_Ascending = 0, SORT_Decending = 1
Const SORT_Case = 0, SORT_IgnoreCase = 1
Const ENC_ASCII = 0, ENC_UTF8 = 1, ENC_UNICODE = 2, ENC_UTF8BOM = 3
Const HTTP_Get = 0, HTTP_Post = 1, HTTP_Put = 2, HTTP_Patch = 3, HTTP_Delete = 4
How to calculate various (cryptographic) hashes of a string in VBScript or VBA?
txt = "Grüße to the World"
crc32 = WS.Hash(txt, HASH_CRC32)
md5 = WS.Hash(txt, HASH_MD5)
sha1 = WS.Hash(txt, HASH_SHA1)
md256 = WS.Hash(txt, HASH_SHA256)
How to Base64 encode and decode a string in VBScript or VBA
txt = "Grüße to the World"
b64 = WS.Base64Enc(txt) ' Use Base64Dec to decode back to string
How to sort an 1- or 2-dimensional array in VBScript or VBA?
Dim i, ar2(9999, 2) '2-dimensional array,: a table with 10000 rows (0..9999) with 3 columns (0..2) each
For i = 0 To UBound(ar2)
ar2(i, 0) = CLng(Int((100000 - 100 + 1) * Rnd + 100)) 'random values between 100 and 100000
ar2(i, 1) = CLng(100 + i)
ar2(i, 2) = CLng(200 - i)
Next
WScript.Echo "Part of unsorted 2-dimensional array: "
For i = 0 To 10
WScript.Echo ar2(i, 0) & vbTab & ar2(i, 1) & vbTab & ar2(i, 2)
Next
' Case-insensitive sort on first column of array ar2 in ascending order
WS.ArraySort ar2, 0, SORT_Ascending, SORT_IgnoreCase
WScript.Echo "Part of sorted 2-dimensional array: "
For i = 0 To 10
WScript.Echo ar2(i, 0) & vbTab & ar2(i, 1) & vbTab & ar2(i, 2)
Next
How to ask user input with a dynamic dialog?
Dim i, r
r = WS.DialogInput("User Information", "First Name", "Last Name", "*Password", "#Length", "@@Birth Date", "!Newsletter")
For Each i In r
WScript.Echo i
Next
Prefix field name with # for numeric input, * for password, ! for a checkbox and @ for a date.
How too set/get clipboard text?
WS.ClipboardSet("The quick brown fox")
Dim txt
txt = WS.ClipboardGet
How to call a WIN32 DLL-function in VBScript?
Dim user
user = Space(1024) '""
WS.DllFunction "Kernel32.dll", "ExpandEnvironmentStringsW", "Hello %username%", user, 1024
WScript.Echo user
WScript.Echo "Call Win32 MessageBoxW"
WScript.Echo(WS.DllFunction("USER32.DLL", "MessageBoxW", 0, user, "Hello", 1))
How to send e-mail with VBScript using any SMTP server?
WS.MailSetup "smtp.gmail.com" , "user@gmail.com" , "mypassword", "user@gmail.com"
WS.MailSend "info@destination.com", "Test ABCD", "Just a quick test!"
Sometimes you need to adjust security (like with GMail) or add an "application-specific" password to your account.
How to set the Windows sound volume from VBScript?
WS.AudioVolume(50) 'Set speaker volume to 50%
How to change monitor brightness from login script?
WS.MonitorBrightness(30) 'Set brightness of all supported displays to 30%
How to trim all spaces and tabs from a line?
WS.TextTrim(txt, " " & vbTab)
How to AES-encrypt/decrypt text in VBA?
txt = "Helló World!"
enc = WS.Encrypt(txt, "mypassword")
dec = WS.Decrypt(enc, "mypassword")
How to get all output of a command or program (stdout and stderr)?
output = WS.Cmd("dir c:\")
How to achieve super fast string concatenation in VBA or VBScript?
t1 = Timer
For i = 1 To 100000
WS.StringAdd i & ", "
Next
r = WS.StringValue
WScript.Echo Timer - t1
WScript.Echo Left(r, 100)
'(Don't) try r = r & i & ", " ...
Reading and saving UTF-8 files with or without BOM
filePath = WS.DialogSelectFile("Open File", "", "Text File|*.txt;*.cmd;*.bat;*.ini|All files|*.*")
If filePath <> "" Then
txt = WS.FileRead(filePath, ENC_UTF8) 'Use FileWrite to save text to a file.
End If
WS.File
Working with JSON data in VBA or VBScript?
Set json = WST.JsonParse("[ ""Test 1"", 2, 3 ]")
Debug.Print json.Item(0) 'or json.[0]
Debug.Print WST.JsonStringify(json)
For more information about WSTools visit https://wizardsoft.nl/products/wstools