Microsoft Excel ActiveX Controls Disabled?

后端 未结 11 2643
死守一世寂寞
死守一世寂寞 2020-11-22 06:43

I have some Excel worksheets that use ActiveX checkboxes to control certain activity. They worked recently but today started to give errors. I was alerted to this by a col

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 07:30

    I'm an Excel developer, and I definitely felt the pain when this happened. Fortunately, I was able to find a workaround by renaming the MSForms.exd files in VBA even when Excel is running, which also can fix the issue. Excel developers who need to distribute their spreadsheets can add the following VBA code to their spreadsheets to make them immune to the MS update.

    Place this code in any module.

    Public Sub RenameMSFormsFiles() 
      Const tempFileName As String = "MSForms - Copy.exd"  
      Const msFormsFileName As String = "MSForms.exd"  
      On Error Resume Next 
    
      'Try to rename the C:\Users\[user.name]\AppData\Local\Temp\Excel8.0\MSForms.exd file  
      RenameFile Environ("TEMP") & "\Excel8.0\" & msFormsFileName, Environ("TEMP") & "\Excel8.0\" & tempFileName 
      'Try to rename the C:\Users\[user.name]\AppData\Local\Temp\VBE\MSForms.exd file  
      RenameFile Environ("TEMP") & "\VBE\" & msFormsFileName, Environ("TEMP") & "\VBE\" & tempFileName 
    End Sub  
    
    Private Sub RenameFile(fromFilePath As String, toFilePath As String) 
      If CheckFileExist(fromFilePath) Then 
          DeleteFile toFilePath  
          Name fromFilePath As toFilePath  
      End If  
    End Sub
    
    Private Function CheckFileExist(path As String) As Boolean 
      CheckFileExist = (Dir(path) <> "")  
    End Function  
    
    Private Sub DeleteFile(path As String) 
      If CheckFileExist(path) Then 
          SetAttr path, vbNormal  
          Kill path  
      End If  
    End Sub    
    

    The RenameMSFormsFiles subroutine tries to rename the MSForms.exd files in the C:\Users\[user.name]\AppData\Local\Temp\Excel8.0\ and C:\Users\[user.name]\AppData\Local\Temp\VBE\ folders to MSForms - Copy.exd.

    Then call the RenameMSFormsFiles subroutine at the very beginning of the Workbook_Open event.

    Private Sub Workbook_Open() 
      RenameMSFormsFiles  
    End Sub
    

    The spreadsheet will try to rename the MSForms.exd files when it opens. Obviously, this is not a perfect fix:

    1. The affected user will still experience the ActiveX control errors when running the VBA code the very first time opening the spreadsheet. Only after executing the VBA code once and restarting Excel, the issue is fixed. Normally when a user encounters a broken spreadsheet, the knee-jerk reaction is to close Excel and try to open the spreadsheet again. :)
    2. The MSForms.exd files are renamed every time the spreadsheet opens, even when there's no issue with the MSForms.exd files. But the spreadsheet will work just fine.

    At least for now, Excel developers can continue to distribute their work with this workaround until Microsoft releases a fix.

    I've posted this solution here.

提交回复
热议问题