【问题标题】:Handles clause requires a WithEvents variable defined in the containing type or one of its base types?Handles 子句需要在包含类型或其基类型之一中定义的 WithEvents 变量?
【发布时间】:2015-06-18 04:50:26
【问题描述】:

当我在 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

【问题讨论】:

    标签: vb.net visual-studio-2013


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-12-02
      • 2023-03-20
      • 1970-01-01
      • 2019-04-19
      • 2018-10-17
      • 2022-11-08
      • 1970-01-01
      相关资源
      最近更新 更多