添加VBA代码以添加自定义功能区后,我发现需要时间才能显示在工具栏中。用于创建功能区的 VBA 代码在我们的工作簿打开事件中调用。
Sub LoadCustRibbon(sDummy)
Dim hFile As Long
Dim path As String, fileName As String, ribbonXML As String, user As String
hFile = FreeFile
user = Environ("Username")
path = "C:Users" & user & "AppDataLocalMicrosoftOffice"
fileName = "Excel.officeUI"
ribbonXML = "<mso:customUI xmlns:mso='http://schemas.microsoft.com/office/2009/07/customui'>" & vbNewLine
ribbonXML = ribbonXML + " <mso:ribbon>" & vbNewLine
ribbonXML = ribbonXML + " <mso:qat>" & vbNewLine
ribbonXML = ribbonXML + " <mso:sharedControls>" & vbNewLine
ribbonXML = ribbonXML + " <mso:button id='RefreshRow' label='RefreshRow' imageMso='Refresh' onAction='RefreshRow'/>" & vbNewLine
ribbonXML = ribbonXML + " <mso:button id='RefreshSheet' label='RefreshSheet' imageMso='RefreshAll' onAction='RefreshSheet'/>" & vbNewLine
ribbonXML = ribbonXML + " </mso:sharedControls>" & vbNewLine
ribbonXML = ribbonXML + " </mso:qat>" & vbNewLine
ribbonXML = ribbonXML + " <mso:tabs>" & vbNewLine
ribbonXML = ribbonXML + " <mso:tab id='RefreshTab' label='Refresh Scores' insertBeforeQ='mso:TabFormat'>" & vbNewLine
ribbonXML = ribbonXML + " <mso:group id='RefreshScoreGrp' label='Refresh Scores' autoScale='true'>" & vbNewLine
ribbonXML = ribbonXML + " <mso:button id='RefreshRow' label='RefreshRow' imageMso='Refresh' onAction='RefreshRow'/>" & vbNewLine
ribbonXML = ribbonXML + " <mso:button id='RefreshSheet' label='RefreshSheet' imageMso='RefreshAll' onAction='RefreshSheet'/>" & vbNewLine
ribbonXML = ribbonXML + " </mso:group>" & vbNewLine
ribbonXML = ribbonXML + " </mso:tab>" & vbNewLine
ribbonXML = ribbonXML + " </mso:tabs>" & vbNewLine
ribbonXML = ribbonXML + " </mso:ribbon>" & vbNewLine
ribbonXML = ribbonXML + "</mso:customUI>"
ribbonXML = Replace(ribbonXML, """", "")
Open path & fileName For Output Access Write As hFile
Print #hFile, ribbonXML
Close hFile
End Sub
我希望打开工作簿时功能区应立即显示。不知道我错过了什么。不过,我使用了堆栈溢出线程中的此代码片段。
我确实有一个临时解决方案,即在事件中的 LoadCustRibbon 之后立即添加一个 MsgBox Workbook_Open。但我希望看到没有它的丝带。有什么帮助吗?
Private Sub Workbook_Open()
Call LoadCustRibbon("")
MsgBox "Please enable macros! Also, go through the 'Read Me' tab on how to use this workbook"
End Sub