Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量吗?

时间:2015-06-18 04:50:26

标签: vb.net visual-studio-2013

当我在Visual Studio 2013中运行以下代码时,它显示:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types

Private Sub Application_DocumentBeforeSave(ByVal Doc As Word.Document, ByRef SaveAsUI As Boolean, _
    ByRef Cancel As Boolean) Handles Application.DocumentBeforeSave
    Doc.Paragraphs(1).Range.InsertParagraphBefore()
    Doc.Paragraphs(1).Range.Text = "This text was added by using code." 
End Sub

谁能告诉我如何解决这个问题? 上面的代码来自https://msdn.microsoft.com/en-us/library/cc442946.aspx

1 个答案:

答案 0 :(得分:0)

该代码假定当前类具有Application类型Microsoft.Office.Interop.Word.Application的字段(包括DocumentBeforeSave事件)

  1. 创建新的Word 2013加载项项目
  2. 打开ThisAddin.vb文件
  3. 请注意,此类还有ThisAddin.Designer.vb中定义的部分类,其中声明了Application字段:
  4. Friend WithEvents Application As Microsoft.Office.Interop.Word.Application