Tuesday, October 27, 2009

Microsoft Word and default filenames

Have you noticed how, when you save a new document in a Microsoft Word, the save dialog comes up with a default filename suggestion? Ever wondered how it comes up with that suggestion, impractical as it may sometimes be?


No? Neither had I, but unfortunately we are developing an office add-in that actually replaces the file dialogs with our own, and to facilitate user acceptance, minor functional details like that have to be mimicked in detail. Users might not notice the behaviour when it's there, but they sure do notice it when its missing or, god forbid, ever so slightly different!

The filename generation is a three-step algorithm:

Step 1) First, and not many users are aware of this, Word looks at the document title. You can set this title by selecting Properties from the File menu, but it will show up in the document properties in Windows Explorer as well.

Programmatically, you can access the document title through the BuiltInDocumentProperties property of the Document object, by requesting the wdPropertyTitle property.

Step 2) If the document title is empty, Word looks at the very first sentence of your document. It will skip anything that is not a "valid" character (i.e. an alphanumeric or a space), and then simply return all characters until the next invalid character. Usually this comes down to simply returning the sentence up to the first punctuation mark. This very same sanitization algorithm is actually applied to the document title from step 1 as well.

Step 3) If both the title is empty and your document doesn't have any valid characters in it (i.e. it is mostly empty), things get a bit hairy. This is when Word falls back onto the doc1, doc2 etc naming convention. I am still not 100% sure how this convention works, but here is how we handle it: simply take the first three characters and the suffix number from the document Name property (which will be something like Document1), and lowercase the first character. This approach works properly with localization in the three languages (English, German and Dutch) we actively test here in our development lab, but I cannot give any guarantees yet on the 34 other languages supported by Microsoft Office.

As a funny sidenote, across Office applications things behave in slightly different ways. Excel simply suggests the name of the current Workbook (Book1, Book2, etc). PowerPoint uses a similar algorithm to Word, but only looks at the text in the presention, not at the presentation title. It does however automatically populate the title property with that first sentence, something Word doesn't do.

Ah well, it's those minute differences that keep office developers on their toes I guess. To the typical end user the office applications are similar enough to allow them to switch from one to the next without too much pain, and that's all that really counts in the end.

No comments:

Post a Comment