Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...