How to fix a corrupted Microsoft Office File

My wife creates massive proposals using Microsoft Word and Microsoft Office. Today she had a Word document spontaneously get corrupted and lose hours of her changes. The Microsoft Word document would not open at all, not on her computer, or any of her co-worker's computers.

I offered to take a look at the document and see if I could recover any part. She sent me the document and I started googling around for 'how to recover docx files and found a number of paid utilities claiming to fix the situation. Not ready to spend money, and on a whim, I tried to open the document with the Open Office, open source word processing software. Guess what, it worked!

The corrupted document opened just fine with Open Office. I easily saved the document as a .doc file which opened just fine on my wife's computer in Microsoft Office.

Since the file is now a .doc format and not a .docx format, some of the formatting was munged. However, fixing formatting is a whole lot easier than re-crafting pages and pages of text, don't you agree?

The best part about this, is it took less than a minute and $0 to repair the file.

Here are the steps:

[More]

So a CFML variable name cannot end with a "." character

I spent a good hour today trying to get some old code working and got stuck trying to fix:

a CFML variable name cannot end with a "." character

Can you spot the issue in the following code block?

view plain print about
1<cffunction name="doLog" output="false" access="public" returntype="any">
2    <cfset var emailData = {} />
3    <cfset emailData.EmailAddressTo = variables.instance.config.getScheduledTaskEmailAddress() />
4    <cfset emailData.EmailSubject = "From the Nag Email Scheduled Task" />
5    <cfset emailData. EmailContent = "#arrayToList( variables._base._log, '#chr(13)##chr(10)#' )#" />
6    <cfset variables.instance.emailService.sendEmail( argumentcollection:emailData ) />        
7    <cfset super.doLog() />
8</cffunction>

[More]

How to customize the ColdFusion AutoSuggest

ColdFusion 8 and 9 have an autosuggest functionality that just couldn't be simpler. If you want to have an autosuggest input on the screen, you can do it with a single line of code:

view plain print about
1<cfinput type="text" name="language" autosuggest="english,spanish,french">

That snippet will place an autosuggest box on the screen and allow a choice of english, spanish or french. Snazzy yeah?

If you want, you can bind the autosuggest to a javascript function, or directly to a CFC, making data retrieval and formatting very simple.

view plain print about
1<cfinput type="text" name="language" autosuggest="javascript:doSomethingJavascripty( {cfautosuggestvalue} )" />

view plain print about
1<cfinput type="text" name="language" autosuggest="cfc:DoSomething.coldFusiony( {cfautosuggestvalue} )" />

You can also make more complex examples, take a look at the ColdFusion 9 Documentation for Autosuggest for some ideas. All this is possible because ColdFusion used the extensive YUI library under the hood. The control used by the ColdFusion Autosuggest is the YUI AutoComplete widget.

Let's say you wanted to do something that the YUI AutoComplete offers, but isn't in the ColdFusion documentation, what do you do?

[More]

So You Want Cleaner Code? Nesting Conditionals and Booleans

This post continues the infrequent series on Clean Code examples. If you are enjoying this series, come to CFUnited 2010 and CF.Objective() 2010 to catch my presentation, Making Bad Code Good- Part 2, a live version of this series.

The main idea here is to provide practical examples of working code that can be written in a cleaner fashion. You may agree or disagree with what I've written, and I want to hear from you either way in the comments.

If you have a code sample you'd like to see refactored, send it to me through email (if you have my email already) or through the Contact Me form on this blog.

Code Sample

view plain print about
1<cffunction name="execute" access="public" output="false" returntype="boolean">
2    <cfargument name="attributes" required="true"/>
3    <cfset var local = structNew() />
4    <cfset local.customerGateway = createObject("component","model.customerGateway").init() />
5    <cfif structKeyExists( client, "customerID" ) AND val( client.customerID ) GT 0>
6        <cfif val( arguments.customerGW.getByID( val(client.customerID ) ).getChallengeQuestionID() ) GT 0>
7            <cfset local.result = "true" />
8        </cfif>
9    <cfelseif structKeyExists( arguments.attributes, "ChallengeAnswer") AND
10                structKeyExists( arguments.attributes, "ChallengeQuestionID") >

11        <cfif structKeyExists( arguments.attributes, "ChallengeQuestionID") AND val( attributes.ChallengeQuestionID ) GT 0>
12            <cfif structKeyExists( arguments.attributes, "ChallengeAnswer") AND len( trim( attributes.ChallengeAnswer ) ) GT 0>
13                <cfset local.cust =    arguments.customerGW.getById( client.customerID ) /> />

14                <cfset local.cust.setChallengeQuestionID( attributes.ChallengeQuestionID ) />
15                <cfset local.cust.setChallengeAnswer( attributes.ChallengeAnswer ) />
16                <cfset arguments.customerGW.save( cust ) />
17                <cfset local.result = "true" />
18            <cfelse>
19                <cfset local.result = "false" />
20                <cfset attributes.userMessage= "Please select a security question and provide an answer." />
21            </cfif>
22        </cfif>
23    <cfelse>    
24        <cfset local.result = "false" />
25    </cfif>        
26    <cfreturn local.result />
27</cffunction>

Read through the entire example and ask yourself the following questions:

  • What is the purpose of this code block? What are you using for your inferences?
  • Is it clear what this code is doing? Why or why not?
  • Can you spot the subtle bug in the code?

[More]

A Side Effect of ySlow and Firebug

A Side Effect of ySlow and Firebug

I spent a number of hours researching a bug in a Model-Glue application. Each time a form was submitted successfully, a duplicate record showed up in the database. This application uses a number of frameworks and it wasn't very clear which layer was causing the problem.

I started to dig into the issue, looking for any rhyme or reason and pinged my good buddy Ezra Parker for some sanity checks. After some intense debugging, we found out that the second record in the database showed up after the ColdFusion request ended. I tried all sorts of programmer sorcery to find out why this second request happened and did not get much useful information. This duplicate request problem defied all logic!

Through the course of working through the information and issues, we explored many potential causes and questions like:

  • Was Model-Glue possibly adding a second redirect somewhere?
  • Was there a CFThread buried in Model-Glue, ColdSpring, Transfer or CFUniform that caused this?
  • Was there some javascript call being fired off, and replicating the request?
  • Were space hackers infiltrating my computer?
  • Am I on Candid Camera?
  • Should I just quit programming and open a Bakery?

[More]

So You Want Cleaner Code? Return Statement Execution

I'm going to start an infrequent series on Clean Code examples. If you are enjoying this series, come to CFUnited 2010 and CF.Objective() 2010 to catch my presentation, Making Bad Code Good- Part 2, a live version of this series.

The main idea here is to provide practical examples of working code that can be written in a cleaner fashion. You may agree or disagree with what I've written, and I want to hear from you either way in the comments.

If you have a code sample you'd like to see refactored, send it to me through email (if you have my email already) or through the Contact Me form on this blog.

Code Sample

view plain print about
1<cfif FindKey.Recordcount gt 0>
2    <cfreturn true />
3<cfelse>
4    <cfreturn false />
5</cfif>

[More]

ColdFusion License Key Invalid?

I'm having all my servers upgraded to ColdFusion 9. In the process we ran into a situation where a perfectly valid key was not accepted by the ColdFusion installer.

The serial number that you entered is invalid

After a little troubleshooting, Tim Geist at Viviotech (top notch hosts) found an Adobe KB on Invalid License Keys and Linux:

When installing ColdFusion 9 Standard onto a Linux operating system, you may encounter the issue where the installer does not accept the serial number. When you enter the number into the installer, you will receive a message similar to the following:

* The message 'The serial number that you entered is invalid' * A red cross will appear next to the Serial Number text field

The installer will not let you progress any further.

Read More

Apparently, proceeding with the developer installation, then entering the license key in later fixes the issue. I'm sure this will be remedied in a future point release of ColdFusion.

So you wanna convert Open Office Documents to Wiki format?

While working on some Model-Glue documentation we needed to convert an open office document to TracWiki format. I'm not fluent in TracWiki and I certainly didn't want to manually format 30-40 pages. Thankfully, there is an Open Office --> TracWiki conversion macro that will automate pretty much all of it. It helps if you used the Styles features of Open Office because the TracWiki will be cleaner, but no worries, it'll convert it anyways if you didn't. Here is how it works.

  1. Open the Open Office document you want to copy text from. We'll call it the Source document.
  2. Set the macro permissions to Medium or Low by going to the menu -> Tools -> Options -> Open Office.org -> Security -> Macro Security
  3. Download and open the Open Office Template Macro (ODT file)
  4. Remove all text in the newly opened ODT file.
  5. Highlight the text in your Source document, copy it, then paste it into the ODT file.
  6. Press the button with the correct format, the text will be converted to the correct wiki format and copied to the clipboard. (English is on page 2)
  7. Paste the text from the clipboard into the edit box in your wiki.

You now have converted Open Office document text to wiki format.

The conversion picked nearly everything. I manually cleaned up white space issues and also added in code blocks around all the source code sections, but I was very pleased with the amount of work handled by the OpenOfficeToTracWikiScript.

How to Make a Show Password Link with JQuery

While we all agree a good security measure is hiding passwords in form inputs, giving a user the ability to unhide the password is a nice usability feature. How often are you changing your password with nefarious people standing over your shoulder, right?

Really, the difference between markup for a text field and password field is just the type attribute of either 'text' or 'password'. JQuery makes it easy to work with attributes, by the $().attr() method. So, this would seemingly be a very simple task, right? Let's try it:

view plain print about
1<script type="text/javascript>
2 oldBox.attr('
type', 'text');
3</script>
Whoa, this promptly results in the following error:

uncaught exception: type property can't be changed

Ouch. That didn't work like we expected, did it? Let's look at why. Once an element is inside the DOM, the type of the element can't be changed. The differences between the various DOM controls just mean we can't change a type of an element once it has been added. We can, however, clone the element, change the type then attach it to the DOM and it will work perfectly.

[More]

So you wanna learn Regex? - Part 6

Welcome to So You Wanna Learn Regex? Part 6. OK, I know I said part 5 would be the last part in the series, but I just had to work this one out and wanted to share. Remember, If you want more tutorials about regex, especially more advanced ones than the mickey mouse onces here, go bug Ben. He knows more about this than I ever will and I hear he has a blog...

In our last exercise, we looked at cleaning up some data scripts.

In this exercise, we are going to reformat a configuration file from .ini style to ColdSpring MapFactory style. Specifically, I'm integrating CFFormProtect into an application and I want the config to be managed in ColdSpring with the rest of my configurations. Sure, I could go flapping around with copy+paste, smashing keys, burning tendons, but that seems so Junior Programmerish, doesn't it?

Assume this set of declarations:

mouseMovement=1
usedKeyboard=1
timedFormSubmission=1
hiddenFormField=1
akismet=0
tooManyUrls=1
teststrings=1
projectHoneyPot=0
timedFormMinSeconds=5
timedFormMaxSeconds=3600
encryptionKey=JacobMuns0n
akismetAPIKey=
akismetBlogURL=
akismetFormNameField=
akismetFormEmailField=
akismetFormURLField=
akismetFormBodyField=
tooManyUrlsMaxUrls=6

What we want, is to turn:mouseMovement=1 into: <entry key="mouseMovement"><value>1</value></entry>

Note we've split a string delimted by an equals sign into some XML nodes.

So as you know, we define this pattern in the gobbledegook of regular expressions. When read one chunk at a time, these actually make sense. We'll go through the exercise, then look at why it worked.

In Eclipse, perform the following:

[More]