title: Excel VBA: Ink Picture Events Fail to Fire
body: <p>I am trying to write a VBA script to allow an Excel sheet to scroll down a few rows normally before freezing panes at a certain point. Here is the code I would like to run whenever the user scrolls the page:</p>
  
  <pre><code>Sub FreezeView()
      Dim lngTopRow As Long
      With ActiveWindow.VisibleRange
          lngTopRow = .Row
      End With
      If ((lngTopRow &gt;= 7) And (ActiveWindow.FreezePanes = False)) Then
          ActiveSheet.Range("B10").Select
          ActiveWindow.FreezePanes = True
      Else
          If ((lngTopRow = 10) And (ActiveWindow.FreezePanes = True)) Then
              ActiveWindow.FreezePanes = False
          End If
      End If
  '    MsgBox (lngTopRow)
  End Sub
  </code></pre>
  
  <p>To my knowledge, Excel VBA doesn't have any direct events to detect scrolling; however, based on <a href="https://www.mrexcel.com/forum/excel-questions/287154-worksheet-onscroll-event-how-cool.html" rel="nofollow noreferrer">This Article</a>, this is supposedly possible through the use of an InkPicture object. Unfortunately, the example sheet on this page is no longer hosted. I have attempted to access the InkPicture's _Painting event (and other events) to make this work, but haven't been able to get any of these events to "fire." Here is the code I have been using to try to accomplish this:</p>
  
  <p>First, I have created a class module with the name "<strong>clsInkPicEvents</strong>" with the following code:</p>
  
  <pre><code>Public WithEvents InkPicEvents As InkPicture
  
  Private Sub Class_Initialize()
      MsgBox ("Initialized")
  End Sub
  
  Private Sub InkPicEvents_Click()
      MsgBox ("Click")
  End Sub
  
  Private Sub InkPicEvents_Painted(ByVal hDC As Long, ByVal Rect As MSINKAUTLib.IInkRectangle)
      MsgBox ("Painted")
  End Sub
  
  Private Sub InkPicEvents_Painting(ByVal hDC As Long, ByVal Rect As MSINKAUTLib.IInkRectangle, Allow As Boolean)
      MsgBox ("Painting")
  End Sub
  
  Private Sub InkPicEvents_Resize(Left As Long, Top As Long, Right As Long, Bottom As Long)
      MsgBox ("Resize")
  End Sub
  
  Private Sub InkPicEvents_Stroke(ByVal Cursor As MSINKAUTLib.IInkCursor, ByVal Stroke As MSINKAUTLib.IInkStrokeDisp, Cancel As Boolean)
      MsgBox ("Stroke")
  End Sub
  </code></pre>
  
  <p>Then, in "<strong>Module 1</strong>" I have the following script to create the InkPicture object and instantiate the class. I'm really not too familiar with this so I'm not certain I am doing this correctly:</p>
  
  <pre><code>Private Sub AddInkPic()
      Set oInkPic1 = Sheets("Sheet1").OLEObjects.Add _
      (ClassType:="msinkaut.InkPicture.1", Left:=Sheets("Sheet1").Range("V12").Left, Top:=Sheets("Sheet1").Range("V12").Top, _
      Width:=100, Height:=100)
  End Sub
  
  Private Sub Class_Initialize1()
      Dim clsInkPic1 As clsInkPicEvents
      Set clsInkPic1 = New clsInkPicEvents
      Set clsInkPic1.InkPicEvents = Sheets("Sheet1").OLEObjects("InkPicture1").Object
      clsInkPic1.InkPicEvents.SetEventInterest ICEI_Painting, True
      MsgBox (clsInkPic1.InkPicEvents.GetEventInterest(ICEI_Painting))
  End Sub
  </code></pre>
  
  <p>I attempted to manually set the event interest to true for the _Painting event as I was suspicious that it was set to false by default, but that doesn't seem to have worked. I was also suspicious that one of the properties of the InkPicture object may be the cause, so attempted to change different properties such as:</p>
  
  <pre><code>Private Sub EditVals()
      Sheets("Sheet1").OLEObjects("InkPicture1").Object.Enabled = True
      Sheets("Sheet1").OLEObjects("InkPicture1").Object.InkEnabled = True
      Sheets("Sheet1").OLEObjects("InkPicture1").Object.AutoRedraw = True
      Sheets("Sheet1").OLEObjects("InkPicture1").Object.CollectionMode = 2
      Sheets("Sheet1").OLEObjects("InkPicture1").Object.EditingMode = 0
  End Sub
  </code></pre>
  
  <p>But that didn't seem to change anything. Now I'm wondering if it has something to do with the Design Mode, because I notice that I can't ink the InkPicture object after it is created until I click Design Mode on and off again, so I'm wondering if the object isn't "active" until I do that, and if enabling and then disabling Design Mode might somehow "clear" the class instance or something? Really not sure. Anyway, any help would be appreciated! Thanks.</p>