-1

I know there are several docx creation/modification approaches in C#

OpenXML, DocX API, etc.

I've figured out how to enable tracked changes using Open XML, just haven't figured out to lock the tracked changes (a feature in word 2013).

Pinch
  • 4,009
  • 8
  • 40
  • 60

2 Answers2

1

This code works fine for me:

static void Main(string[] args)
{
    using (var document = WordprocessingDocument.Open(@"D:\DocTest\Test1.docx", true))
    {
        AddTrackingLock(document);
    }
}

private static void AddTrackingLock(WordprocessingDocument document)
{
    var documentSettings = document.MainDocumentPart.DocumentSettingsPart;
    var documentProtection = documentSettings
                                .Settings
                                .FirstOrDefault(it =>
                                        it is DocumentProtection &&
                                        (it as DocumentProtection).Edit == DocumentProtectionValues.TrackedChanges)
                                as DocumentProtection;

    if (documentProtection == null)
    {
        var documentProtectionElement = new DocumentProtection();
        documentProtectionElement.Edit = DocumentProtectionValues.TrackedChanges;
        documentProtectionElement.Enforcement = OnOffValue.FromBoolean(true);
        documentSettings.Settings.AppendChild(documentProtectionElement);
    }
    else
    {
        documentProtection.Enforcement = OnOffValue.FromBoolean(true);
    }
}
Nick
  • 798
  • 1
  • 7
  • 14
  • thanks alot for your answer but that code doesn't enforce locking with a password. – Pinch Jul 14 '14 at 17:45
  • 1
    In the DocumentProtection, modify the `Hash` property, it is the `Password Hash.Represents the attribte in schema: w:hash` (from MSDN documentation: http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.documentprotection_members%28v=office.14%29.aspx) – Nicolas R Jul 21 '14 at 12:57
1

Have a look at How to set the editing restrictions in Word using Open XML SDK 2.0

I don't have any working code for you, but the password hash needs to be stored in DocumentProtection.Hash. Hope that this hint helps moving you along!

EFrank
  • 1,880
  • 1
  • 25
  • 33