Cannot implicitly convert type 'Word.ApplicationEvents2_DocumentBeforeSaveEventHandler' to 'Word.ApplicationEvents4_DocumentBeforeSaveEventHandler'

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月9日 00:15:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定