Category: Code

Resize all images in a Word document with #vbs #macro

TLDR Quick and dirty macro to resize all images in a word document to 145 mm. I am lazy like that. Get the gist of it here. The “Why” Ok, so. I am a consultant, as you may know. And as a consultant, people pay me to do stuff and expect me to be efficient and deliver with high quality. Part of that entails writing documentation containing how I do stuff, where I do it and often why I do it. This documentation is expected to be detailed enough to make sure my customers will be able to follow the steps and produce a copy of my work. Most of the time these document are a big part of the disaster recovery processes and might have to be executed by the on-call staff with no previous knowledge of the product. This means screenshots. Lots of screenshots. And while templates are nice and help me out a lot, the screenshots have to be from the actual installation and not some copy-pasta from a previous assignment. To help me out, I use the excellent free program Greenshot to quickly generate screenshots into a WIP-folder (or directly into Word if that is appropriate) and that makes it possible to grab snapshots of the process without having to pause all the time to PrtScn-Alt+Tab-Ctrl+V-Crop-Ctrl+C-Alt+Tab-Ctrl+C. I simply press PrtScn and go on with the installation. Being a bit pedantic, doesn’t help me out here though as I find myself selecting and resizing all the images in the Word document I have produced. This is a very time-consuming process and not the most exact one either. I want all images to be of scaled, if necessary, to an equal maximum width that fits comfortably withing the margins of the document template my company has prepared for me. So I created a fairly simple macro to do it for me. The “How” If you are un-familiar with macros in Word, you will find them under the “View” tab. Clicking on “Macros” will give you the option to execute the ones you have, or you can create your own using VBA (Visual Basic for Applications). To create one, select where you want it saved (normal.dotx in my case, as I want it readily available) and enter a name for it. This will bring up the Visual Basic for Applications IDE, where you can write your own fun stuff.

Quick-Hack: Send SMS through Powershell [#powershell]

Decided to do a quick-hack/fast-publish on this one as I have had a bit less time to create a nice clean production-ready version as of yet… and people has been asking about how far off the article is. What this script does is to send a text message using a GSM/GPRS modem connected to a local (or LAN-connected with local drivers) serial port using Powershell. Disclaimer! This script “works” but is not fit for production. See it as an example of the general concept to evolve and adapt into something worthy of production use. What’s missing in the latest iteration is: A working Event-Handler to deal with asynchronous call-backs. Support for AT+MSGW (write to modem memory) Reusing messages in modem memory for multiple recipients. Various error- and exeption-handlers. Actually verifying that the modem is AT-capable. Querying the system for available modems and their ports. The Script So, a short note before digging into the script. Prerequisites for this script is that you have identified which COM-port to use and it’s supported baud-rates and whether it supports DTR or not. If you do not know what the hell I am talking about, you could probably have it work with my preconfigured settings anyway. If you are unsure about if your modem supports AT commands you could open a serial connection to the modem using Hyperterminal or PuTTY and run AT+CMGF=1. If supported, the return should be OK. If it is not supported (you get ERROR instead) you would have to use PDU-mode which require a bit of hex-encoding of your messages. This is nothing I have had to do yet and will not be including in this script. Maybe in the future. Maybe. So, looking a some powershelling then. First thing would be to connect to the modem. # Create your instance of the SerialPort Class$serialPort = new-Object System.IO.Ports.SerialPort# Set various COM-port settings$serialPort.PortName = "COM1"$serialPort.BaudRate = 19200$serialPort.WriteTimeout = 500$serialPort.ReadTimeout = 3000$serialPort.DtrEnable = "true"# Open the connection$serialPort.Open()

w3Socket in VBScript

I’ve been doing quite a lot of VBScripting in a couple of projects lately. The current one requires med to connect to a couple of telnet servers and look for… stuff. Since we’re not in VB6 och .Net i cannot simply user Winsock as normally due to the lack of licensing features in VBS/WSH. Thankfully for me, Dimac has release a nifty little component for free that makes talking telnet in VBS very simple. I thought that, hey! I must share this! So here’s an example in Classic VBS: Dim oTelnetoTelnet = CreateObject(Socket.Tcp)With oTelnet.DoTelnetEmulation = True.TelnetEmulation = TTY.Host = 192.168.242.1:23.Open.WaitFor SLUSSEN login:.SendLine ANiftyUsename.WaitFor Password:.SendLine [email protected] ~ #.SendLine ifconfigMsgBox .Buffer.CloseEnd WithSet oTelnet = Nothing Not very hard at all. This one is connecting to my router (with fake user/pwd… DUH!) and there’s no support for setting the prompt type there, thus the “~ #”. The result of running this script with correct login info gives me a popup with the routers NIC-configuration.