Excel - 如何根据 Workbook_Open 事件创建按钮

Excel - How to create button upon Workbook_Open event

我正在尝试创建一个 Excel 加载项以在打开任何工作簿时创建一个简单的按钮,但我得到

Object variable or With Block variable not set

我认为这是因为技术上还没有 'ActiveWorkbook'。

我要做的第一件事是删除当前 sheet 上的所有按钮。然后我想放一个按钮。

有人知道如何实现吗?

代码

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。但是按钮没有被创建。

是这样的吗?

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

参见示例 here

归功于 http://www.jkp-ads.com/Articles/FixLinks2UDF.asp

注意:下面还有一个我没有post的模块,里面有宏Project_Count 仅当工作簿名称为 TT_GO_ExceptionReport

时,我才绑定到放置在工作簿上的按钮

我还有一个 VBScript 可以下载加载项,将其放在用户加载项文件夹中,然后进行安装。如果您想知道如何做到这一点,请发表评论。

解决问题的加载项代码:

本工作簿

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 选项显式

'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 选项显式

'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

Class 名为 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