Technical discussion ahead, so watch yoself...
Microsoft next year is unveiling a shiny new version of their Office suite of programs. Among other things, they're changing the user interface drastically. Below is how the current Word 2003 looks:

And below is how Word 2007 will look when it comes out:

Pretty big change. I think it looks pretty cool, but i'm always into new things. They're calling it the Ribbon.
So at work i've downloaded and installed the beta version of Office 2007 and have been using it pretty much every day. The program i use the most at work is Excel, especially when i'm working on Navy stuff. I pretty much live and breath Excel. The crazy thing is that i'm discovering new features and ways of doing things all the time. There's so much you can do that it's hard to know what all is there, and that's one of the reasons they're going to this new user interface. Apparently a large percentage of feature requests that Microsoft gets for Office are for features that are already there. People just haven't been able to find them easily.
I like the new user interface combined with the multiple tweaks and additions they've made this time around. It's a solid release, and i'm hoping we upgrade to it at work. It probably won't happen, but i can dream.
So i had some downtime at work and decided to play around with customizing the UI in Excel. If you notice in the Word 2007 picture, there are several tabs called Write, Insert, Page Layout, etc. Excel has a similar setup. I thought it'd be neat to add my own custom tab along with some buttons. I took a spreadsheet that i use to run one of our models and saved it in the 2007 file format. The reason i chose this sheet was because it has four macros that let me generate input for the model, load and save scenarios, and open output spreadsheets. It's really a control panel for the model. That's all done with Visual Basic code for the macros.
The first thing i had to learn was how to actually add the new tab and buttons. After it's saved to the 2007 format, if you add ".zip" to the end of the file name you can use WinZip or any other file compression program to open the file and you can see all it's various pieces. It's really just a group of compressed text files that Office knows how to format properly. Office 2007 uses XML to structure its files, and i have to use XML to create the new tab. It's actually pretty straightforward, and i got the new stuff to appear without much trouble. You create a custom XML file, add it to the file in WinZip, then take away the ".zip" extension and open the file in Excel. It was when i started trying to use the new buttons to run the macros that i started having issues.
For those that don't know, when you create a macro in Excel, it creates a set of Visual Basic code that does what you want. You can either record a macro by telling Office to record your actions in the program until you tell it stop, or you can manually write the code yourself. Personally, i usually start by writing the code myself. If i'm not sure how to do something, i'll record a macro, look at the generated code, then adapt it to my own needs. A macro in Visual Basic looks something like this:
Public Sub MyMacro() Code that does something End Sub
The blue words are special words that Visual Basic reserves for itself, and it highlights them in blue so you know not to try to use them for your own purposes. If you want to run one of these macros using a custom button, you have to change the code to look like this:
Public Sub MyMacro(ByVal control As IRibbonControl) Code that does something End Sub
What that new stuff does is send a copy of the button (with the name of "control") to the macro so it knows who's calling it. To be honest, i don't know why that's necessary, but it doesn't work without it so i'm at the mercy of Microsoft here. So i start running the macros, and everything seems to be going ok. I need to start working on some other things, so i close the worksheet. Or at least, i try to. As i close it I get a scary message saying "Argument is not optional." along with the Visual Basic editor appearing and helpfully pointing out where the problem is located. Ah, ok, i see it. I have a bit of code that runs when you close the spreadsheet. It checks if the currently loaded scenario has been changed, and asks if you want to save it if it has. If you do want to save it, it runs one of the macros called SaveScenario. The problem is that when i do that i don't send a copy of the button to the macro. Since the macro is expecting that button, it chokes when i try to run the macro without it.
Ok, well, let's try this:
Public Sub MyMacro(Optional ByVal control As IRibbonControl) Code that does something End Sub
By adding that "Optional" key word in, i can run the macro without sending the button to it if i need to. Ok, cool, close the worksheet, everything works fine.
Later i come back to the sheet. When i open it, i get another scary message that says something like "Excel has detected that part of this file is damaged or unreadable. Would you like to try to repair it?" Well, i guess i don't have another choice, so i click "Yes." Unfortunately, Excel can't open the file. It's corrupted. I try adding the ".zip" extension to it and opening it in WinZip again. WinZip gives me the finger and tells me the file isn't in the proper format. So i guess i'm screwed. I delete the file, start over from scratch from the original (that i have NOT deleted, because that would be foolish), and try to see what went wrong.
After playing around with some of the macros, i discover that it has something to do with when i save a model scenario or when i actually run the model. What are the common things i do in both of them? In both macros, i generate text files. To do that, i save the spreadsheet as a text file, then re-save it as a spreadsheet file. It never seemed to be a problem before, but that's never stopped anything before. I decide to create a blank spreadsheet, start recording a macro, save as a text file then save as a spreadsheet, and look at the code.
This is the bit of code that Excel spits out:
ActiveWorkbook.SaveAs FileName:="Book1.txt" FileFormat:=xlTextPrinter, CreateBackup:=False ActiveWorkbook.SaveAs FileName:="Book1.xlsm" FileFormat:=xlOpenXMLFormatMacroEnabled, CreateBackup:=False
And this is what i originally had:
ActiveWorkbook.SaveAs FileName:="Book1.txt" FileFormat:=xlTextPrinter, CreateBackup:=False ActiveWorkbook.SaveAs FileName:="Book1.xlsm" FileFormat:=xlNormal, CreateBackup:=False
First, i need to point out that with Office 2007, the new file formats have new file extensions. So a Word 2003 document has a ".doc" extension, while Word 2007's have a ".docx" extension. Similarly, Excel 2007 changes from ".xls" to ".xlsx." In addition to that, if a spreadsheet has a macro, you have to save it with a ".xlsm" extension. This gives an easy way to look at a file name and know right away if there is code in it or not.
In both cases of the code above, it takes the currently open file (ActiveWorkbook) and saves it as a text file in the first line, then saves it again back to the spreadsheet file format in the second line. You can see right away that in the second line, the FileFormat is different. Apparently Excel 2007 expects something different from the 2003 version. xlNormal no longer works, it has to be xlOpenXMLFormatMacroEnabled. After thinking about it for a while, this makes sense to me. Since you have to specifically tell Excel that you're saving it as a file with macros, to do that in code will require a format name that Excel recognizes. I wish it was smart enough to look at the ".xlsm" in the file name and figure this out on its own, but oh well.
Everything seems to be working after this, so i leave for the day.
The next day i come in, open the spreadsheet, and lo and behold, my custom tab and buttons are missing. I open up the file in WinZip and my custom XML file is gone. I start suspecting that the same code from the day before is the culprit yet again, so i add the custom XML file back in and run the macro again. After it's done i close the spreadsheet and open it again. Sure enough, the tab and buttons are missing again. Apparently when you save as a text file then re-save as a spreadsheet, it strips out any custom stuff you add to the file. This seems pretty strange to me because the Visual Basic code doesn't get stripped out when you do this. I suspect that this is actually a bug in Excel 2007. Remember, this is beta software. It's unfinished, not released to the general public, and the whole point of running beta software is to find bugs and submit them to the developer. It's a testament to the quality of the software that i haven't found very many so far and i'm actually able to run it for day-to-day use.
So first i tried to find a way to spit out the text files we need some other way. I was unsuccessful. Apparently the only way to do so is how we were already doing it. So i started looking at how to manually write out the values through pure Visual Basic code instead of using recorded code. I tried this out and it looks like this may work. It looks like this may actually be faster than how we were doing it before, as far as how long it takes the text files to be generated. There are still some issues i need to work out, but i left early on Friday so i didn't have time to play with it any longer.
I originally started this whole process to learn some new things about Office 2007. In the process i learned a lot more about file formats and the perils of upgrading software to the latest version. This is why companies don't generally jump to the newest versions of operating systems, office suites, web browsers, etc, without testing them thoroughly. Sometimes you get lucky and there are minimal problems with upgrading. Sometimes, though, things break, and it's up to the company to figure out how to get things working before rolling out new software throughout the company. It's been fun so far, and i'm looking forward to getting things working properly. |