using Microsoft.Office.Interop.Word in Sharepoint environment

北城余情 提交于 2019-12-25 17:19:06

问题


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();

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);
    }
}
}

回答1:


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

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.

From Considerations for server-side Automation of 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.

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.



来源:https://stackoverflow.com/questions/52225264/using-microsoft-office-interop-word-in-sharepoint-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!