英文:
Cannot implicitly convert type 'Word.ApplicationEvents2_DocumentBeforeSaveEventHandler' to 'Word.ApplicationEvents4_DocumentBeforeSaveEventHandler'
问题
我已将Microsoft Word对象库的版本从15.0升级到16.0,现在出现以下错误:
无法隐式转换类型
'Microsoft.Office.Interop.Word.ApplicationEvents2_DocumentBeforeSaveEventHandler'
为
'Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler'
代码执行以下操作:
Word.Application wordApp = new Word.Application();
wordApp.DocumentBeforePrint += new Word.ApplicationEvents2_DocumentBeforePrintEventHandler(wordApp_DocumentBeforePrint);
似乎存在ApplicationEvents2和ApplicationEvents4之间的不匹配:
namespace Microsoft.Office.Interop.Word
{
[ComVisible(false)]
[TypeLibType(16)]
public delegate void ApplicationEvents2_DocumentBeforePrintEventHandler(Document Doc, ref bool Cancel);
}
namespace Microsoft.Office.Interop.Word
{
[ComEventInterface(typeof(ApplicationEvents4), typeof(ApplicationEvents4_EventProvider))]
[ComVisible(false)]
[TypeLibType(16)]
public interface ApplicationEvents4_Event
{
event ApplicationEvents4_DocumentBeforePrintEventHandler DocumentBeforePrint;
...
}
}
英文:
I have updated my version Microsoft Word Object Library from 15.0 to 16.0 and I am now getting the following error:
Cannot implicitly convert type
'Microsoft.Office.Interop.Word.ApplicationEvents2_DocumentBeforeSaveEventHandler'
to
'Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler'
The code does the following:
`Word.Application wordApp = new Word.Application();
wordApp.DocumentBeforePrint += new Word.ApplicationEvents2_DocumentBeforePrintEventHandler(wordApp_DocumentBeforePrint);`
I seem to have a mismatch between ApplicationEvents2 and ApplicationEvents4:
`namespace Microsoft.Office.Interop.Word
{
[ComVisible(false)]
[TypeLibType(16)]
public delegate void ApplicationEvents2_DocumentBeforePrintEventHandler(Document Doc, ref bool Cancel);
}`
namespace Microsoft.Office.Interop.Word
{
[ComEventInterface(typeof(ApplicationEvents4), typeof(ApplicationEvents4_EventProvider))]
[ComVisible(false)]
[TypeLibType(16)]
public interface ApplicationEvents4_Event
{
event ApplicationEvents4_DocumentBeforePrintEventHandler DocumentBeforePrint;
...
}
}`
答案1
得分: 0
这个错误消息表示你正在尝试在更新后的版本中使用的 DocumentBeforePrint 事件的委托类型已经发生了改变。ApplicationEvents2_DocumentBeforePrintEventHandler 委托类型已经更改为 ApplicationEvents4_DocumentBeforePrintEventHandler。
可以尝试像这样做:
Word.Application wordApp = new Word.Application();
wordApp.DocumentBeforePrint += new Word.ApplicationEvents4_DocumentBeforePrintEventHandler(wordApp_DocumentBeforePrint);
此外,请确保更新 wordApp_DocumentBeforePrint 的签名以匹配 ApplicationEvents4_DocumentBeforePrintEventHandler 委托的期望签名。
Microsoft 文档中的 ApplicationEvents4_DocumentBeforePrintEventHandler 委托
英文:
This error message says that the delegate type that you are trying to use for the DocumentBeforePrint event has changed in the updated version. The ApplicationEvents2_DocumentBeforePrintEventHandler delegate type has been changed to ApplicationEvents4_DocumentBeforePrintEventHandler.
Can try like this:
Word.Application wordApp = new Word.Application();
wordApp.DocumentBeforePrint += new Word.ApplicationEvents4_DocumentBeforePrintEventHandler(wordApp_DocumentBeforePrint);
Also, make sure to update the signature of the wordApp_DocumentBeforePrint to match the expected signature of the ApplicationEvents4_DocumentBeforePrintEventHandler delegate
Microsoft docs for ApplicationEvents4_DocumentBeforePrintEventHandler delegate
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论