当我在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
答案 0 :(得分:0)
该代码假定当前类具有Application
类型Microsoft.Office.Interop.Word.Application
的字段(包括DocumentBeforeSave
事件)
ThisAddin.vb
文件ThisAddin.Designer.vb
中定义的部分类,其中声明了Application
字段:Friend WithEvents Application As Microsoft.Office.Interop.Word.Application