8

我正在尝试设计一个有一些限制的工作簿,而不在 Excel 中使用 VBA,它在 2007 年和 2010 年兼容。我选择了带有 XML 代码的“Microsoft Office 自定义 UI 编辑器”来限制一些选项:- 另存为信息选项卡、插入、删除、移动/复制工作表、隐藏工作表、取消隐藏工作表。我这样做很成功,但我注意到插入表选项卡“ICON”

在此处输入图像描述

仍在工作并且可以访问。任何人都可以通过文件中的 XML 指向控件名称来禁用它吗?

我的代码是:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <commands>
               <command idMso="FileSaveAsWebPage" enabled="false" />
               <command idMso="FileSaveAs" enabled="false" />
               <command idMso="FileSaveAsMenu" enabled="false" />
               <command idMso="FileSaveAsExcelXlsx" enabled="false" />
               <command idMso="FileSaveAsExcelXlsxMacro" enabled="false" />
               <command idMso="FileSaveAsExcel97_2003" enabled="false" />
               <command idMso="FileSaveAsExcelOpenDocumentSpreadsheet" enabled="false" />
               <command idMso="FileSaveAsPdfOrXps" enabled="false" />
               <command idMso="FileSaveAsOtherFormats" enabled="false" />

               <command idMso="SheetInsert" enabled="false" />
               <command idMso="SheetInsertPage" enabled="false" />
               <command idMso="SheetDelete" enabled="false" />
               <command idMso="SheetRename" enabled="false" />
               <command idMso="SheetMoveOrCopy" enabled="false" />
               <command idMso="SheetUnhide" enabled="false" />
               <command idMso="SheetProtect" enabled="false" />
               <command idMso="SheetTabColorGallery" enabled="false" /> 
               <command idMso="SheetTabColorMoreColorsDialog" enabled="false" />
             <command idMso="SelectAllSheets" enabled="false" />
  </commands>
  <backstage>
           <tab idMso="TabInfo" visible="false"/>
  </backstage>
</customUI>

我也尝试在 Microsoft 和rondebruin中搜索Office Fluent 用户界面控制标识符。

4

2 回答 2

5

For this to be done through XML you would need to be able to access the element - it needs to have an ID. Manually scanning through the various lists that Microsoft published turned up nothing helpful, but since their documentation is notoriously sloppy I decided to write a very small piece of code that finds the ID of "every control with an ID" in the Excel application, and lists it:

Sub listID()
Dim r As Range
Dim ctls
Dim ii As Long

Cells(1, 1).Value = "ID"
Cells(1, 2).Value = "caption"
Cells(1, 3) = "Type"
Set r = Range("a1")

For ii = 1 To 100000
  Set ctls = CommandBars.FindControl(Id:=ii)
  If Not (ctls Is Nothing) Then
  'Debug.Print "controls ID " & ii & " exists; the caption is " & ctls.Caption & "; the type is " & ctls.Type
  Set r = r.Offset(1, 0)
  r.Value = ii
  r.Offset(0, 1) = ctls.Caption
  r.Offset(0, 2) = ctls.Type
  r.Offset(0, 3) = ctls.TooltipText
  End If
Next ii

End Sub

After I run this, and filter on anything with eet in the name, I would expect to see all the controls "that can be controlled" (because they have an msoID) and that relate to "Sheets". The following is the snapshot of what this produces:

enter image description here

When I hover my mouse over the "button" you want to hide, I get the toolTip "Insert Sheet" - which isn't any of the ones I can see in my list. I conclude from this that it is indeed impossible to do what you are asking - you cannot disable that button with XML.

That doesn't mean you can't achieve what you would like. I would suggest the following approaches.

  1. Trap the workbook event that is triggered when a new sheet is created, and delete it on the spot. When the button "stops working" people will soon give up. Example code below.
  2. Hide the sheet tabs completely, and provide an alternative method of navigating between sheets. Since this is obviously a "controlled spreadsheet" that may be a good idea anyway. You could either create a custom tab on the ribbon (using XML, you seem to be familiar with that), or create a floating toolbar that lives at the bottom of the sheet - close to where the "old" tabs used to be. In that way you simulate the behavior - but it's a lot of work and a bit of a hack
  3. Add protection to the workbook. Use Protection -> Protect Workbook -> "Protect structure": sheets can not be moved, deleted, hidden, unhidden, or renamed. New sheets cannot be inserted.

The code you would have to add to ThisWorkbook is:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
  Dim temp As Boolean
  temp = Application.DisplayAlerts
  Application.DisplayAlerts = False
  Sh.Delete
  Application.DisplayAlerts = temp
End Sub

Once this is in your workbook, any time a user clicks the "new sheet" button there will be a very brief flash, but no new sheet is created. You could add a Application.ScreenUpdating = False but the brief flash remains...

Sorry I don't have better news for you.

于 2013-07-27T15:53:53.430 回答
1

您可以选择 Review 选项卡,选择 Protect Workbook,然后检查 Structure。您无需指定密码,“插入工作表”按钮将被禁用(但仍可见)。

或者您可以从 Office 按钮、高级选项卡转到 Excel 选项,向下滚动到显示此工作簿的选项并取消选中显示工作表选项卡

我不相信这可以通过 XML 完成。也就是说,禁用或隐藏此按钮(不隐藏所有工作表选项卡)。也许有人可能会证明我错了;)

于 2013-07-27T14:27:38.120 回答