Wacky VBA, Bitchen PHP

I was spending a night debugging some heinous VBA code. It was my code, but it sucked. It sucked because it's pretty hard to write glue code that integrates different parts of MS Office. All I wanted to do was produce some distribution lists from an Access database, but thanks to Office Outlook's totally complicated system of managing email lists, you can't just spit out something like the old .alias file:
listname: joe@blow.com, foo@bar.com, blarg@foo.com
That would be easy. No, Outlook requires you to make a DistributionListItem, and then AddMembers to it. Simple enough, but a member must be a Recipient object. The Recipient can be made from an email address, but I didn't know that, so, instead, I spent a lot of CPU cycles creating ContactItems. Those are those little address card things. So, I made the contacts and wrapped them in a Recipient. It's not quite that basic, though. You have to Resolve the Recipient, and Outlook will match up the name with the associated ContactItem in its vast database. The tricky thing is that the vast database also connects to LDAP server (aka Active Directory) and can look up people in directories. It also looks in the Public Folders. So, building a list involves a lot of work, but, with some effort the lists are made. Unfortunately, Outlook has a limit on the number of recipients in a distribution list. I don't know what the limit is anymore, but I decided to limit it to 99. To build larger lists, you can make a list that refers to sub-lists. So, if I had 250 email addresses, I'd make a list, and in that list, make three more sublists, and split the big list into these three small lists. Simple enough... but it seems like I hit a problem. When I try to turn the name of the sub-list into a Recipient, and then Resolve it... it doesn't resolve correctly. I get an error. An error would be bad enough, but, this is an error that throws up an Alert Box! Argh. Even worse, it seems like it can't be trapped. (I'll have to try different things.)

Conclusions

It's hard to debug apps that control other apps, especially when the other app also calls library that might communicate with a server. It's hard to write simple tools for Outlook because there's too much overhead with all it's big objects. It's hard to deal with exceptions that don't bubble from one app to the other. It's kind of addictive to try and chase down these problems, especially when you uncover better ways to write your code. When you do Google searches to figure this crap out, you realize a few other things: Lots of sample VBA code is garbage. Doing simple stuff in Outlook is a pain in the ass, so, MS Office users suffer a lack of decent automation.

Bitchen PHP

Here's a little bit of demo code about a way to embed templates within PHP templates. It's based on the idea of array_map(), or Perl's map. The requirements are that the data is retrieved by an object that acts like an Iterator. (It could work with arrays, too. Of course, that would mean we could just use array_map. This uses an object, so we can return more than one value per call to next(). Alternatively, you can return nothing - just alter the object's properties. Either/or.) The a() function takes the template and applies it to each element that the iterator returns. This version only allows one variable per template. What's cool about this is that the luserspace code is so short, and it's not too strange looking.
<?php
// haxorspace
function a($template, $object)
{
	while ($data = $object->next())
	{
		$t = $template;
		$t = str_replace('$data', $data, $t);
		echo $t;
	}
}
class O { 
	var $data = array();
	function O() 
	{
		$this->data[0] = 'a';
		$this->data[1] = 'b';
		$this->data[2] = 'c';
	}
	function next() 
	{
		return array_shift($this->data);
	}
}

//-----------------------------------------------------
// luserspace

$object = new O;
$template = '<li>$data</li>';

?>
<ul>
<?php a($template,$object)?>
</ul>
Oh pthhht. It's way better to use array_map(). Complexity sucks.
<?php
// haxorspace
$data[] = 'a';
$data[] = 'b';
$data[] = 'c';

//-----------------------------------------------------
// luserspace
function template($d) { echo "<li>$d</li>"; }
?>
<ul>
<?php array_map('template',$data)?>
</ul>
This latter method is better, because you can make your functions more complex. By doing that, you can remove complexity from the main template. Even better, you can have any kind of data type in the array, and the templating function can contain the hideous logic to switch between them. Use with restraint, these "picture functions" are a good way to modularize the code.