乐博网lob.cn提示:调试环境为 vs2008 + windows2003 / windows2008 / xp / vista
Public Class Sheet1 Private MySetMenu As Office.CommandBarButton Private MyCancelMenu As Office.CommandBarButton Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup AddMenuBar() End Sub Private Sub Sheet1_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown End Sub Private missing = System.Reflection.Missing.Value Private Sub AddMenuBar() Try Dim MyCommandBarPopup As Office.CommandBarPopup = Nothing Dim MyCommandBarMenu As Office.CommandBar = _ CType(Application.CommandBars.ActiveMenuBar, Office.CommandBar) Dim MyControlsCount As Integer = MyCommandBarMenu.Controls.Count MyCommandBarPopup = CType(MyCommandBarMenu.Controls.Add( _ Office.MsoControlType.msoControlPopup, missing, missing, _ MyControlsCount, True), Office.CommandBarPopup) If (MyCommandBarPopup IsNot Nothing) Then MyCommandBarPopup.Caption = "工作簿保护" MySetMenu = CType(MyCommandBarPopup.Controls.Add( _ Office.MsoControlType.msoControlButton, missing, missing, _ missing, True), Office.CommandBarButton) MySetMenu.Caption = "设置用户密码" MySetMenu.TooltipText = "设置当前Excel工作簿的用户密码" MySetMenu.FaceId = 65 AddHandler MySetMenu.Click, AddressOf MySetMenuCommand_Click MyCancelMenu = CType(MyCommandBarPopup.Controls.Add( _ Office.MsoControlType.msoControlButton, missing, missing, _ missing, True), Office.CommandBarButton) MyCancelMenu.Caption = "取消用户密码" MyCancelMenu.TooltipText = "取消当前Excel工作簿的用户密码" MyCancelMenu.FaceId = 66 AddHandler MyCancelMenu.Click, AddressOf MyCancelMenuCommand_Click End If Catch ex As Exception MessageBox.Show(ex.Message, "乐博网信息提示", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub '取消用户密码 '更多.net源码和实例,来自乐博网 www.lob.cn Private Sub MyCancelMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) If (MessageBox.Show("是否确认取消当前Excel工作簿的用户密码?", _ "乐博网信息提示", MessageBoxButtons.YesNo) = DialogResult.Yes) Then Globals.ThisWorkbook.Password = "" MessageBox.Show("已经成功取消当前Excel工作簿的用户密码!", _ "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End Sub End Class
|