Excel – 如何在Workbook_Open事件上创buildbutton

我试图使任何工作簿打开时,Excel加载项创build一个简单的button,但我越来越

对象variables或With Blockvariables未设置

我认为这是因为技术上没有“ActiveWorkbook”呢。

我想要做的第一件事是删除工作表上当前的任何button。 然后我想要放一个button。

任何人都知道如何做到这一点?

Private Sub Workbook_Open() ActiveWorkbook.ActiveSheet.Buttons.Delete Dim CommandButton As Button Set CommandButton = ActiveWorkbook.ActiveSheet.Buttons.Add(1200, 100, 200, 75) With CommandButton .OnAction = "Test_Press" .Caption = "Press for Test" .Name = "Test" End With End Sub 

然后我有一个Private Sub Test_Press()来显示一个MsgBox。 该button没有被创build。

信贷去http://www.jkp-ads.com/Articles/FixLinks2UDF.asp

注:我有另一个模块,我没有发布下面,其中包含macrosProject_Count绑定到我放在工作簿上的button,只有当工作簿名称是TT_GO_ExceptionReport

我也有一个VBScript,下载加载项,放在用户的插件文件夹,并安装它。 如果你想知道如何做,请留下评论。

解决问题的插件代码:

的ThisWorkbook

 Option Explicit Private Sub Workbook_Open() ' Purpose : Code run at opening of workbook '------------------------------------------------------------------------- 'Initialise the application InitApp modProcessWBOpen.TimesLooped = 0 Application.OnTime Now + TimeValue("00:00:03"), "CheckIfBookOpened" End Sub 

模块1名为modInit Option Explicit

 'Create a module level object variable that will keep the instance of the 'event listener in memory (and hence alive) Dim moAppEventHandler As cAppEvents Sub InitApp() 'Create a new instance of cAppEvents class Set moAppEventHandler = New cAppEvents With moAppEventHandler 'Tell it to listen to Excel's events Set .App = Application End With End Sub 

模块2名为modProcessWBOpen Option Explicit

 'Counter to keep track of how many workbooks are open Dim mlBookCount As Long 'Counter to check how many times we've looped Private mlTimesLooped As Long ' Purpose : When a new workbook is opened, this sub will be run. ' Called from: clsAppEvents.App_Workbook_Open and ThisWorkbook.Workbook_Open '------------------------------------------------------------------------- Sub ProcessNewBookOpened(oBk As Workbook) If oBk Is Nothing Then Exit Sub If oBk Is ThisWorkbook Then Exit Sub If oBk.IsInplace Then Exit Sub CountBooks 'This checks to make sure the name of the new book matches what I 'want to place the button on If oBk.Name = "TT_GO_ExceptionReport.xlsm" Then Dim CommandButton As Button Set CommandButton = Workbooks("TT_GO_ExceptionReport.xlsm").Sheets(1).Buttons.Add(1200, 100, 200, 75) With CommandButton .OnAction = "Project_Count" .Caption = "Press for Simplified Overview" .Name = "Simplified Overview" End With End If End Sub Sub CountBooks() mlBookCount = Workbooks.Count End Sub Function BookAdded() As Boolean If mlBookCount <> Workbooks.Count Then BookAdded = True CountBooks End If End Function ' Purpose : Checks if a new workbook has been opened ' (repeatedly until activeworkbook is not nothing) '------------------------------------------------------------------------- Sub CheckIfBookOpened() If BookAdded Then If ActiveWorkbook Is Nothing Then mlBookCount = 0 TimesLooped = TimesLooped + 1 'May be needed if Excel is opened from Internet explorer Application.Visible = True If TimesLooped < 20 Then Application.OnTime Now + TimeValue("00:00:01"), "CheckIfBookOpened" Else TimesLooped = 0 End If Else ProcessNewBookOpened ActiveWorkbook End If End If End Sub Public Property Get TimesLooped() As Long TimesLooped = mlTimesLooped End Property Public Property Let TimesLooped(ByVal lTimesLooped As Long) mlTimesLooped = lTimesLooped End Property 

名为cAppEvents的 类模块

 ' Purpose : Handles Excel Application events '------------------------------------------------------------------------- Option Explicit 'This object variable will hold the object who's events we want to respond to Public WithEvents App As Application Private Sub App_WorkbookOpen(ByVal Wb As Workbook) 'Make sure newly opened book is valid ProcessNewBookOpened Wb End Sub Private Sub Class_Terminate() Set App = Nothing End Sub 

像这样的东西?

 Option Explicit Sub Button() Dim cButton As Button Dim rng As Range Dim i As Long ActiveSheet.Buttons.Delete For i = 2 To 3 Step 2 Set rng = ActiveSheet.Range(Cells(i, 2), Cells(i, 2)) Set cButton = ActiveSheet.Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height) With cButton .OnAction = "Test_Press" .Caption = "Press for Test " & i .Name = "Test" & i End With Next i End Sub 

在这里看到例子