Lets say you have a neat little Excel 2003 macro, when you run your macro in Excel 2007, Excel runs it in Compatibility Mode, and any benefits (such as the 16384 columns) you were hoping to use are still unavailable. So how can we enable these benefits depending on the Excel version? Simply by including the following code in the Auto_Open subroutine.
Public Sub auto_open() 'add some smarts if opened in Excel 2007 or later Dim oldFileName As String oldFileName = ThisWorkbook.Name Dim tempi As Integer 'used to store position of '.' before file extension in workbook file name tempi = InStrRev(oldFileName, ".xls", -1, vbTextCompare) If Application.Version > 11 And Len(oldFileName) - tempi = 3 Then 'assume running in compatability mode Application.DisplayAlerts = False Dim newFileName As String newFileName = Mid$(oldFileName, 1, tempi) & "xlsm" If fileExist(newFileName) Then 'if the new workbook version already exists, then open it and close this one 'open the new workbook by emulating double clicking the file, as this is the only way to run the auto_open Shell "Excel """ & ThisWorkbook.Path & "\" & newFileName & """", 3 '3 = vbMaximizedFocus 'close this Excel application Application.Quit Else 'if the new workbook version doesn't exist, then save it as new workbook version 'save as macro enables office 2007 workbook ActiveWorkbook.SaveAs Filename:=newFileName, FileFormat:=52, CreateBackup:=False '52 = xlOpenXMLWorkbookMacroEnabled 'create a timer to call the same new workbook as it will be now opened in non compatibility mode Application.OnTime Now + TimeValue("00:00:01"), "auto_open" Workbooks(newFileName).Close End If Application.DisplayAlerts = True End If UserForm1.Show End Sub
Some brief info on how this works, so far the only way I have found to run in Non-Compatibility Mode is to save the Workbook as an Excel Macro Enabled Workbook and reopen the file, or if the file already exists then open that file and close the Excel 2003 Workbook. The main problem is the showing of a UserForm on the reopen or existing open. The bove code is the only way I have found so far, and it involves some trickery. It can be simplified if portions are separated into the Workbook_Activate subroutine, but I wanted to provide a copy and paste solution with minimal fiddling around. The only change in the above code is the UserForm1 name.
One of the problems was the Workbook_Activate or Auto_open not running when called via the normal Open method. I actually had to ask ExpertsExchange, the expert was very helpful.
Any comments or suggestions welcome as always.
Leave a Reply