5

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?

Ahmad Khan
  • 2,655
  • 19
  • 25

1 Answers1

1

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";

        }
    }
}
Ahmad Khan
  • 2,655
  • 19
  • 25
Taterhead
  • 5,763
  • 4
  • 31
  • 40
  • I wasn't changing the zoom level using any code. I was manually editing the xml of docx file. And it saved correctly, I double checked it. Let me try this code too to see whether it changes the zoom level now or not. – Ahmad Khan Nov 15 '17 at 11:53
  • Your code is just changing the zoom percent to `120%`, I've seen the changed XML files too, its changed to `120%` in `settings.xml`, but word is still showing it in `100%` zoom. – Ahmad Khan Nov 15 '17 at 12:31
  • And one more thing, if `RevisionView` doesn't do what I was expecting from it, what does it actually do? – Ahmad Khan Nov 15 '17 at 12:32
  • re: zoom not working. Try starting with a fresh new document. Then set the document zoom to 80% and save. Run the console app on this document. Then re-open to see if the zoom changes to 120%. Your original document might be locked down. – Taterhead Nov 15 '17 at 12:52
  • The zoom value in settings.xml changes to 120%, but when opening with word, it still shows in 80%. – Ahmad Khan Nov 15 '17 at 13:24
  • Ok it seems that you cannot preserve Word zoom settings in the document using OpenXML. Only with PowerPoint and Excel. Please see https://support.office.com/en-us/article/Zoom-in-or-out-of-a-document-presentation-or-worksheet-0a0ebbed-10e7-444b-b16b-6f0c090f8ec7 . Word preserves the zoom settings from the last time you ran word and ignores the settings.xml . I will update the answer and leave the code for reference on how to change zoom with OpenXML. – Taterhead Nov 15 '17 at 15:09
  • 1
    Thanks. But still my main concern was showing the Reviewing Pane by default. Anyways thanks for your help :) – Ahmad Khan Nov 16 '17 at 06:07
  • Yes, this question needs to be split in two. Both questions (reviewing pane and zoom settings for word) are good and need a separate so post to separate the concerns. At least change the title to one regarding the reviewing pane. Right now it is misleading. – Taterhead Nov 16 '17 at 06:12