![](/uploads/1/2/5/7/125745164/389870738.jpg)
-->
This topic shows how to use the Open XML SDK 2.5 for Office to accept all revisions in a word processing document programmatically.
The following assembly directives are required to compile the code in this topic.
Open the Existing Document for Editing
Accept multiple changes in MS Word. This will accept all changes in the selected area. How to disable tracking of formatting changes in Word for Mac 2011? On the Home tab, in the Styles group, click the Styles Dialog Box Launcher. In the Styles task pane, click Manage Styles, and then click the Restrict tab. In the Manage Styles dialog box, select the sort order that you want to use to view styles.
To open an existing document, you can instantiate the WordprocessingDocument class as shown in the following using statement. To do so, you open the word processing file with the specified fileName by using the Open(String, Boolean) method, with the Boolean parameter set to true in order to enable editing the document.
The using statement provides a recommended alternative to the typical .Open, .Save, .Close sequence. It ensures that the Dispose method (internal method used by the Open XML SDK to clean up resources) is automatically called when the closing brace is reached. The block that follows the using statement establishes a scope for the object that is created or named in the using statement, in this case wdDoc. Because the WordprocessingDocument class in the Open XML SDK automatically saves and closes the object as part of its System.IDisposable implementation, and because Dispose is automatically called when you exit the block, you do not have to explicitly call Save and Close as long as you use using.
Structure of a WordProcessingML Document
The basic document structure of a WordProcessingML document consists of the document and body elements, followed by one or more block level elements such as p, which represents a paragraph. A paragraph contains one or more r elements. The r stands for run, which is a region of text with a common set of properties, such as formatting. A run contains one or more t elements. The t element contains a range of text. The following code example shows the WordprocessingML markup for a document that contains the text 'Example text.'
Using the Open XML SDK 2.5, you can create document structure and content using strongly-typed classes that correspond to WordprocessingML elements. You will find these classes in the DocumentFormat.OpenXml.Wordprocessing namespace. The following table lists the class names of the classes that correspond to the document, body, p, r, and t elements.
WordprocessingML Element | Open XML SDK 2.5 Class | Description |
---|---|---|
document | Document | The root element for the main document part. |
body | Body | The container for the block level structures such as paragraphs, tables, annotations and others specified in the ISO/IEC 29500 specification. |
p | Paragraph | A paragraph. |
r | Run | A run. |
t | Text | A range of text. |
ParagraphPropertiesChange Element
When you accept a revision mark, you change the properties of a paragraph either by deleting an existing text or inserting a new text. In the following sections, you read about three elements that are used in the code to change the paragraph contents, mainly,
<w: pPrChange>
(Revision Information for Paragraph Properties), <w:del>
(Deleted Paragraph), and <w:ins>
(Inserted Table Row) elements.The following information from the ISO/IEC 29500 specification introduces the ParagraphPropertiesChange element (pPrChange).
pPrChange (Revision Information for Paragraph Properties)
This element specifies the details about a single revision to a set of paragraph properties in a WordprocessingML document.
This element stores this revision as follows:
- The child element of this element contains the complete set of paragraph properties which were applied to this paragraph before this revision.
- The attributes of this element contain information about when this revision took place (in other words, when these properties became a 'former' set of paragraph properties).
Consider a paragraph in a WordprocessingML document which is centered, and this change in the paragraph properties is tracked as a revision. This revision would be specified using the following WordprocessingML markup.
The element specifies that there was a revision to the paragraph properties at 01-01-2006 by Samantha Smith, and the previous set of paragraph properties on the paragraph was the null set (in other words, no paragraph properties explicitly present under the element). pPrpPrChange
© ISO/IEC29500: 2008.
Deleted Element
The following information from the ISO/IEC 29500 specificationintroduces the Deleted element (del).
del (Deleted Paragraph)
This element specifies that the paragraph mark delimiting the end of a paragraph within a WordprocessingML document shall be treated as deleted (in other words, the contents of this paragraph are no longer delimited by this paragraph mark, and are combined with the following paragraph—but those contents shall not automatically be marked as deleted) as part of a tracked revision.
Consider a document consisting of two paragraphs (with each paragraph delimited by a pilcrow ¶):
If the physical character delimiting the end of the first paragraph is deleted and this change is tracked as a revision, the following will result:
This revision is represented using the following WordprocessingML:
The del element on the run properties forthe first paragraph mark specifies that this paragraph mark was deleted,and this deletion was tracked as a revision.
© ISO/IEC29500: 2008.
The Inserted Element
The following information from the ISO/IEC 29500 specificationintroduces the Inserted element (ins).
ins (Inserted Table Row)
This element specifies that the parent table row shall be treated as aninserted row whose insertion has been tracked as a revision. Thissetting shall not imply any revision state about the table cells in thisrow or their contents (which must be revision marked independently), andshall only affect the table row itself.
![Word Word](/uploads/1/2/5/7/125745164/865160145.png)
Consider a two row by two column table in which the second row has beenmarked as inserted using a revision. This requirement would be specifiedusing the following WordprocessingML:
The ins element on the table row properties for the second table rowspecifies that this row was inserted, and this insertion was tracked asa revision.
© ISO/IEC29500: 2008.
How the Sample Code Works
After you have opened the document in the using statement, youinstantiate the Body class, and then handlethe formatting changes by creating the changesList, and removing each change (the w:pPrChange element) from the List, which is the same as accepting changes.
You then handle the deletions by constructing the deletionsList, and removing each deletion element (w:del) from the List, which is similar to the process ofaccepting deletion changes.
Finally, you handle the insertions by constructing the insertionsList and inserting the new text by removing theinsertion element (w:ins), which is thesame as accepting the inserted text.
Sample Code
The following code example shows how to accept the entire revisions in aword processing document. To run the program, you can call the methodAcceptRevisions to accept revisions in thefile 'word1.docx' as in the following example.
After you have run the program, open the word processing file to makesure that all revision marks have been accepted.
The following is the complete sample code in both C# and Visual Basic.
See also
In this article, we want to share with you 3 ways to accept specific tracked changes only in your Word document.
Normally, in a document with tracked changes, you can accept them all at once or each one by one. But sometimes, you may need to accept only specific changes, such as all formatting changes. To address this, it requires extra steps.
Following are 3 approaches to accept specific tracked changes only in different cases.
Method 1: Accept All Tracked Changes of Specific Type
Here, let’s say we need to accept all formatting changes in the document.
- Firstly, click “Review” tab.
- Then click “Show Markup” command.
- Next check only “Formatting” box on the drop-down menu.
- Now you can see only formatting changes are shown in the document. Click on the drop-down button on “Accept” command.
- And select “Accept All Changes Shown” option.
Likewise, to accept other changes, just show only that type of changes in the document and accept them.
Method 2: Accept Only Deletions or Insertions
As you can see from the first screenshot, insertions and deletions are bound together. Therefore, to accept one of them, you need a macro.
- First of all, press “Alt+ F11” to trigger VBA editor in Word.
- Next click “Normal” project and click “Insert” on menu bar.
- And select “Module” on that menu to insert a new one.
- Then double click on module to open it and paste following codes:
- Finally, click “Run” button or just hit “F5” to execute codes.
This macro accepts all deletions in the document. As to accept all insertions, you simply replace with this macro:
Method 3: Accept All Changes in a Selection
This method shows you how to quickly accept all changes in different types in a selection.
- To start with, make a selection over target range.
- Then click “Review” tab in the Ribbon.
- Next click on drop-down button on “Accept” command.
- On the drop-down menu, select “Accept Change”. And that’s it.
Data Loss Penetrating Everywhere
Going with the tide of digitization, the probability of getting corrupt doc is increasing. Considering the value of your data, it’s highly recommended to get hold of a sufficient repairing tool. So you can recover documents with the least time.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix xls and pdf repair software products. For more information visit www.datanumen.com
![](/uploads/1/2/5/7/125745164/389870738.jpg)