Last 10:

Archives:

WP Links:

home button news button clients button cv button

February 5, 2008

Internet Explorer Breaks Excel Pivot Tables

Filed under: Computers, Coding — admin @ 3:52 pm

 

Yep, it’s true.  It appears that one hand of Microsoft doesn’t know what the other is doing.  I haven’t found a lot of hits on this but apparently both sides of this bug confirm its existence and both claim it’s not their problem.

Symptom:

  • Microsoft Excel cannot find the references inside a pivot table that references the same workbook when opened from Internet Explorer

Cause:

  • Internet Explorer adds ‘[X]’ to the end of the file name when storing the file in the IE temp folder, where X represents an incrementing integer based on the number of downloads of the same file, and the square brackets are illegal characters for file names in Windows

Steps to reproduce:

  • Browsing with Internet Explorer
  • Clicking on a link to an Excel spreadsheet with a pivot table in it that references itself
  • Clicking on "Open" rather than "Save"

The only solution I could think of was to write a VBA macro to rename the file and reattach all of the pivot tables.  I hope this saves someone else some trouble in the future.  When you think of me, think of me fondly…

( You’ll have to add a reference to Microsoft VBScript Regular Expressions 5.5 or better. )

 

 Sub Auto_Open()
    
    Dim sFilename As String   ‘ the corrected filename
    Dim oASheet As Worksheet  ‘ the worksheet object
    Dim oPT As PivotTable     ‘ the pivot table object
    Dim sTempSource As String ‘ the pivot table temporary source variable
    Dim sNewRange As String   ‘ the new pivot table source variable
    Dim reg As New RegExp     ‘ a regular expression for matching wildcards
    Dim i As Integer          ‘ looping integer
    Dim j As Integer          ‘ looping integer
    
    ‘ Set a regex to test whether this is a temp spreadsheet from IE.
    ‘ Only a temp spreadsheet from IE will have square brackets in the filename.
    reg.Pattern = "[[\]]"
    
    If reg.Test(ActiveWorkbook.FullName) Then
        ‘ the temp filename has square brackets in it
        
        ‘ just a little message telling the user what’s going on
        MsgBox "Renaming temp file and reattaching pivot tables…"
        
        ‘ The .Name property already has the brackets stripped out of it so
        ‘ we can build the new path using it and the .Path property.
        sFilename = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
        
        ‘ Now we save the file so that the assignment of source data to the
        ‘ pivot tables won’t fail.
        ActiveWorkbook.SaveAs Filename:=sFilename
            
        ‘ The range for the pivot table requires a square bracket
        ‘ just before the file name so we rebuild the path, less the drive letter.
        sNewRange = Replace(ActiveWorkbook.Path, "C:", "") & "\[" & ActiveWorkbook.Name & "]"
        
        ‘ now set the regex for the tail end of the spreadsheet, including the trailing "]"
        reg.IgnoreCase = True
        reg.Pattern = ".*\.xls\]"
        
        ‘ now loop through all of the sheets looking for pivot tables
        For j = 1 To ActiveWorkbook.Worksheets.Count
            Set oASheet = ActiveWorkbook.Worksheets(j)
        
            ‘ now loop through any pivot tables on the worksheet
            For i = 1 To oASheet.PivotTables.Count
                Set oPT = oASheet.PivotTables(i)
            
                sTempSource = oPT.SourceData
                
                ‘ go ahead and replace the invalid path with the new path
                sTempSource = "’" & reg.Replace(sTempSource, sNewRange)
            
                ‘oPT.PivotTableWizard SourceType:=xlDatabase, SourceData:=sTempSource
                
                ‘ assign the new path to the sourcedata property
                oPT.SourceData = sTempSource
                
                ‘ release the object
                Set oPT = Nothing
            Next i
            
            ‘ release the object
            Set oASheet = Nothing
        Next j
        
        ‘ Save the workbook, just to be thorough.
        ‘ Without saving a user would be able to close the workbook without saving
        ‘ and then open the workbook from the
        ‘ recent documents list and have broken pivot tables.
        ActiveWorkbook.Save
    Else
        ‘ Not a spreadsheet with square brackets in the filename
        ‘MsgBox "No renaming required"
    End If

End Sub

5 Comments »

  1. Is this VBA script run from the Excel file or do we put it in our web code?

    Comment by Joel — February 18, 2008 @ 12:13 pm

  2. Yes, this is a VBA script. Putting this code in the document’s ThisWorkbook will force it to automatically run when the spreadsheet is opened.

    Comment by admin — February 19, 2008 @ 12:09 pm

  3. Hey, thanks for this script!

    Comment by Mike Akers — August 4, 2008 @ 5:19 pm

  4. Thanks for the script. It works nicely! (Had to replace backticks with quotes)

    Comment by Greg Sheremeta — October 27, 2008 @ 3:36 pm

  5. Yeah, Wordpress changes the single quotes into backticks to protect MySQL from SQL injection attacks. Glad it worked for you!
    Jacob

    Comment by admin — October 27, 2008 @ 5:21 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress

Quinte Web Design Kennedy Data Solutions Jacob Kennedy MS Access Microsoft Access ASP Web Design Website Web Site Database Parry Sound Trenton Ontario Canada Quinte West Prince Edward County Belleville Deseronto