VB.net Office VSTO Addin WorkbookBeforeSave does not have signature compatible

Viewed 45

I was trying to start with VSTO Addin for Office and I translated an example Microsoft published in their site . But I always receive this error when I implemented the WorkbookBeforeSave event:

Method 'Application_WorkbookBeforeSave' cannot handle event 'WorkbookBeforeSave' because they do not have a compatible signature

I searched in google and it looks the signature is ok. I tried this examples in C# and it worked fine.

this is the code I have:

Imports System.Windows.Forms
Imports Microsoft.Office.Interop.Excel

Public Class ThisAddIn
  
  Private Sub ThisAddIn_Startup() Handles Me.Startup
    AddHandler Application.WorkbookBeforeSave, New Excel.AppEvents_WorkbookBeforeSaveEventHandler(AddressOf Application_WorkbookBeforeSave)
  End Sub

  Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

  End Sub

Sub Application_WorkbookBeforeSave(wb As Workbook, SaveUI As Boolean, Cancel As Boolean) Handles Application.WorkbookBeforeSave(
    Dim activeworksheet As Worksheet = Application.ActiveSheet
    Dim firstRow As Range = activeworksheet.get_range("A1")

    firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown)
    Dim newFirstRow As Range = activeworksheet.get_tange("A1")
    newFirstRow.Value2 = "This text was added by using code"

  End Sub
End Class

The error is the line that has "(AddressOf Application_WorkbookBeforeSave)" Could somebody help me with this? Thanks

1 Answers

I found the problem, it was my mistake. The mistake was in the declaration :

Sub Application_WorkbookBeforeSave(wb As Workbook, SaveUI As Boolean, Cancel As Boolean) Handles Application.WorkbookBeforeSave

The argument Cancel as Boolean should be By Ref Cancel as Boolean

Related