21
Views
4
Comments
Solved
Change the Settings of a Word Document
Question

This is perhaps more a web development question than an OutSystems specific question, but I am hoping that perhaps there is an existing Forge component that can do what I need.

I am using Hidden text fields in MS Word so that some users don't see certain fields. However, for some users I want everything to show. I don't want the users who must see the text to have to manually go and change the setting to show all hidden fields, I want this to be done for them.

I see it is possible through VBA/C# etc but I don't want code to run in the Word document itself via a button or something like that, I want it to run ideally before the document is downloaded so they get it with everything visible. So I need a script to run in OutSystems before the download.

This is the setting that needs to be set to true

There are quite a number of MS Word Forge Components but I haven't seen one that can edit the settings of a document. I am thinking this probably needs to be an extension created in Integration Studio using C#?

Can anyone assist with this?

Solution

Thanks Stefan.
I found the same through some research and came up with the following code which does the trick. Hope this can help someone else too.


using System;

using System.IO;

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;


class Program

{

    public static void MssUnhideAllFields(byte[] ssBinary_in, out byte[] ssBinary_out)

    {

        using (MemoryStream memoryStream = new MemoryStream(ssBinary_in))

        {

            using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))

            {

                var mainPart = doc.MainDocumentPart;

                var docBody = mainPart.Document.Body;


                foreach (var run in docBody.Descendants())

                {

                    var runProperties = run.RunProperties;

                    if (runProperties == null)

                        continue;


                    var vanishElement = runProperties.Vanish;

                    if (vanishElement != null)

                    {

                        // Remove the vanish element

                        runProperties.RemoveChild(vanishElement);

                    }

                }


                // Save the changes

                mainPart.Document.Save();

            }


            // Get the modified document's content as a byte array

            ssBinary_out = memoryStream.ToArray();

        }

    }


    public static void Main(string[] args)

    {

        byte[] inputBinary = /* your binary data here */ ; // Replace with your binary document data

        byte[] outputBinary;


        MssUnhideAllFields(inputBinary, out outputBinary);


        // Now you can use the outputBinary for further processing or output

    }

}

Solution

Yes. Using the OpenXML SDK it is possible to access the fields. You would need to iterate over all paragraphs then all runs within a paragraph and then all elements within a run to get to the fields. (<- as far as my memory is right here). Although i have lots of openxml code i dont have a concrete example to share with you - never had the requirement to deal with fields within a word document :-(

Hi Nicholas,

the setting "Hidden text" is afaik not a document level setting but rather an user settings that is stored in the user profile. That means that there is no way to manipulate the openxml settings part to include this setting. Here is a list of all document level settings https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.settings?view=openxml-2.8.1

One option you could choose (if within an organization) to apply a Group Policy to a specific set of users that includes this option.

Best

Stefan

Hi Stefan

Thanks so much for this information. I don't think the policy option will be possible in my scenario.


Perhaps I need to automatically set all of the hidden fields to be no longer hidden rather than changing the setting. I assume this would be possible via C# extension?

Solution

Yes. Using the OpenXML SDK it is possible to access the fields. You would need to iterate over all paragraphs then all runs within a paragraph and then all elements within a run to get to the fields. (<- as far as my memory is right here). Although i have lots of openxml code i dont have a concrete example to share with you - never had the requirement to deal with fields within a word document :-(

Solution

Thanks Stefan.
I found the same through some research and came up with the following code which does the trick. Hope this can help someone else too.


using System;

using System.IO;

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;


class Program

{

    public static void MssUnhideAllFields(byte[] ssBinary_in, out byte[] ssBinary_out)

    {

        using (MemoryStream memoryStream = new MemoryStream(ssBinary_in))

        {

            using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))

            {

                var mainPart = doc.MainDocumentPart;

                var docBody = mainPart.Document.Body;


                foreach (var run in docBody.Descendants())

                {

                    var runProperties = run.RunProperties;

                    if (runProperties == null)

                        continue;


                    var vanishElement = runProperties.Vanish;

                    if (vanishElement != null)

                    {

                        // Remove the vanish element

                        runProperties.RemoveChild(vanishElement);

                    }

                }


                // Save the changes

                mainPart.Document.Save();

            }


            // Get the modified document's content as a byte array

            ssBinary_out = memoryStream.ToArray();

        }

    }


    public static void Main(string[] args)

    {

        byte[] inputBinary = /* your binary data here */ ; // Replace with your binary document data

        byte[] outputBinary;


        MssUnhideAllFields(inputBinary, out outputBinary);


        // Now you can use the outputBinary for further processing or output

    }

}

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.