C# 在Sharepoint环境中使用Microsoft.Office.Interop.Word

C# 在Sharepoint环境中使用Microsoft.Office.Interop.Word,c#,sharepoint,interop,office-interop,C#,Sharepoint,Interop,Office Interop,你必须原谅我对这个密码的无知。我已经编写了一些代码来修改事件接收器。我已经为SharePoint设置了一个开发环境,并最终使其能够访问和更改代码的某些元素 但是,以下行出现故障: Word.Application wordApp = new Word.Application(); 在这种情况下,它似乎无法打开Sharepoint服务器上安装的本地word应用程序来处理上载的文档。关于如何在SharePoint环境中作为事件接收器启动word应用程序的任何提示 为了完整起见,下面提供了完整的代码

你必须原谅我对这个密码的无知。我已经编写了一些代码来修改事件接收器。我已经为SharePoint设置了一个开发环境,并最终使其能够访问和更改代码的某些元素

但是,以下行出现故障:

Word.Application wordApp = new Word.Application();
在这种情况下,它似乎无法打开Sharepoint服务器上安装的本地word应用程序来处理上载的文档。关于如何在SharePoint环境中作为事件接收器启动word应用程序的任何提示

为了完整起见,下面提供了完整的代码

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Word = Microsoft.Office.Interop.Word;


namespace chrisclementen.chrisclementen
{

public class chrisclementen : SPItemEventReceiver
{
    /// <summary>
    /// An item was added.
    /// </summary>
    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        commentscheck(properties);
    }


     private void commentscheck(SPItemEventProperties properties)
    {

        bool commentsorrevisions = false;

        SPListItem item = properties.ListItem;
        SPFile file = item.File;
        if (properties.AfterUrl.EndsWith("docx"))
            {

                commentsorrevisions = WordCommentsChecker(file, properties);

            }


     }
     private static bool WordCommentsChecker(SPFile file, SPItemEventProperties properties)
{

    bool outcome = false;

    Word.Application wordApp = new Word.Application();
    properties.ListItem["Title"] = "bextor";
    properties.ListItem.Update();
    Word.Document document = wordApp.Documents.Open(file);
    int commentscount = document.Comments.Count;
    int revisionscount = document.Revisions.Count;

    if (commentscount != 0 || revisionscount != 0)
    {
        Console.WriteLine("comments");
        document.ActiveWindow.Close();
        wordApp.Application.Quit(-1);
        outcome = true;

    }

    else
    {
        Console.WriteLine("No Comments.");
        document.ActiveWindow.Close();
        wordApp.Application.Quit(-1);
        outcome = false;
    }

    return outcome;
}
    /// <summary>
    /// An item was updated.
    /// </summary>
    public override void ItemUpdated(SPItemEventProperties properties)
    {
        commentscheck(properties);
    }
}
}
关于如何在SharePoint环境中作为事件接收器启动word应用程序的提示

不可以。您不应该在无头服务器进程中为用户使用Word桌面应用程序。Microsoft明确指出,这可能并将产生问题,正如您现在可能正在经历的那样

发件人:

Microsoft目前不建议也不支持从任何无人参与、非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT服务)自动化Microsoft Office应用程序,因为在该环境中运行Office时,Office可能会表现出不稳定的行为和/或死锁


就这样。你应该寻找另一种读写Word文档的方法。有许多库可以做到这一点。

对于上传的给定文档,实际上没有其他方法可以访问评论或评论。是的,正如我所说的那样。有多个库可以做到这一点,但如果你的问题是如何打开Word应用程序,答案是:你不能也不应该这样做。@dipl0 Patrick的答案完全正确,而且是正确的。任何其他了解这一主题的人都会做出同样的回答。如果您必须有一个建议,那么请查看OpenXMLSDK。