Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Reviewing Pane in Word Client by default

What I want: I'm editing a WordprocessingDocument, and adding some tracked changes in it. This part is done. Now, I want MS word to show all revisions by default, i.e., it shouldn't require user to click on the red side bar to open the tracked changes in the document.

red side bar

What I did: For this, I found a class RevisionView, which adds the xml element <w:revisionView /> in settings.xml under w:settings element. The RevisionView has some properties like Comments, DisplayRevision, Formatting etc. I explicitly set them all to true.

RevisionView revView = new RevisionView();
revView.DisplayRevision = new OnOffValue(true);
revView.Formatting = new OnOffValue(true);
revView.InkAnnotations = new OnOffValue(true);
revView.Markup = new OnOffValue(true);
revView.Comments = new OnOffValue(true);

and then I added this revView to the Settings:

Settings settings = wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings;
settings.RemoveAllChildren<RevisionView>();
settings.AppendChild(revView);
settings.Save();

And then I reviewed the document xml explicitly, it is adding the following xml in the settings:

<w:revisionView w:markup="true" w:comments="true" w:insDel="true" w:formatting="true" w:inkAnnotations="true" />

But adding this element in settings doesn't affect the view. It isn't showing the revisions opened by default.

Then, for testing purpose, I changed the zoom element in the settings.xml by hand from <w:zoom w:percent="100" /> to <w:zoom w:percent="120" />. What I expected was: word would change the zoom for this document from 100 to 120 now. But it didn't do that, the zoom was 100 even after changing to 120 in settings.xml.

One more thing: I can't use interop as I have to deploy this to a server, that's why I'm doing all this using OpenXmlSdk.

What I'm asking:

  • Is it even possible to do what i want?

  • If it is, then what am I doing wrong? Is RevisionView the option, on what should I rely?

  • Is there a way to force word to apply (override the default application level settings) the settings provided in settings.xml?

  • Why isn't word changing the zoom from 100 to 120?

like image 827
Ahmad Khan Avatar asked Nov 07 '22 14:11

Ahmad Khan


1 Answers

Here are the answers to your questions:

Is it even possible to do what i want?

What you are trying to do is: Upon opening the docx file, have the Reviewing Pane be open automatically. I could not find a way to force the Word client to do this with OpenXml.

If it is, then what am I doing wrong? Is RevisionView the option, on what should I rely?

Its not possible, so the answer here is also no.

Is there a way to force word to apply (override the default application level settings) the settings provided in settings.xml?

Yes using the OpenXml SDK. The Settings Part has properties that you can control with code to alter the default Word client behavior.

Why isn't word changing the zoom from 100 to 120?

I can't answer this without seeing your file. Perhaps you did not save the file back correctly when you edited the file manually.

I was able to build a simple console app with the following code. The app will change the zoom level for any Word file to 120%. You need to add your file path to the code.

I generated most of this code using the OpenXml Productivity Tool..

Note - When building this in Visual Studio, don't forget to add DocumentFormat.OpenXml and WindowsBase to your project references.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ConsoleApp4
{
    class Program
    {

        private static WordprocessingDocument document;
        private static System.Collections.Generic.IDictionary<System.String, OpenXmlPart> UriPartDictionary = new System.Collections.Generic.Dictionary<System.String, OpenXmlPart>();
        private static System.Collections.Generic.IDictionary<System.String, DataPart> UriNewDataPartDictionary = new System.Collections.Generic.Dictionary<System.String, DataPart>();


        static void Main(string[] args)
        {

            using (document = WordprocessingDocument.Open("<DOCX FILE PATH HERE>", true))
            {
                BuildUriPartDictionary();
                ChangeParts();
            }

        }

        private static void BuildUriPartDictionary()
        {
            System.Collections.Generic.Queue<OpenXmlPartContainer> queue = new System.Collections.Generic.Queue<OpenXmlPartContainer>();
            queue.Enqueue(document);
            while (queue.Count > 0)
            {
                foreach (var part in queue.Dequeue().Parts)
                {
                    if (!UriPartDictionary.Keys.Contains(part.OpenXmlPart.Uri.ToString()))
                    {
                        UriPartDictionary.Add(part.OpenXmlPart.Uri.ToString(), part.OpenXmlPart);
                        queue.Enqueue(part.OpenXmlPart);
                    }
                }
            }
        }

        private static void ChangeParts()
        {
            ChangeDocumentSettingsPart1(((DocumentSettingsPart)UriPartDictionary["/word/settings.xml"]));
        }

        private static void ChangeDocumentSettingsPart1(DocumentSettingsPart documentSettingsPart)
        {
            Settings settings1 = documentSettingsPart.Settings;

            Zoom zoom1 = settings1.GetFirstChild<Zoom>();

            zoom1.Percent = "120";

        }
    }
}
like image 193
Taterhead Avatar answered Nov 14 '22 22:11

Taterhead