Absolute positioning in HTML e-mails for Outlook 2007

I couldn’t possibly explain my frustration from Microsoft switching Outlook’s rendering engine to Word so I better don’t even try. Instead, I’m going to tell you how to make Outlook/Word position elements absolutely.

The idea originally came from doublethink (written in Czech) who made a text field in Word, saved it as HTML and then dig through the mess to extract the essence. I just went a step further and did the same for images as well.

Start by adding this to tag HTML:

  1. <html xmlns:v="urn:schemas-microsoft-com:vml">

Images

We’re lucky Word knows how to insert pictures and “wrap” the text behind/above the picture.

  1. <!–[if gte vml 1]>
  2. <v:shape id="headerShapeImg" type="#_x0000_t75"
  3.  style="position: absolute; height: 106px; width: 769px; z-index: -1;">
  4. <v:imagedata src="/pix/header.png" alt="this is my header"/>
  5. </v:shape>
  6. <![endif]–>
  7.  
  8. <![if !vml]>
  9. <img v:shapes="headerShapeImg" src="/pix/header.png" id="headerImg" alt="this is my header">
  10. <![endif]>

Place the whole thing directly into BODY, both parts where the image is supposed to be. Please note that the IMG tag will be used only for engines other than Word and MSIE.

ID “headerImg” is there to provide reference for further formatting - although don’t expect it to work in Word/Outlook.The only universal method is to use inline CSS via STYLE. Also like it or not, you’ll have to duplicate your formatting definition - define it both for V:SHAPE and for IMG (or DIV, see below).

Others

Other stuff must be wrapped in a div like this:

  1. <!–[if gte vml 1]>
  2. <v:shape id="tableShape" type="#_x0000_t202" filled="f" stroked="f"
  3. style="position: absolute; margin-left: 280px; margin-top: 76px; z-index: 1; width: 470px;"></v:shape>
  4. <![endif]–>
  5.  
  6. <div v:shape="tableShape"><table><tr><td>…</td></tr></table></div>

I’m sure you have already guessed the meaning of attributes filled (background color) and stroked (frame border). Remember, this is Word so we’re talking about a “text frame”.

That’s also a reason why you have to use margin-left and margin-top instead of top and left, or even margin: top right bottom left. And don’t forget to add width to every absolutely positioned element - in numbers, “auto” doesn’t count.

Duplicate formatting

Sweet, huh? Here’s what I used to make my life easier. We’re PHP programmers, remember?

  1. <?php
  2. $tableStyle = <<<HEREDOC
  3. position: absolute;
  4. margin-left: 280px;
  5. margin-top: 76px;
  6. z-index: 1;
  7. width: 470px;
  8. HEREDOC;
  9. ?>
  10. <v:shape id="tableShape"
  11.  style="<?php echo $tableStyle; ?>"></v:shape>
  12. <div v:shape="tableShape"
  13.  style="<?php echo $tableStyle; ?>"><table><tr><td>…

The output will still contain duplicated formatting but at least, you have a single place for changes.

How to debug

The first method that comes to your mind is probably to run your mailing script and then wait for the e-mail to arrive to your Outlook. Since we’re in fact debugging for Word I preferred to open it directly in Word. I wrote my script to display the HTML “e-mail” as a web page instead of sending it and then I opened the url in Word via CTRL+O. You are allowed to paste a url and Word downloads it.

For command line lovers (like me):

  1. "C:\Program Files\Microsoft Office\Office12\WINWORD.EXE" http://server.com/path/page.php

You’ll have to be careful about local cache so append fictitious parameters in order to force reload:

  1. http://server.com/path/page.php&amp;whatever=changing_value

During early stages debug locally with dummy data, of course. No need for the HTML page to be generated by a script using real data. Note though, that when you open a file in Word you cannot write to it using another editor. And when you save from Word you get back all the marvellous garble that you have worked so hard to erase. Enjoy!

Some formatting must be applied to a child element

Did I mention that text-align: right works only when it’s NOT in the DIV we position but in its child? The same is true for font-family, font-weight and font-size:

  1. <div v:shape="myShape">
  2. <div style="text-align: right; font-family: sans-serif;">
  3. hello world
  4. </div>
  5. </div>

will work while if you join the two DIVs together, it will not.

One Response to “Absolute positioning in HTML e-mails for Outlook 2007”

  1. Adrenalin Says:

    Oh my inexistant God! You really saved my day.

    You know, after banging your head against Outlook 2007 for several hours, trying to Google my way to find out about the lack of backgrounds and then finally ending up in here, tired and frustrated, ready to resign… ;)

    I thank you from the bottom of my heart for these advices.

Leave a Reply