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

问题描述 投票:-1回答:1

你必须原谅我对这段代码的无知。我编写了一些代码来修改事件接收器。我为SharePoint设置了一个开发环境,最后让它访问并更改了代码的某些元素。

但是,它失败的是以下行:

Word.Application wordApp = new Word.Application();

在那,它似乎无法打开Sharepoint服务器上安装的本地word应用程序,以处理上传的文档。有关如何在SharePoint环境中启用单词应用程序作为事件接收器的任何提示。

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

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);
    }
}
}
c# sharepoint interop office-interop
1个回答
1
投票

有关如何在SharePoint环境中启用单词应用程序作为事件接收器的任何提示?

不可以。您不应在服务器进程中使用Word(用户的桌面应用程序)(无头)。微软明确表示这可以而且会产生问题,正如您现在可能正在经历的那样。

来自Considerations for server-side Automation of Office

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

就是这样。您应该寻找另一种方式来读取或写入Word文档。有许多图书馆能够做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.