title: Adding event code to checkbox in PowerPoint VBA
body: <p>I am developing a PowerPoint 2010 deck that presents the user with a series of pages containing one statement, one checkbox (built from a label element to enable changing the size of the checkbox) and forward/back arrows on each page.</p>
  
  <p>Since this will be used on numerous projects with varying numbers of pages I am building the “deck” dynamically using PowerPoint VBA to construct the pages dynamically from an Excel spreadsheet containing the list of individual statements. </p>
  
  <p>I have been able to write the VBA code to open the Excel file, read the statements into an array in PowerPoint and construct the appropriate number of pages with all of the elements on the page. To this point everything works fine. Where I am having difficulty is in assigning the click action to the checkbox. </p>
  
  <p>Here is the code that is called by the page building routine to insert the checkbox (obviously there is more code prior to this for accessing the Excel file, creating the pages and adding the “statement” text boxes...all of which works):</p>
  
  <pre><code>Sub AddSelectBox(Index As Integer, pptBuildingSlide As Slide)
  
  'Add Checkbox
      With pptBuildingSlide.Shapes.AddOLEObject(Left:=342, Top:=294, Width:=42, Height:=42, ClassName:="Forms.Label.1")
          .Name = "label" &amp; Index
          .OLEFormat.Object.Font.Name = "Wingdings 2"
          .OLEFormat.Object.Font.Charset = "2"
          .OLEFormat.Object.Caption = "£"
          .OLEFormat.Object.Font.Size = 40
      End With
  
  'Add Checkbox Click Code
  '(CODE FOR ADDING CLICK EVENT TO EACH BOX GOES HERE)
  
  End Sub
  </code></pre>
  
  <p>The checkbox on each page has a discreet name keyed to the page number (e.g. Label1, Label2, etc.). I need to add the following code to each checkbox on each page to toggle the checkmark so later in the program I can see which were checked by reading the “caption” attributes. (The font is set to “Wingdings 2” to give a blank box and a checked box on click)</p>
  
  <pre><code>Private Sub Label1_Click()
  
     If Label1.Caption = "£" Then
         Label1.Caption = "R"
     Else
         Label1.Caption = "£"
     End If
  
  End Sub
  </code></pre>
  
  <p>I have searched the web looking for any references to add event code dynamically and found a number of examples (e.g. <a href="https://stackoverflow.com/questions/566770/assign-on-click-vba-function-to-a-dynamically-created-button-on-excel-userform">Assign on-click VBA function to a dynamically created button on Excel Userform</a>) but almost all are for Excel or Access. I should point out that coding is “not my day job” and I have managed to get this far reading “Mastering VBA for Office 2003” and web searching…so my ability to translate those examples to PowerPoint has come up short. Thanks for any help you can offer.</p>
  
  <p>5/29 Additional information:<br>
  I came across the <code>.CreateEventProc</code> method as a way to write code into VBA. The example I found was written for Excel at <a href="http://www.cpearson.com/excel/vbe.aspx" rel="nofollow noreferrer">this site</a>. I've gotten this far with it (the message box code would be replaced with the click code but I was just using this for testing to avoid introducing other errors)...</p>
  
  <pre><code>Sub CreateEventProcedure()
      Dim VBProj As VBIDE.VBProject
      Dim VBComp As VBIDE.VBComponent
      Dim CodeMod As VBIDE.CodeModule
      Dim LineNum As Long
      Const DQUOTE = """" ' one " character
  
      Set VBProj = ActivePresentation.VBProject
      Set VBComp = VBProj.VBComponents(Slides(1))
      Set CodeMod = VBComp.CodeModule
  
      With CodeMod
          LineNum = .CreateEventProc("Click", "Label1")
          LineNum = LineNum + 1
          .InsertLines LineNum, "    MsgBox " &amp; DQUOTE &amp; "Hello World" &amp; DQUOTE
      End With
  End Sub
  </code></pre>
  
  <p>...but get a "Compile Error: Sub or Function not defined" at <code>(slides(1))</code>. Any help cleaning it up (if it is in fact an appropriate solution) would be appreciated.</p>