none
Reviewing Pane feature Microsoft.office.interop.word RRS feed

  • Question

  • I searched all possible keywords to search how i can use Reviewing pane feature of MS Word in C# using Microsoft.office.interop.word namespace to get hold of the user name who has modified the file.I have managed open the MS Word file and to keep the TrackRevisions feature to true. But I am finding it difficult to get hold of the Reviewing pane and feature to display the user who modified it. Following is the code which I am executing to open the file and keeping the TrackRevisions feature on.

    public ActionResult def()
                {
                    Word.Application app = new Word.Application();
                    Word.Document dd = app.Documents.Open("C:\\hi12.docx");
                    **dd.TrackRevisions = true;**
                    int count = dd.Words.Count;
                    dd.Save();
    
                    dd.Close(ref Nothing, ref format, ref Nothing);
                    return File(("C:\\hi12.docx"), ("application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
                }

    How can I use TaskPane interface from Microsoft.office.interop.word namespace?

    Wednesday, January 2, 2013 5:27 AM

Answers

  • Ankur,

    I am sorry.  I misread the original post.  I thought you were wanting to open the reviwing Pane programmatically which is something that I was trying to do.  The line of code that I gave you will cause the Reviewing Pane to open after you have opened the document.  The way to get to the Author property of the revision is as Cindy said except that C# does not use Items.  You need to type the line of code like this ...

        string userName = document.Revisions[x].Author

    Where x is the index of the Revision you want to inspect.  And remember that the indexing of all of the collections of objects exposed in the interop assembly is 1-based and not 0-based as is the norm for C#.

    -Tom


    Tom Schoeneweis Burghboy on loan to Utah ***

    Friday, January 4, 2013 7:50 AM
  • Hi Ankur,

    'x' is the index of the revision that you want to inspect.  It will be between 1 and the number of revisions in the Document object or the Range object you are looking at.  if you wanted to loop through the revisions though you can use foreach

    foreach (Revision rev in doc.Revisions)
    {
        string editedBy = rev.Author;
        // do whatever you need to with the revision author's name
    }
    -Tom

    Tom Schoeneweis

    Friday, January 4, 2013 8:20 AM
  • Hi Ankur,

    Remember that the indexing on these collections is 1-based, so you should make sure that the Count  > 0 before trying to access the Revision ... like this:

    int cnt = tempDoc.Revisions.Count;
    
    if (cnt > 0)
    {
        string author = tempDoc.Revisions[cnt].Author;
    }

    That way you will not try to access something that does not exist.  The first Revision will be Revisions[1], not Revisions[0].

    -Tom


    Tom Schoeneweis

    Friday, January 4, 2013 8:38 AM
  • Hi Ankur,

    The following CodeProject post looks like it addresses what you want.  It will save the entire file (which should include the revision information, etc). Solution 1 tells how to create the field you want to store the info in and use File class to read the file to a byte[] and then store it.

    http://www.codeproject.com/Questions/343921/how-to-store-word-file-as-binary-data-into-sql-ser

    -Tom


    Tom Schoeneweis

    Friday, January 4, 2013 6:44 PM

All replies

  • Hi Ankur

    You cannot interact with the task pane. But you should be able to access the Revisions present in the document. For example:
      dd.Revisions.Count


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 2, 2013 6:31 AM
    Moderator
  • @Cindy : Doing this can i access the names of the users who have modified it.? And how can I save the document with the same name it was saved first time. Or it is that the way that I am using to open the document only opens in a saveAS mode
    Wednesday, January 2, 2013 6:34 AM
  • Hi Ankur

    >>access the names of the users who have modified it.? <<

    The following will give you the name of the author who made the first Revision in the document. You pass Item the index value. You can also get the date and various other pieces of information through Item.
      Revisions.Item(1).Author

    <<how can I save the document with the same name it was saved first time. Or it is that the way that I am using to open the document only opens in a saveAS mode>>

    I'm not sure what you're asking, here. If no changes are made the document can be closed without saving; if changes are made the document can be closed without saving, and any changes are discarded.

    If you want to save the changes and retain the name, simply use the Save method.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 2, 2013 7:15 AM
    Moderator
  • Hi cindy.

    My problem about saveing is, when I open the document it gets opend by the name of function in this case its def()(please refer above code). I make the changes, save method is giving me some errorcross some articles where it is stated that use OPEN XML SDK for accessing the MS Office documents instead of using what I am using. So can you provide me a useful link so that I can start of with Open XML sdk.

    Reagrds,

    Ankur

    Wednesday, January 2, 2013 7:28 AM
  • hi cindy.

    I am not able to get Revisions.Item(1).Author. How do i write the code referring to the fucntion I am executing.

    dd.Revisions. ....??????

    Please Help

    Wednesday, January 2, 2013 8:11 AM
  • I have managed to open a word document and I am keeping Track of the revisions also. How can I get the user name who modified it. Can i create an addin for it. Please help. Following is the code I am executing to open the Word file

    public ActionResult def()
            {
                Word.Application app = new Word.Application();
                Word.Document dd = app.Documents.Open("C:\\hi12.docx");
                dd.TrackRevisions = true;
                int count = dd.Words.Count;
             
                dd.Close(ref Nothing, ref format, ref Nothing);
              
                return File(("C:\\hi12.docx"), ("application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
            }

    Wednesday, January 2, 2013 9:04 AM
  • Hi Ankur

    <<I am not able to get Revisions.Item(1).Author>>

    Ah, sorry, my brain is in VB-language mode today. In VB you specify an index value of a Collection with (number); in C# the equivalent is [number].

    For C# you should be able to get it via Revisions.Items[1] or, if C# doesn't like Items for some reason, see if there isn't a .get_Items method?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 2, 2013 1:15 PM
    Moderator
  • Hi Ankur

    <<I make the changes, save method is giving me some errorcross some articles where it is stated that use OPEN XML SDK for accessing the MS Office documents >>

    Are you programming in a server environment, then?

    >>So can you provide me a useful link so that I can start of with Open XML sdk.<<

    OpenXMLDeveloper.org is a good place to start with Office Open XML.

    There's also documentation for the SDK, including some "How to" examples:
      http://msdn.microsoft.com/en-us/library/bb448854.aspx

    And finally there's the Open XML SDK forum:
      http://social.msdn.microsoft.com/forums/en-US/oxmlsdk/threads/

    I definitely recommend you use version 2.5 unless you absolutely must work with a pre-.NET Framework 4.0. It can be downloaded here:
      http://www.microsoft.com/en-us/download/details.aspx?id=30425


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 2, 2013 1:22 PM
    Moderator
  • Hi Ankur,

    I have been trying to do the same thing it sounds like you were doing in your original post.  I am comparing batches of Word documents generated with different versions of our program.  If there are differnces, then I save the delta document and want that document to be opened via the GUI and have the Reviewing Pane open when the delta document is opened.  The problem with doing it easily is that the Reviewing Pane is not a member of the TaskPanes enumeration. But, drilling down through the CommandBars collection, I was able to find the 'Reviewing Pane' CommandBarControl and call its 'Execute()' method which opens the pane along with the document.  Here is how I accessed it ... you may need to change the references to match your declarations ...

           doc.CommandBars["Reviewing"].Controls["Reviewing Pane"].Execute();

    Hope that helps.

    -Tom Schoeneweis

    • Proposed as answer by burghboy Thursday, January 3, 2013 5:45 PM
    • Unproposed as answer by burghboy Friday, January 4, 2013 7:51 AM
    Thursday, January 3, 2013 5:44 PM
  • Hi burghboy 

    " doc.CommandBars["Reviewing"].Controls["Reviewing Pane"].Execute(); ".. how do I go about this line.? From here can I get the user who has modified the file..??

    Regards,

    Ankur

    Friday, January 4, 2013 7:22 AM
  • Ankur,

    I am sorry.  I misread the original post.  I thought you were wanting to open the reviwing Pane programmatically which is something that I was trying to do.  The line of code that I gave you will cause the Reviewing Pane to open after you have opened the document.  The way to get to the Author property of the revision is as Cindy said except that C# does not use Items.  You need to type the line of code like this ...

        string userName = document.Revisions[x].Author

    Where x is the index of the Revision you want to inspect.  And remember that the indexing of all of the collections of objects exposed in the interop assembly is 1-based and not 0-based as is the norm for C#.

    -Tom


    Tom Schoeneweis Burghboy on loan to Utah ***

    Friday, January 4, 2013 7:50 AM
  • burghboy

    Thanks a lot man. Now 1 question. [x] I dont have loop. so the x can have what kind of values.? x is file name of what.? Please help

    Regards,

    Ankur

    Friday, January 4, 2013 8:09 AM
  • Hi Ankur,

    'x' is the index of the revision that you want to inspect.  It will be between 1 and the number of revisions in the Document object or the Range object you are looking at.  if you wanted to loop through the revisions though you can use foreach

    foreach (Revision rev in doc.Revisions)
    {
        string editedBy = rev.Author;
        // do whatever you need to with the revision author's name
    }
    -Tom

    Tom Schoeneweis

    Friday, January 4, 2013 8:20 AM
  • Hi Burghboy

    here is my code that I am trying to execute

    Document tempDoc = app.Documents.Open("E:\\ankur.docx");

    int cnt = tempDoc.Revisions.Count;

    string author = tempDoc.Revisions[cnt].Author;

    tempDoc.Close(ref Nothing, ref format, ref Nothing);

    I am getting an error when the control reaches the 3rd line string author Error is "  The requested member of the collection does not exist."

    Friday, January 4, 2013 8:29 AM
  • Hi Ankur,

    Remember that the indexing on these collections is 1-based, so you should make sure that the Count  > 0 before trying to access the Revision ... like this:

    int cnt = tempDoc.Revisions.Count;
    
    if (cnt > 0)
    {
        string author = tempDoc.Revisions[cnt].Author;
    }

    That way you will not try to access something that does not exist.  The first Revision will be Revisions[1], not Revisions[0].

    -Tom


    Tom Schoeneweis

    Friday, January 4, 2013 8:38 AM
  • burghboy

    You are My superman :-) . It worked. I got what i wanted to achieve. Awesome. Now I can get the name of the user who modified it. If you dont mind I have some more questions to trouble you with. If you allow me to ask.

    Now as I have got the name of the person who changed it. Can i get the changes that the user made.

    Regards,

    Ankur



    Friday, January 4, 2013 8:44 AM
  • Hi Ankur,

    You could loop through the Revisions with a foreach and check if the author was the one you want or you could use a LINQ expression like this:

    if (doc.Revisions.Count > 0)
    {
        List<Revision> revs = doc.Revisions.Cast<Revision>().Where(r => r.Author == doc.Revisions[doc.Revisions.Count].Author).ToList();
    }

    This will give you a generic list collection of Revisions that were made by the author of the latest revision in the document.

    I am heading to bed but I'll look at the thread later to see if that helped.

    -Tom


    Tom Schoeneweis

    Friday, January 4, 2013 9:08 AM
  • burghboy

    Sure Sir, Will post my questions to the thread. Thanks a lot again.

    1) How do I store the text of the MS word document in the database and retrive back in MS Word with the trackchanges. I have managed to keep a track on the document by the following code.

    tempDoc.TrackRevisions = true;

    But will I be able to retrive these changes when I am fetching the vtext back from the database?

    2) How do I get what changes the user has made.?

    Regards,

    Ankur


    Friday, January 4, 2013 9:11 AM
  • Hi Ankur,

    The following CodeProject post looks like it addresses what you want.  It will save the entire file (which should include the revision information, etc). Solution 1 tells how to create the field you want to store the info in and use File class to read the file to a byte[] and then store it.

    http://www.codeproject.com/Questions/343921/how-to-store-word-file-as-binary-data-into-sql-ser

    -Tom


    Tom Schoeneweis

    Friday, January 4, 2013 6:44 PM
  • burghboy

    Thanks Sir, Just 1 question remains. How do I open the file in write mode. I mean I dont want to download the file on my System. I just want to open the file from database and when I do CTRL+S or hit the save button the modified file with revisions should be saved in database as it is.

    Regards,

    Ankur

    Saturday, January 5, 2013 4:19 AM
  • Hi Ankur,

    I've never personally stored filed in a database, but here is an article from codeProject that looks interesting.  The author compares several methods and explains which he feels is better with some evidence.  Have a look at it ...  I think it looks pretty interesting.

    http://www.codeproject.com/Articles/32216/How-to-store-and-fetch-binary-data-into-a-file-str

    -Tom


    Tom Schoeneweis

    Monday, January 7, 2013 5:55 AM
  • Hi burghBoy..

    Will surely see the article. Can you suggest any links for a tree view control that works like MS Publisher. Like when I click a folder in a tree view it gives me the files and folders inside that folder or when file is reached , I can open the file.

    Regards,

    Ankur

    Monday, January 7, 2013 6:52 AM