I have to admit...

Dec 18, 2008 09:50

I just used VBScript.

Basically, I've got to do these tedious email work requests as a part of my job (tedious but admittedly necessary). And I was just asked to stop batching requests together, which means 8x more of these things.

Typically I just copy an old one I sent previously and change the info, but that is still painful AND it leads to a lot of typo emails where I forget to replace something somewhere for the new request.

So as a programmer, naturally, I decide to script the process.

First off, let me tell you, figuring out how to do this was not easy. There are no tutorials. The windows knowledge base thing has a 4 step list that gets you into the 'form editor' and thats it. You're on your own.

There's a toolbar to drop in some buttons and labels and things. And fooling around I also manage to find a way to create 'user defined properties' and dragging those into the form editor automatically creates a label and text input box for the user to input those values.

But then what? How do you connect that to the actual content of the email?

As it turns out, you have to resort to scripting for EVERYTHING. The 'form editor' is basically a Visual Basic UI editor inside of an email window. So eventually you find the 'view code' button, which pops open a text editor window. Its basically a stripped down version of Wordpad, stripped because it has none of the functionality outside of the text input box. It supports RTF, because if I copy syntax-colored code from a real IDE into the window it maintains its formating, but it doesn't do/allow its own syntax highlighting.

Anyhow, what I have is this: a Visual Basic UI editor, a text input box that expects code of some sort, and the internet.

After enough playing around I manage to figure out some event functions (or "Sub" routines in VBScript) to hook changes in my form inputs. I can muck around in the email's DOM object representation and set the subject line. Finally I find out how I can modify the body of the email-- by parsing its HTMLbody value as a string.

So basically I have to re-write HTML? Not good enough. Instead, I seed the body of template email with easy to parse tags where I want my data to end up (like this: [DATAFIELD1] ) and simply do a search and replace on the HTMLbody string when I want to insert my values.

Except now I have no way of changing the data again if the user changes their input before sending-- so I have to keep an unchanged copy the HTMLbody string and simply modify and use that as the HTMLbody. So the next time they change data I dont look for tags in the email body, I look for them in the copy of the email body.

Except that for some reason I cant store a copy of the HTMLbody. The error message doesn't make any sense, but if I take out the code that tries to copy it works.. so I create a whole new MailItem object, and set IT'S HTMLbody property to copy my current email's HTMLbody and that works.

And after a day of tweaking I get something that generally works, except when the email is received, it cannot be viewed in the preview pane due to 'security reasons'? Whatever, the subject line has the info they need before opening the email.. good enough.

But yeah.. basically the Microsoft Way for automating office programs is still the same: use a crappy programming language and a poorly documented DOM to piece together enough functionality with a minimum of negative side effects.

If the language was Python or JScript instead of VBScript, it still would have been just as bad, but I wouldn't have felt as bad doing it.
Previous post Next post
Up