VSTO制作任务窗格源码
由于Excel2010以上版本的加载项,一个工作簿一个进程,制作任务窗格需要多个事件相互配合。Excel2010(含2010)版本所有工作簿是公用进程!
依次添加两个列表框(ListView1和ListView2)还有一个分割线(Splitter),还有图标(ImageList)添加ListView1,Dock属性改为Top;添加Splitter,Dock属性改为Top,添加ListView2,Dock属性改为Fill添加ImageList。所有属性改好如下图:
准备两个图标。
鼠标右键
选择图像
添加Imports System.Windows.FormsPublic Class UserControl1Public Sub Wb()REM 知识点:工作簿变量(Excel.Workbook)/For next 循环/REM 让选中的工作簿跟着滚动条滚动(ListView1.EnsureVisible);ListView1.Items(i).Selected = TrueREM 1.依次把打开所有的工作簿添加到列表框中;2.在列表中需寻找等于当前工作簿并让滚动条滚动Dim wb As Excel.Workbook '定义工作簿变量On Error Resume NextDim i As Int16 '定义iREM.1.依次把打开所有的工作簿添加到列表框中ListView1.Items.Clear() '添加工作簿之前清楚ListView1中内容For Each wb In App.Workbooks '在打开工作簿中循环ListView1.Items.Add(wb.Name, 1) '依次把工作簿的名字添加到ListView中Next '结束循环REM 2.在列表中寻找等于当前工作簿名,并让滚动条随着滚动,同时在列表中选中当前工作簿REM 让列表中内容选中等于当前工作簿(ListView1.Items(i).Selected = True)For i = 0 To ListView1.Items.Count - 1If App.ActiveWorkbook.Name = ListView1.Items(i).Text Then '如果当前工作簿等于列表中内容则ListView1.Items(i).Selected = True '选中当前工作簿Exit For '退出循环End If '结束条件Next '结束循环'为了让列表呈现更好的选择效果,让滚动内容呈现向下可见5个内容If ListView1.Items.Count - (i + 1) < 5 ThenListView1.EnsureVisible(ListView1.Items.Count - 1)ElseListView1.EnsureVisible(i + 5) '让选选钟内容跟着滚动条滚动,且向下多显示5个内容End IfEnd SubPublic Sub Sht()REM 同工作簿原理一样Dim sht As Excel.Worksheet, i As Int16REM.1.依次把正在操作的工作簿中所有工作表添加列表2中ListView2.Items.Clear() '添加之前清楚所有内容,以免重复添加On Error Resume NextFor Each sht In App.ActiveWorkbook.Sheets '在操作的工作簿中所有工作表中循环ListView2.Items.Add(sht.Name, 0) '依次添加到ListView2中Next '结束循环REM 2.在列表中寻找等于当前工作表名,并让滚动条随着滚动,同时在列表中选中当前工作簿REM 让列表中内容选中等于当前工作表(ListView1.Items(i).Selected = True)For i = 0 To ListView2.Items.Count - 1 '在列表2中循环If App.ActiveSheet.Name = ListView2.Items(i).Text Then '如果正在操作的工作表等于列表框中内容则ListView2.Items(i).Selected = TrueExit ForEnd IfNext '结束循环'为了让列表呈现更好的选择效果,让滚动内容呈现向下可见5个内容If ListView2.Items.Count - (i + 1) < 5 ThenListView2.EnsureVisible(ListView2.Items.Count - 1)ElseListView2.EnsureVisible(i + 5) '让选选钟内容跟着滚动条滚动,且向下多显示5个内容End IfEnd SubPrivate Sub ListView1_Click(sender As Object, e As EventArgs) Handles ListView1.ClickDim Index As Int16Index = ListView1.SelectedIndices(index:=0) '获取当前选中的内容的索引号App.Workbooks(ListView1.Items(Index).Text).Activate() '激活当前选中的工作簿App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMaximized '让工作簿显示Call Sht() '选中的工作中的所有工作表,引用sub sht()End SubPrivate Sub ListView2_Click(sender As Object, e As EventArgs) Handles ListView2.ClickDim Index As Int16Index = ListView2.SelectedIndices(index:=0) '获取当前选中的内容索引号App.Sheets(ListView2.Items(Index).Text).Activate() '激活选中的工作表End SubPrivate Sub UserControl1_Load(sender As Object, e As EventArgs) Handles Me.LoadCall Wb()Call Sht()End SubEnd Class在工作簿和工作表时间中添加以下代码Private Sub Application_WorkbookActivate(Wb As Workbook) Handles Application.WorkbookActivateOn Error Resume NextIf Globals.Ribbons.Ribbon1.导航窗格.Checked ThenCall 用户控件.Wb()Call 用户控件.Sht()If App.Version <= 14 ThenExit SubElseMe.CustomTaskPanes.RemoveAt(0)用户控件 = New UserControl1任务窗格 = Me.CustomTaskPanes.Add(用户控件, "导航")With 任务窗格.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft.Width = 220.Visible = TrueEnd WithEnd IfElseMe.CustomTaskPanes.RemoveAt(0)End IfEnd SubPrivate Sub Application_SheetActivate(Sh As Object) Handles Application.SheetActivateOn Error Resume NextCall 用户控件.Sht()End SubEnd ClassPrivate Sub 导航窗格_Click(sender As Object, e As RibbonControlEventArgs) Handles 导航窗格.ClickOn Error Resume NextIf 导航窗格.Checked ThenGlobals.ThisAddIn.用户控件 = New UserControl1Globals.ThisAddIn.任务窗格 = Globals.ThisAddIn.CustomTaskPanes.Add(Globals.ThisAddIn.用户控件, "导航")With Globals.ThisAddIn.任务窗格.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft.Visible = True.Width = 220End WithElseGlobals.ThisAddIn.CustomTaskPanes.RemoveAt(0)End IfEnd Sub开始要声明两个变量Public 用户控件 As UserControl1Public 任务窗格 As Microsoft.Office.Tools.CustomTaskPane