以编程方式将 Min/Max 添加到 Excel 右下角的状态栏

Programmatically Add Min/Max to Status Bar in Bottom Right Corner in Excel

我想通过 VBA 操作 Excel 中的状态栏。我想添加 Min/Max 和更多的操作,但现在,这是我的目标。我尝试使用 Macro Recorder,但它没有拾取任何东西。要手动执行此操作,只需右键单击状态栏,然后单击 select "Max/Min" 即可显示。是否有通过 VBA 进行调整的隐藏方法?

注意:使用的是 Office 2013 x32,但我有许多最终用户可能正在使用 更新的 版本,例如 Office 365 and/or x64 版本 Excel,但没有 旧版本

这些值在 Windows 注册表中设置

HKCU\SOFTWARE\Microsoft\Office.0\Excel\StatusBar\

确保 16.0 替换为您的用户正在使用的内部 Office 版本号。

注册表项是

  • MaxValue
  • MinValue

它们是 REG_DWORD 类型,10 被禁用的地方启用。

请注意,设置注册表项后,无论您选择以下哪种解决方案,都需要重新启动Excel。

解决方案 1:组策略

通过服务器上的 GPO(组策略)设置它们,每个人都会在 Excel 的下一次启动时自动启用它。

解决方案 2:VBA

或者使用 VBA 保存注册表项,并使用 Application.Version 确定注册表路径所需的版本号,其中 returns 16.0 适用于 Office 2019 Professional。
可以在此处找到一种注册表操作方法:Read and write from/to registry in VBA(如果您 google,您也会找到其他方法)。

解决方案 3:批处理文件

您还可以使用批处理文件创建密钥:

reg add "HKCU\SOFTWARE\Microsoft\Office.0\Excel\StatusBar" /v MaxValue /t REG_DWORD /d 1
reg add "HKCU\SOFTWARE\Microsoft\Office.0\Excel\StatusBar" /v MinValue /t REG_DWORD /d 1

使用 reg /? 获取更多信息。

解决方案 4:Powershell

最后,Powershell 也成为可能

set-itemproperty -path HKCU:\SOFTWARE\Microsoft\Office.0\Excel\StatusBar -name MaxValue -Value 1
set-itemproperty -path HKCU:\SOFTWARE\Microsoft\Office.0\Excel\StatusBar -name MinValue Value -Value 1

参见 Working with Registry Entries

Excel 版本可以用

在 Powershell 中读取
$xl = New-Object -ComObject Excel.Application
$xl.Version
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
Remove-Variable xl

通过打开 Excel ComObject 并询问版本。

这是我的解决方案,完全使用 VBA。感谢@PEH 的 和许多替代方案。

您需要做的就是以某种方式调用 SetMinMaxEnabledInExcelStatusBar 模块。

注意:您完全可以跳过 SetRegKey,每个 SaveRegKey 运行,我不确定我更喜欢哪种方法。我主要使用 SetRegkey 作为我的测试模块,然后在它工作后将其单独放置,我的心态是如果 "RegKeyCurrentStateInt = RegKeyDesiredStateInt"

为什么会弄乱注册表
Public Const DWordRegKeyEnabled As Integer = 1
Public Const DWordRegKeyDisabled As Integer = 0

Public RegKeyStr As String, RegKeyLocStr As String, RegKeyNameStr As String
Public RegKeyDesiredStateInt As Integer, RegKeyCurrentStateInt As Integer
Public RegKeyFoundBool As Boolean

Public Sub SetMinMaxEnabledInExcelStatusBar()

 RegKeyDesiredStateInt = DWordRegKeyEnabled
 
 RegKeyLocStr = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\" & Application.Version & "\Excel\StatusBar\"
 RegKeyNameStr = "MaxValue"
 RegKeyStr = RegKeyLocStr & RegKeyNameStr
 Debug.Print "RegKeyStr = " & RegKeyStr
 Call SetRegKey(RegKeyStr, RegKeyDesiredStateInt)
 RegKeyNameStr = "MinValue"
 RegKeyStr = RegKeyLocStr & RegKeyNameStr
 Debug.Print "RegKeyStr = " & RegKeyStr
 Call SetRegKey(RegKeyStr, RegKeyDesiredStateInt)
 
End Sub

Public Sub SetRegKey(RegKeyStr As String, RegKeyDesiredStateInt As Integer)
 
 RegKeyFoundBool = RegKeyExists(RegKeyStr)
 Debug.Print "RegKeyFoundBool = " & RegKeyFoundBool
 
 If RegKeyFoundBool = False Then
  Debug.Print "RegKeyFoundBool = False"
  Call SaveRegKey(RegKeyStr, RegKeyDesiredStateInt)
 Else
  Debug.Print "RegKeyFoundBool = True"
  
  RegKeyCurrentStateInt = ReadRegKeyVal(RegKeyStr)
  Debug.Print "RegKeyCurrentStateInt = " & RegKeyCurrentStateInt
 
  If RegKeyCurrentStateInt <> RegKeyDesiredStateInt Then
   Debug.Print "RegKeyCurrentStateInt <> RegKeyDesiredStateInt"
   Call SaveRegKey(RegKeyStr, RegKeyDesiredStateInt)
  Else
   Debug.Print "RegKeyCurrentStateInt = RegKeyDesiredStateInt"
  End If
 End If

End Sub

Public Function ReadRegKeyVal(RegKeyStr As String) As Integer
 ReadRegKeyVal = CreateObject("WScript.Shell").RegRead(RegKeyStr)
End Function

Public Function RegKeyExists(RegKeyStr As String) As Boolean

  On Error GoTo ErrorHandler
  CreateObject("WScript.Shell").RegRead (RegKeyStr)
  RegKeyExists = True
  Exit Function
  
ErrorHandler:
  RegKeyExists = False
End Function

Public Sub SaveRegKey(RegKeyStr As String, RegKeyDesiredStateInt As Integer, Optional RegKeyType As String = "REG_DWORD")
 CreateObject("WScript.Shell").RegWrite RegKeyStr, RegKeyDesiredStateInt, RegKeyType
 Debug.Print "Generated --> " & RegKeyStr & "," & RegKeyDesiredStateInt & "," & RegKeyType
End Sub