在Sharepoint环境中使用Microsoft.Office.Interop.Word [英] using Microsoft.Office.Interop.Word in Sharepoint environment

查看:123
本文介绍了在Sharepoint环境中使用Microsoft.Office.Interop.Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将不得不原谅我对这段代码的无知.我已经编写了一些代码来修改事件接收器.我已经为SharePoint建立了一个开发环境,最后使它可以访问和更改代码的某些元素.

You'll have to forgive my ignorance with regards to this code. I have written some code to modify an event receiver. I have set up a development environment for SharePoint and finally got it to access and change certain elements of the code.

但是,它在以下行中失败了:

However, it is the following line where it fails:

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

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

In that, it doesn't seem to be able to open the local word application installed on the Sharepoint server in order to process the uploaded document. Any tips on how I would be able to enable the starting of the word application in the SharePoint environment as an event receiver.

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

The full code is provided below for the sake of completeness

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应用程序作为事件接收器的任何提示?

Any tips on how I would be able to enable the starting of the word application in the SharePoint environment as an event receiver?

不.您不应在服务器进程(无头)中使用Word(用户的桌面应用程序). Microsoft明确声明,这可能会并且将产生问题,就像您现在正在经历的那样.

No. You should not use Word (a desktop application for users) in a server process (headless). Microsoft explicitly states this can and will produce problems, as you probably are experiencing now.

来自注意事项Office的服务器端自动化:

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

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

就是这样.您应该寻找另一种读取或写入Word文档的方式.有许多库可以做到这一点.

So that is it. You should look for another way to read or write Word documents. There are many libraries capable of doing just that.

这篇关于在Sharepoint环境中使用Microsoft.Office.Interop.Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆