For some reason, you can’t do this in vbscript:
It throws this error:
Microsoft VBScript compilation (0x800A0402)
Expected integer constant
Instead, you have to do this:
x = 15
For some websites that I run I fairly regularly need to resize some photos to a certain width. ImageMagick is a great application for command line image manipulation.
The syntax to convert a file to a specific width and the corresponding height (which is what I want to do) is:
convert InputName.jpg -resize 550x Outputname.jpg
What I’ve found useful is running command line scripts through vbscript:
Set wshShell = WScript.CreateObject(“WSCript.shell”)
wshShell.Run “cmd.exe /C convert InputName.jpg -resize 550x Outputname.jpg”, 0, True
I know you can do all sorts of clever things from the command line, but not quite as many as vbscript can. I find it really useful to be able to access all of the vbscript logic, and read and write text files and so on, while at the same time being able to issue commands to the command prompt.
I needed to write a script to remove passwords from Excel files today (I did know, the password, this wasn’t a cracking exercise).
The function is pretty self explanatory. it takes two arguments, and removes the password.
Filenamewithpath = “D:\VBscripts\test.xlsx”
Password = “6tg4HHE”
Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = FALSE
objExcel.DisplayAlerts = FALSE
Set objWorkbook = objExcel.Workbooks.Open(Filenamewithpath„„Password)
objWorkbook.Password = “”
set objExcel = nothing
set objWorkbook = nothing
So, I’ve been messing around automating Excel recently. And one thing I’ve discovered is that Microsoft has loads of “constants” which don’t work in vbscript.
It’s kind of annoying – but it does mean it helps with identifying what, say, 53, will do (it draws a box with an arrow).
Luckily, there’s a list of them all, which I’m duplicating here, just in case the original site disappears.
When I’m outputting text or data, I tend to output it into text files. They’re simple and clean. But people often want Excel files. Yeah, I don’t understand it either.
I sat down today and had a look at writing extra data to an Excel file, and found that adding data to the last line isn’t always simple.
set objExcel = CreateObject(“Excel.Application”)
Set objWorkbook = objExcel.Workbooks.Open(FILENAME)
LastRow = objExcel.ActiveSheet.UsedRange.Rows.Count
objExcel.Cells(LastRow + 1, 1).Value = “Test value”
This function loads an Excel file, finds the first blank row and writes to it, then saves and closes the file. And does so, all invisibly to the user.
I spent a silly amount of time yesterday troubleshooting an access error. It kept saying I had an error in my SQL, yet I knew it was correct.
In the end, it turned out one of the columns had a reserved name.
The moral of the story is, always enclose your column names with square brackets:
SELECT [Column] FROM [Table]
It’s easy to get lazy. Just don’t. Always put square brackets round, and you won’t have this problem.
This is quite a nifty little bit of VBScript for adding something to the user’s clipboard.
strCopy = “This text has been copied to the clipboard.”
Set objIE = CreateObject(“InternetExplorer.Application”)
objIE.document.parentwindow.clipboardData.SetData “text”, strCopy
Have to admit, I’m beginning to go off VB Script. Obviously, it’s incredibly useful, since it works on every Windows machine, and lets you do things like make HTAs, scripts etc.