diff --git a/docs/general/how-to-add-a-new-document-part-to-a-package.md b/docs/general/how-to-add-a-new-document-part-to-a-package.md index bc5ba2d2..90d9714d 100644 --- a/docs/general/how-to-add-a-new-document-part-to-a-package.md +++ b/docs/general/how-to-add-a-new-document-part-to-a-package.md @@ -40,8 +40,7 @@ The code starts with opening a package file by passing a file name to one of the *** -The **using** statement provides a recommended alternative to the typical .Create, .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 **wordDoc**. Because the class in the Open XML SDK -automatically saves and closes the object as part of its **System.IDisposable** implementation, and because the **Dispose** method is automatically called when you exit the block; you do not have to explicitly call **Save** and **Close**, as long as you use **using**. +[!include[Using Statement](../includes/using-statement.md)] [!include[Structure](../includes/word/structure.md)] diff --git a/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md b/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md index 5d9bf19f..11d86267 100644 --- a/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md +++ b/docs/general/how-to-copy-the-contents-of-an-open-xml-package-part-to-a-document-part-in-a-dif.md @@ -33,7 +33,7 @@ programmatically. To open an existing document, instantiate the class as shown in the following two **using** statements. In the same statement, you open the word processing file with the specified -file name by using the [Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method, with the Boolean parameter. +file name by using the method, with the Boolean parameter. For the source file that set the parameter to **false** to open it for read-only access. For the target file, set the parameter to **true** in order to enable editing the document. @@ -57,16 +57,7 @@ order to enable editing the document. ``` *** -The `using` statement provides a recommended -alternative to the typical .Create, .Save, .Close sequence. It ensures -that the 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. Because the class in the Open XML SDK -automatically saves and closes the object as part of its implementation, and because - is automatically called when you -exit the block, you do not have to explicitly call . +[!include[Using Statement](../includes/using-statement.md)] -------------------------------------------------------------------------------- diff --git a/docs/general/how-to-create-a-package.md b/docs/general/how-to-create-a-package.md index c40dea47..a7946d10 100644 --- a/docs/general/how-to-create-a-package.md +++ b/docs/general/how-to-create-a-package.md @@ -59,15 +59,7 @@ template. ``` *** -The **using** statement provides a recommended -alternative to the typical .Create, .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 bracket 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 **wordDoc**. Because the 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 bracketed block, you do not have to explicitly call **Save** and **Close**─as -long as you use **using**. +[!include[Using Statement](../includes/using-statement.md)] Once you have created the Word document package, you can add parts to it. To add the main document part you call . Having done that, diff --git a/docs/general/how-to-get-the-contents-of-a-document-part-from-a-package.md b/docs/general/how-to-get-the-contents-of-a-document-part-from-a-package.md index 8f2a83bc..1dc5c19a 100644 --- a/docs/general/how-to-get-the-contents-of-a-document-part-from-a-package.md +++ b/docs/general/how-to-get-the-contents-of-a-document-part-from-a-package.md @@ -54,17 +54,7 @@ opened in read-only mode to avoid accidental changes. ``` *** - -The **using** statement provides a recommended -alternative to the typical .Create, .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 **wordDoc**. Because the class in the Open XML SDK -automatically saves and closes the object as part of its **System.IDisposable** implementation, and because -the **Dispose** method is automatically called -when you exit the block; you do not have to explicitly call **Save** and **Close**─as -long as you use using. +[!include[Using Statement](../includes/using-statement.md)] --------------------------------------------------------------------------------- diff --git a/docs/general/how-to-remove-a-document-part-from-a-package.md b/docs/general/how-to-remove-a-document-part-from-a-package.md index fcfacedf..128cd48a 100644 --- a/docs/general/how-to-remove-a-document-part-from-a-package.md +++ b/docs/general/how-to-remove-a-document-part-from-a-package.md @@ -54,16 +54,7 @@ should be opened in read/write mode. *** -The **using** statement provides a recommended -alternative to the typical .Create, .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 **wordDoc**. Because the class in the Open XML SDK -automatically saves and closes the object as part of its **System.IDisposable** implementation, and because -the **Dispose** method is automatically called -when you exit the block; you do not have to explicitly call **Save** and **Close**─as -long as you use **using**. +[!include[Using Statement](../includes/using-statement.md)] --------------------------------------------------------------------------------- diff --git a/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md b/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md index a8648b65..41d38e3b 100644 --- a/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md +++ b/docs/general/how-to-replace-the-theme-part-in-a-word-processing-document.md @@ -29,7 +29,7 @@ In the sample code, you start by opening the word processing file by instantiating the class as shown in the following **using** statement. In the same statement, you open the word processing file *document* by using the -[Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method, with the Boolean parameter set + method, with the Boolean parameter set to **true** to enable editing the document. ### [C#](#tab/cs-0) @@ -49,19 +49,7 @@ to **true** 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 *wordDoc*. Because -the 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**. +[!include[Using Statement](../includes/using-statement.md)] ## How to Change Theme in a Word Package diff --git a/docs/general/how-to-search-and-replace-text-in-a-document-part.md b/docs/general/how-to-search-and-replace-text-in-a-document-part.md index 9a73ba0a..d970c5fc 100644 --- a/docs/general/how-to-search-and-replace-text-in-a-document-part.md +++ b/docs/general/how-to-search-and-replace-text-in-a-document-part.md @@ -32,7 +32,7 @@ In the sample code, you start by opening the word processing file by instantiating the **** class as shown in the following **using** statement. In the same statement, you open the word processing file *document* by using the -**[Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open)** method, with the Boolean parameter set +**** method, with the Boolean parameter set to **true** to enable editing the document. ### [C#](#tab/cs-0) @@ -53,18 +53,7 @@ to **true** 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 *wordDoc*. Because -the 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**. +[!include[Using Statement](../includes/using-statement.md)] -------------------------------------------------------------------------------- diff --git a/docs/includes/using-statement.md b/docs/includes/using-statement.md new file mode 100644 index 00000000..77938662 --- /dev/null +++ b/docs/includes/using-statement.md @@ -0,0 +1,11 @@ +The `using` statement provides a recommended +alternative to the typical .Create, .Save, .Close sequence. It ensures +that the 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. Because the class in the Open XML SDK +automatically saves and closes the object as part of its implementation, and because + is automatically called when you +exit the block, you do not have to explicitly call and + as long as you use `using`. \ No newline at end of file diff --git a/docs/presentation/how-to-apply-a-theme-to-a-presentation.md b/docs/presentation/how-to-apply-a-theme-to-a-presentation.md index 2182b0ad..4c37d68e 100644 --- a/docs/presentation/how-to-apply-a-theme-to-a-presentation.md +++ b/docs/presentation/how-to-apply-a-theme-to-a-presentation.md @@ -51,12 +51,7 @@ represents the path for the target presentation 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 **themeDocument** and **presentationDocument**. +[!include[Using Statement](../includes/using-statement.md)] ----------------------------------------------------------------------------- diff --git a/docs/presentation/how-to-change-the-fill-color-of-a-shape-in-a-presentation.md b/docs/presentation/how-to-change-the-fill-color-of-a-shape-in-a-presentation.md index 8d0cb092..6939c994 100644 --- a/docs/presentation/how-to-change-the-fill-color-of-a-shape-in-a-presentation.md +++ b/docs/presentation/how-to-change-the-fill-color-of-a-shape-in-a-presentation.md @@ -53,12 +53,7 @@ you want to open 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 *ppt*. +[!include[Using Statement](../includes/using-statement.md)] ## The Structure of the Shape Tree diff --git a/docs/presentation/how-to-delete-a-slide-from-a-presentation.md b/docs/presentation/how-to-delete-a-slide-from-a-presentation.md index f27d74ca..5e679caa 100644 --- a/docs/presentation/how-to-delete-a-slide-from-a-presentation.md +++ b/docs/presentation/how-to-delete-a-slide-from-a-presentation.md @@ -76,7 +76,7 @@ statement. *** -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 **presentationDocument**. +[!include[Using Statement](../includes/using-statement.md)] [!include[Structure](../includes/presentation/structure.md)] diff --git a/docs/presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md b/docs/presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md index 83f73734..2600ecd0 100644 --- a/docs/presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md +++ b/docs/presentation/how-to-delete-all-the-comments-by-an-author-from-all-the-slides-in-a-presentatio.md @@ -59,12 +59,7 @@ Options. *** -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 *doc*. +[!include[Using Statement](../includes/using-statement.md)] [!include[Structure](../includes/presentation/structure.md)] diff --git a/docs/presentation/how-to-get-all-the-external-hyperlinks-in-a-presentation.md b/docs/presentation/how-to-get-all-the-external-hyperlinks-in-a-presentation.md index d7ba13a1..d69df315 100644 --- a/docs/presentation/how-to-get-all-the-external-hyperlinks-in-a-presentation.md +++ b/docs/presentation/how-to-get-all-the-external-hyperlinks-in-a-presentation.md @@ -58,12 +58,7 @@ path for the file from which you want to open 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 **document**. +[!include[Using Statement](../includes/using-statement.md)] -------------------------------------------------------------------------------- diff --git a/docs/presentation/how-to-get-all-the-text-in-a-slide-in-a-presentation.md b/docs/presentation/how-to-get-all-the-text-in-a-slide-in-a-presentation.md index 083855d4..56c0af4f 100644 --- a/docs/presentation/how-to-get-all-the-text-in-a-slide-in-a-presentation.md +++ b/docs/presentation/how-to-get-all-the-text-in-a-slide-in-a-presentation.md @@ -56,12 +56,7 @@ 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 **presentationDocument**. +[!include[Using Statement](../includes/using-statement.md)] -------------------------------------------------------------------------------- diff --git a/docs/presentation/how-to-get-all-the-text-in-all-slides-in-a-presentation.md b/docs/presentation/how-to-get-all-the-text-in-all-slides-in-a-presentation.md index 1c03a39c..96ffdbf2 100644 --- a/docs/presentation/how-to-get-all-the-text-in-all-slides-in-a-presentation.md +++ b/docs/presentation/how-to-get-all-the-text-in-all-slides-in-a-presentation.md @@ -56,12 +56,7 @@ 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 **presentationDocument**. +[!include[Using Statement](../includes/using-statement.md)] -------------------------------------------------------------------------------- diff --git a/docs/presentation/how-to-get-the-titles-of-all-the-slides-in-a-presentation.md b/docs/presentation/how-to-get-the-titles-of-all-the-slides-in-a-presentation.md index f06c1b30..44d82d82 100644 --- a/docs/presentation/how-to-get-the-titles-of-all-the-slides-in-a-presentation.md +++ b/docs/presentation/how-to-get-the-titles-of-all-the-slides-in-a-presentation.md @@ -55,14 +55,7 @@ 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 -*presentationDocument*. +[!include[Using Statement](../includes/using-statement.md)] [!include[Structure](../includes/presentation/structure.md)] diff --git a/docs/presentation/how-to-insert-a-new-slide-into-a-presentation.md b/docs/presentation/how-to-insert-a-new-slide-into-a-presentation.md index 1c0c65f2..0ed94c26 100644 --- a/docs/presentation/how-to-insert-a-new-slide-into-a-presentation.md +++ b/docs/presentation/how-to-insert-a-new-slide-into-a-presentation.md @@ -51,14 +51,7 @@ 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 -*presentationDocument*. +[!include[Using Statement](../includes/using-statement.md)] [!include[Structure](../includes/presentation/structure.md)] diff --git a/docs/presentation/how-to-move-a-paragraph-from-one-presentation-to-another.md b/docs/presentation/how-to-move-a-paragraph-from-one-presentation-to-another.md index e800cebe..6e5103dd 100644 --- a/docs/presentation/how-to-move-a-paragraph-from-one-presentation-to-another.md +++ b/docs/presentation/how-to-move-a-paragraph-from-one-presentation-to-another.md @@ -52,13 +52,7 @@ for the file from which you want to open 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 *doc*. +[!include[Using Statement](../includes/using-statement.md)] [!include[Structure](../includes/presentation/structure.md)] diff --git a/docs/presentation/how-to-move-a-slide-to-a-new-position-in-a-presentation.md b/docs/presentation/how-to-move-a-slide-to-a-new-position-in-a-presentation.md index e8bf6e84..b63644a6 100644 --- a/docs/presentation/how-to-move-a-slide-to-a-new-position-in-a-presentation.md +++ b/docs/presentation/how-to-move-a-slide-to-a-new-position-in-a-presentation.md @@ -57,13 +57,7 @@ open 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 **presentationDocument**. +[!include[Using Statement](../includes/using-statement.md)] -------------------------------------------------------------------------------- diff --git a/docs/word/how-to-accept-all-revisions-in-a-word-processing-document.md b/docs/word/how-to-accept-all-revisions-in-a-word-processing-document.md index 0bf65264..18f3e914 100644 --- a/docs/word/how-to-accept-all-revisions-in-a-word-processing-document.md +++ b/docs/word/how-to-accept-all-revisions-in-a-word-processing-document.md @@ -10,7 +10,7 @@ ms.suite: office ms.author: o365devx author: o365devx ms.topic: conceptual -ms.date: 06/30/2021 +ms.date: 09/12/2024 ms.localizationpriority: high --- @@ -20,7 +20,7 @@ This topic shows how to use the Open XML SDK for Office to accept all revisions [!include[Structure](../includes/word/structure.md)] -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." +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." ```xml @@ -34,21 +34,21 @@ The basic document structure of a **WordProcessingML** document consists of the ``` -Using the Open XML SDK, 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](/dotnet/api/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. +Using the Open XML SDK, you can create document structure and content using strongly-typed classes that correspond to `WordprocessingML` elements. You will find these classes in the 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 Class | Description | |---|---|---| -| document | [Document](/dotnet/api/documentformat.openxml.wordprocessing.document) | The root element for the main document part. | -| body | [Body](/dotnet/api/documentformat.openxml.wordprocessing.body) | The container for the block level structures such as paragraphs, tables, annotations and others specified in the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification. | -| p | [Paragraph](/dotnet/api/documentformat.openxml.wordprocessing.paragraph) | A paragraph. | -| r | [Run](/dotnet/api/documentformat.openxml.wordprocessing.run) | A run. | -| t | [Text](/dotnet/api/documentformat.openxml.wordprocessing.text) | A range of text. | +| document | | The root element for the main document part. | +| body | | The container for the block level structures such as paragraphs, tables, annotations and others specified in the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification. | +| p | | A paragraph. | +| r | | A run. | +| t | | 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, `` (Revision Information for Paragraph Properties), **``** (Deleted Paragraph), and **``** (Inserted Table Row) elements. +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, `` (Revision Information for Paragraph Properties), `` (Deleted Paragraph), and `` (Inserted Table Row) elements. -The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification introduces the **ParagraphPropertiesChange** element (**pPrChange**). +The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification introduces the `ParagraphPropertiesChange` element (`pPrChange`). ### *pPrChange (Revision Information for Paragraph Properties) @@ -71,18 +71,18 @@ Consider a paragraph in a WordprocessingML document which is centered, and this ``` -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). **pPr** **pPrChange** +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). `pPr` `pPrChange` © [!include[ISO/IEC 29500 version](../includes/iso-iec-29500-version.md)] ## Deleted Element The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification -introduces the Deleted element (**del**). +introduces 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. +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 ¶): @@ -109,7 +109,7 @@ This revision is represented using the following WordprocessingML: ``` -The **del** element on the run properties for +The `del` element on the run properties for the first paragraph mark specifies that this paragraph mark was deleted, and this deletion was tracked as a revision. @@ -118,7 +118,7 @@ and this deletion was tracked as a revision. ## The Inserted Element The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification -introduces the Inserted element (**ins**). +introduces the Inserted element (`ins`). ### ins (Inserted Table Row) @@ -156,7 +156,7 @@ using the following WordprocessingML: ``` -The **ins** element on the table row properties for the second table row +The `ins` element on the table row properties for the second table row specifies that this row was inserted, and this insertion was tracked as a revision. @@ -165,12 +165,7 @@ a revision. ## Sample Code The following code example shows how to accept the entire revisions in a -word processing document. To run the program, you can pass in the file path -and the author name: - -```dotnetcli -dotnet run -- [filePath] [authorName] -``` +word processing document. After you have run the program, open the word processing file to make sure that all revision marks have been accepted. diff --git a/docs/word/how-to-add-tables-to-word-processing-documents.md b/docs/word/how-to-add-tables-to-word-processing-documents.md index e242dcc2..410ad0bb 100644 --- a/docs/word/how-to-add-tables-to-word-processing-documents.md +++ b/docs/word/how-to-add-tables-to-word-processing-documents.md @@ -10,19 +10,19 @@ ms.suite: office ms.author: o365devx author: o365devx ms.topic: conceptual -ms.date: 06/28/2021 +ms.date: 09/12/2024 ms.localizationpriority: high --- # Add tables to word processing documents -This topic shows how to use the classes in the Open XML SDK for Office to programmatically add a table to a word processing document. It contains an example **AddTable** method to illustrate this task. +This topic shows how to use the classes in the Open XML SDK for Office to programmatically add a table to a word processing document. It contains an example `AddTable` method to illustrate this task. ## AddTable method -You can use the **AddTable** method to add a simple table to a word processing document. The **AddTable** method accepts two parameters, indicating the following: +You can use the `AddTable` method to add a simple table to a word processing document. The `AddTable` method accepts two parameters, indicating the following: - The name of the document to modify (string). @@ -30,245 +30,74 @@ You can use the **AddTable** method to add a simple table to a word processing d table. ### [C#](#tab/cs-0) -```csharp - public static void AddTable(string fileName, string[,] data) -``` - +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet1)] ### [Visual Basic](#tab/vb-0) -```vb - Public Sub AddTable(ByVal fileName As String, - ByVal data(,) As String) -``` +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet1)] *** ## Call the AddTable method -The **AddTable** method modifies the document you specify, adding a table that contains the information in the two-dimensional array that you provide. To call the method, pass both of the parameter values, as shown in the following code. +The `AddTable` method modifies the document you specify, adding a table that contains the information in the two-dimensional array that you provide. To call the method, pass both of the parameter values, as shown in the following code. ### [C#](#tab/cs-1) -```csharp - const string fileName = @"C:\Users\Public\Documents\AddTable.docx"; - AddTable(fileName, new string[,] - { { "Texas", "TX" }, - { "California", "CA" }, - { "New York", "NY" }, - { "Massachusetts", "MA" } } - ); -``` - +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet2)] ### [Visual Basic](#tab/vb-1) -```vb - Const fileName As String = "C:\Users\Public\Documents\AddTable.docx" - AddTable(fileName, New String(,) { - {"Texas", "TX"}, - {"California", "CA"}, - {"New York", "NY"}, - {"Massachusetts", "MA"}}) -``` +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet2)] *** ## How the code works -The following code starts by opening the document, using the [WordprocessingDocument.Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method and indicating that the document should be open for read/write access (the final **true** parameter value). Next the code retrieves a reference to the root element of the main document part, using the [Document](/dotnet/api/documentformat.openxml.packaging.maindocumentpart.document) property of the [MainDocumentPart](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.maindocumentpart) of the word processing document. +The following code starts by opening the document, using the method and indicating that the document should be open for read/write access (the final `true` parameter value). Next the code retrieves a reference to the root element of the main document part, using the property of the of the word processing document. ### [C#](#tab/cs-2) -```csharp - using (var document = WordprocessingDocument.Open(fileName, true)) - { - var doc = document.MainDocumentPart.Document; - // Code removed here… - } -``` - +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet3)] ### [Visual Basic](#tab/vb-2) -```vb - Using document = WordprocessingDocument.Open(fileName, True) - Dim doc = document.MainDocumentPart.Document - ' Code removed here… - End Using -``` +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet3)] *** ## Create the table object and set its properties -Before you can insert a table into a document, you must create the [Table](/dotnet/api/documentformat.openxml.wordprocessing.table) object and set its properties. To set a table's properties, you create and supply values for a [TableProperties](/dotnet/api/documentformat.openxml.wordprocessing.tableproperties) object. The **TableProperties** class provides many table-oriented properties, like [Shading](/dotnet/api/documentformat.openxml.wordprocessing.tableproperties.shading), [TableBorders](/dotnet/api/documentformat.openxml.wordprocessing.tableproperties.tableborders), [TableCaption](/dotnet/api/documentformat.openxml.wordprocessing.tableproperties.tablecaption), [TableCellSpacing](/dotnet/api/documentformat.openxml.wordprocessing.tableproperties.tablecellspacing), [TableJustification](/dotnet/api/documentformat.openxml.wordprocessing.tableproperties.tablejustification), and more. The sample method includes the following code. +Before you can insert a table into a document, you must create the object and set its properties. To set a table's properties, you create and supply values for a object. The `TableProperties` class provides many table-oriented properties, like , , , , , and more. The sample method includes the following code. ### [C#](#tab/cs-3) -```csharp - Table table = new Table(); - - TableProperties props = new TableProperties( - new TableBorders( - new TopBorder - { - Val = new EnumValue(BorderValues.Single), - Size = 12 - }, - new BottomBorder - { - Val = new EnumValue(BorderValues.Single), - Size = 12 - }, - new LeftBorder - { - Val = new EnumValue(BorderValues.Single), - Size = 12 - }, - new RightBorder - { - Val = new EnumValue(BorderValues.Single), - Size = 12 - }, - new InsideHorizontalBorder - { - Val = new EnumValue(BorderValues.Single), - Size = 12 - }, - new InsideVerticalBorder - { - Val = new EnumValue(BorderValues.Single), - Size = 12 - })); - - table.AppendChild(props); -``` - +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet4)] ### [Visual Basic](#tab/vb-3) -```vb - Dim table As New Table() - - Dim props As TableProperties = _ - New TableProperties(New TableBorders( _ - New TopBorder With { - .Val = New EnumValue(Of BorderValues)(BorderValues.Single), - .Size = 12}, - New BottomBorder With { - .Val = New EnumValue(Of BorderValues)(BorderValues.Single), - .Size = 12}, - New LeftBorder With { - .Val = New EnumValue(Of BorderValues)(BorderValues.Single), - .Size = 12}, - New RightBorder With { - .Val = New EnumValue(Of BorderValues)(BorderValues.Single), - .Size = 12}, _ - New InsideHorizontalBorder With { - .Val = New EnumValue(Of BorderValues)(BorderValues.Single), - .Size = 12}, _ - New InsideVerticalBorder With { - .Val = New EnumValue(Of BorderValues)(BorderValues.Single), - .Size = 12})) - table.AppendChild(Of TableProperties)(props) -``` +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet4)] *** -The constructor for the **TableProperties** class allows you to specify as many child elements as you like (much like the [XElement](/dotnet/api/system.xml.linq.xelement) constructor). In this case, the code creates [TopBorder](/dotnet/api/documentformat.openxml.wordprocessing.topborder), [BottomBorder](/dotnet/api/documentformat.openxml.wordprocessing.bottomborder), [LeftBorder](/dotnet/api/documentformat.openxml.wordprocessing.leftborder), [RightBorder](/dotnet/api/documentformat.openxml.wordprocessing.rightborder), [InsideHorizontalBorder](/dotnet/api/documentformat.openxml.wordprocessing.insidehorizontalborder), and [InsideVerticalBorder](/dotnet/api/documentformat.openxml.wordprocessing.insideverticalborder) child elements, each describing one of the border elements for the table. For each element, the code sets the **Val** and **Size** properties as part of calling the constructor. Setting the size is simple, but setting the **Val** property requires a bit more effort: this property, for this particular object, represents the border style, and you must set it to an enumerated value. To do that, create an instance of the [EnumValue\](/dotnet/api/documentformat.openxml.enumvalue-1) generic type, passing the specific border type ([Single](/dotnet/api/documentformat.openxml.wordprocessing.bordervalues) as a parameter to the constructor. Once the code has set all the table border value it needs to set, it calls the [AppendChild\](/dotnet/api/documentformat.openxml.openxmlelement.appendchild) method of the table, indicating that the generic type is [TableProperties](/dotnet/api/ ocumentformat.openxml.wordprocessing.tableproperties)—that is, it is appending an instance of the **TableProperties** class, using the variable **props** as the value. +The constructor for the `TableProperties` class allows you to specify as many child elements as you like (much like the constructor). In this case, the code creates , , , , , and child elements, each describing one of the border elements for the table. For each element, the code sets the `Val` and `Size` properties as part of calling the constructor. Setting the size is simple, but setting the `Val` property requires a bit more effort: this property, for this particular object, represents the border style, and you must set it to an enumerated value. To do that, create an instance of the generic type, passing the specific border type ([Single](/dotnet/api/documentformat.openxml.wordprocessing.bordervalues) as a parameter to the constructor. Once the code has set all the table border value it needs to set, it calls the method of the table, indicating that the generic type is i.e., it is appending an instance of the `TableProperties` class, using the variable `props` as the value. ## Fill the table with data -Given that table and its properties, now it is time to fill the table with data. The sample procedure iterates first through all the rows of data in the array of strings that you specified, creating a new [TableRow](/dotnet/api/documentformat.openxml.wordprocessing.tablerow) instance for each row of data. The following code leaves out the details of filling in the row with data, but it shows how you create and append the row to the table: - -### [C#](#tab/cs-4) -```csharp - for (var i = 0; i <= data.GetUpperBound(0); i++) - { - var tr = new TableRow(); - // Code removed here… - table.Append(tr); - } -``` - -### [Visual Basic](#tab/vb-4) -```vb - For i = 0 To UBound(data, 1) - Dim tr As New TableRow - ' Code removed here… - table.Append(tr) - Next -``` -*** - - -For each row, the code iterates through all the columns in the array of strings you specified. For each column, the code creates a new [TableCell](/dotnet/api/documentformat.openxml.wordprocessing.tablecell) object, fills it with data, and appends it to the row. The following code leaves out the details of filling each cell with data, but it shows how you create and append the column to the table: - -### [C#](#tab/cs-5) -```csharp - for (var j = 0; j <= data.GetUpperBound(1); j++) - { - var tc = new TableCell(); - // Code removed here… - tr.Append(tc); - } -``` - -### [Visual Basic](#tab/vb-5) -```vb - For j = 0 To UBound(data, 2) - Dim tc As New TableCell - ' Code removed here… - tr.Append(tc) - Next -``` -*** - +Given that table and its properties, now it is time to fill the table with data. The sample procedure iterates first through all the rows of data in the array of strings that you specified, creating a new instance for each row of data. The following code shows how you create and append the row to the table. Then for each column, the code creates a new object, fills it with data, and appends it to the row. Next, the code does the following: -- Creates a new [Text](/dotnet/api/documentformat.openxml.wordprocessing.text) object that contains a value from the array of strings. -- Passes the [Text](/dotnet/api/documentformat.openxml.wordprocessing.text) object to the constructor for a new [Run](/dotnet/api/documentformat.openxml.wordprocessing.run) object. -- Passes the [Run](/dotnet/api/documentformat.openxml.wordprocessing.run) object to the constructor for a new [Paragraph](/dotnet/api/documentformat.openxml.wordprocessing.paragraph) object. -- Passes the [Paragraph](/dotnet/api/documentformat.openxml.wordprocessing.paragraph) object to the [Append](/dotnet/api/documentformat.openxml.openxmlelement.append)method of the cell. +- Creates a new object that contains a value from the array of strings. +- Passes the object to the constructor for a new object. +- Passes the object to the constructor for a new object. +- Passes the object to the method of the cell. -In other words, the following code appends the text to the new **TableCell** object. +The code then appends a new object to the cell. This `TableCellProperties` object, like the `TableProperties` object you already saw, can accept as many objects in its constructor as you care to supply. In this case, the code passes only a new object, with its property set to [Auto](/dotnet/api/documentformat.openxml.wordprocessing.tablewidthunitvalues) (so that the table automatically sizes the width of each column). -### [C#](#tab/cs-6) -```csharp - tc.Append(new Paragraph(new Run(new Text(data[i, j])))); -``` - -### [Visual Basic](#tab/vb-6) -```vb - tc.Append(New Paragraph(New Run(New Text(data(i, j))))) -``` -*** - - -The code then appends a new [TableCellProperties](/dotnet/api/documentformat.openxml.wordprocessing.tablecellproperties) object to the cell. This **TableCellProperties** object, like the **TableProperties** object you already saw, can accept as many objects in its constructor as you care to supply. In this case, the code passes only a new [TableCellWidth](/dotnet/api/documentformat.openxml.wordprocessing.tablecellwidth) object, with its [Type](/dotnet/api/documentformat.openxml.wordprocessing.tablewidthtype.type) property set to [Auto](/dotnet/api/documentformat.openxml.wordprocessing.tablewidthunitvalues) (so that the table automatically sizes the width of each column). - -### [C#](#tab/cs-7) -```csharp - // Assume you want columns that are automatically sized. - tc.Append(new TableCellProperties( - new TableCellWidth { Type = TableWidthUnitValues.Auto })); -``` - -### [Visual Basic](#tab/vb-7) -```vb - ' Assume you want columns that are automatically sized. - tc.Append(New TableCellProperties( - New TableCellWidth With {.Type = TableWidthUnitValues.Auto})) -``` +### [C#](#tab/cs-4) +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet5)] +### [Visual Basic](#tab/vb-4) +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet5)] *** - ## Finish up The following code concludes by appending the table to the body of the document, and then saving the document. ### [C#](#tab/cs-8) -```csharp - doc.Body.Append(table); - doc.Save(); -``` - +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet6)] ### [Visual Basic](#tab/vb-8) -```vb - doc.Body.Append(table) - doc.Save() -``` +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet6)] *** @@ -277,10 +106,10 @@ The following code concludes by appending the table to the body of the document, The following is the complete **AddTable** code sample in C\# and Visual Basic. ### [C#](#tab/cs) -[!code-csharp[](../../samples/word/add_tables/cs/Program.cs)] - +[!code-csharp[](../../samples/word/add_tables/cs/Program.cs#snippet0)] ### [Visual Basic](#tab/vb) -[!code-vb[](../../samples/word/add_tables/vb/Program.vb)] +[!code-vb[](../../samples/word/add_tables/vb/Program.vb#snippet0)] +*** ## See also diff --git a/docs/word/how-to-apply-a-style-to-a-paragraph-in-a-word-processing-document.md b/docs/word/how-to-apply-a-style-to-a-paragraph-in-a-word-processing-document.md index 94fe5f23..83c7dfb7 100644 --- a/docs/word/how-to-apply-a-style-to-a-paragraph-in-a-word-processing-document.md +++ b/docs/word/how-to-apply-a-style-to-a-paragraph-in-a-word-processing-document.md @@ -10,30 +10,24 @@ ms.suite: office ms.author: o365devx author: o365devx ms.topic: conceptual -ms.date: 06/28/2021 +ms.date: 06/27/2024 ms.localizationpriority: high --- # Apply a style to a paragraph in a word processing document -This topic shows how to use the classes in the Open XML SDK for Office to programmatically apply a style to a paragraph within a word processing document. It contains an example **ApplyStyleToParagraph** method to illustrate this task, plus several supplemental example methods to check whether a style exists, add a new style, and add the styles part. +This topic shows how to use the classes in the Open XML SDK for Office to programmatically apply a style to a paragraph within a word processing document. It contains an example `ApplyStyleToParagraph` method to illustrate this task, plus several supplemental example methods to check whether a style exists, add a new style, and add the styles part. ## ApplyStyleToParagraph method -The **ApplyStyleToParagraph** example method can be used to apply a style to a paragraph. You must first obtain a reference to the document as well as a reference to the paragraph that you want to style. The method accepts four parameters that indicate: the reference to the opened word processing document, the styleid of the style to be applied, the name of the style to be applied, and the reference to the paragraph to which to apply the style. +The `ApplyStyleToParagraph` example method can be used to apply a style to a paragraph. You must first obtain a reference to the document as well as a reference to the paragraph that you want to style. The method accepts four parameters that indicate: the path to the word processing document to open, the styleid of the style to be applied, the name of the style to be applied, and the reference to the paragraph to which to apply the style. ### [C#](#tab/cs-0) -```csharp - public static void ApplyStyleToParagraph(WordprocessingDocument doc, string styleid, string stylename, Paragraph p) -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet1)] ### [Visual Basic](#tab/vb-0) -```vb - Public Sub ApplyStyleToParagraph(ByVal doc As WordprocessingDocument, - ByVal styleid As String, ByVal stylename As String, ByVal p As Paragraph) -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet1)] *** @@ -43,24 +37,12 @@ The following sections in this topic explain the implementation of this method a The Sample Code section also shows the code required to set up for calling the sample method. To use the method to apply a style to a paragraph in a document, you first need a reference to the open document. In the Open XML SDK, the class represents a Word document package. To open and work with a Word document, create an instance of the class from the document. After you create the instance, use it to obtain access to the main document part that contains the text of the document. The content in the main document part is represented in the package as XML using WordprocessingML markup. -To create the class instance, call one of the overloads of the [Open()](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) method. The following sample code shows how to use the [WordprocessingDocument.Open](/dotnet/api/documentformat.openxml.packaging.wordprocessingdocument.open) overload. The first parameter takes a string that represents the full path to the document to open. The second parameter takes a value of **true** or **false** and represents whether to open the file for editing. In this example the parameter is **true** to enable read/write access to the file. +To create the class instance, call one of the overloads of the method. The following sample code shows how to use the `DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(String, Boolean)` overload. The first parameter takes a string that represents the full path to the document to open. The second parameter takes a value of `true` or `false` and represents whether to open the file for editing. In this example the parameter is `true` to enable read/write access to the file. ### [C#](#tab/cs-1) -```csharp - using (WordprocessingDocument doc = - WordprocessingDocument.Open(fileName, true)) - { - // Code removed here. - } -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet2)] ### [Visual Basic](#tab/vb-1) -```vb - Using doc As WordprocessingDocument = _ - WordprocessingDocument.Open(fileName, True) - ' Code removed here. - End Using -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet2)] *** @@ -68,43 +50,22 @@ To create the class instance, call one of the overloads of the [Open()](/dotnet/ ## Get the paragraph to style -After opening the file, the sample code retrieves a reference to the first paragraph. Because a typical word processing document body contains many types of elements, the code filters the descendants in the body of the document to those of type **Paragraph**. The [ElementAtOrDefault](/dotnet/api/system.linq.enumerable.elementatordefault) method is then employed to retrieve a reference to the paragraph. Because the elements are indexed starting at zero, you pass a zero to retrieve the reference to the first paragraph, as shown in the following code example. +After opening the file, the sample code retrieves a reference to the first paragraph. Because a typical word processing document body contains many types of elements, the code filters the descendants in the body of the document to those of type `Paragraph`. The method is then employed to retrieve a reference to the paragraph. Because the elements are indexed starting at zero, you pass a zero to retrieve the reference to the first paragraph, as shown in the following code example. ### [C#](#tab/cs-2) -```csharp - // Get the first paragraph. - Paragraph p = - doc.MainDocumentPart.Document.Body.Descendants() - .ElementAtOrDefault(0); - - // Check for a null reference. - if (p == null) - { - // Code removed here… - } -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet3)] ### [Visual Basic](#tab/vb-2) -```vb - Dim p = doc.MainDocumentPart.Document.Body.Descendants(Of Paragraph)() _ - .ElementAtOrDefault(0) - - ' Check for a null reference. - If p Is Nothing Then - ' Code removed here. - End If -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet3)] *** -The reference to the found paragraph is stored in a variable named p. If -a paragraph is not found at the specified index, the -[ElementAtOrDefault](/dotnet/api/system.linq.enumerable.elementatordefault) +The reference to the found paragraph is stored in a variable named paragraph. If +a paragraph is not found at the specified index, the `ElementAtOrDefault` method returns null as the default value. This provides an opportunity to test for null and throw an error with an appropriate error message. Once you have the references to the document and the paragraph, you can -call the **ApplyStyleToParagraph** example +call the `ApplyStyleToParagraph` example method to do the remaining work. To call the method, you pass the reference to the document as the first parameter, the styleid of the style to apply as the second parameter, the name of the style as the @@ -119,17 +80,17 @@ child element of the paragraph and includes a set of properties that allow you to specify the formatting for the paragraph. The following information from the ISO/IEC 29500 specification -introduces the **pPr** (paragraph properties) +introduces the `pPr` (paragraph properties) element used for specifying the formatting of a paragraph. Note that section numbers preceded by § are from the ISO specification. Within the paragraph, all rich formatting at the paragraph level is -stored within the **pPr** element (§17.3.1.25; §17.3.1.26). [Note: Some +stored within the `pPr` element (§17.3.1.25; §17.3.1.26). [Note: Some examples of paragraph properties are alignment, border, hyphenation override, indentation, line spacing, shading, text direction, and widow/orphan control. -Among the properties is the **pStyle** element +Among the properties is the `pStyle` element to specify the style to apply to the paragraph. For example, the following sample markup shows a pStyle element that specifies the "OverdueAmount" style. @@ -143,35 +104,17 @@ following sample markup shows a pStyle element that specifies the ``` -In the Open XML SDK, the **pPr** element is -represented by the [ParagraphProperties](/dotnet/api/documentformat.openxml.wordprocessing.paragraphproperties) class. The code +In the Open XML SDK, the `pPr` element is +represented by the class. The code determines if the element exists, and creates a new instance of the -**ParagraphProperties** class if it does not. -The **pPr** element is a child of the **p** (paragraph) element; consequently, the [PrependChild\(T)](/dotnet/api/documentformat.openxml.openxmlelement.prependchild) method is used to add +`ParagraphProperties` class if it does not. +The `pPr` element is a child of the `p` (paragraph) element; consequently, the method is used to add the instance, as shown in the following code example. ### [C#](#tab/cs-3) -```csharp - // If the paragraph has no ParagraphProperties object, create one. - if (p.Elements().Count() == 0) - { - p.PrependChild(new ParagraphProperties()); - } - - // Get the paragraph properties element of the paragraph. - ParagraphProperties pPr = p.Elements().First(); -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet4)] ### [Visual Basic](#tab/vb-3) -```vb - ' If the paragraph has no ParagraphProperties object, create one. - If p.Elements(Of ParagraphProperties)().Count() = 0 Then - p.PrependChild(Of ParagraphProperties)(New ParagraphProperties()) - End If - - ' Get the paragraph properties element of the paragraph. - Dim pPr As ParagraphProperties = p.Elements(Of ParagraphProperties)().First() -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet4)] *** @@ -190,65 +133,23 @@ following code verifies that the styles part exists, and creates it if it does not. ### [C#](#tab/cs-4) -```csharp - // Get the Styles part for this document. - StyleDefinitionsPart part = - doc.MainDocumentPart.StyleDefinitionsPart; - - // If the Styles part does not exist, add it and then add the style. - if (part == null) - { - part = AddStylesPartToPackage(doc); - // Code removed here... - } -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet5)] ### [Visual Basic](#tab/vb-4) -```vb - ' Get the Styles part for this document. - Dim part As StyleDefinitionsPart = doc.MainDocumentPart.StyleDefinitionsPart - - ' If the Styles part does not exist, add it and then add the style. - If part Is Nothing Then - part = AddStylesPartToPackage(doc) - ' Code removed here... - End If -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet5)] *** -The **AddStylesPartToPackage** example method -does the work of adding the styles part. It creates a part of the **StyleDefinitionsPart** type, adding it as a child -to the main document part. The code then appends the **Styles** root element, which is the parent element -that contains all of the styles. The **Styles** -element is represented by the [Styles](/dotnet/api/documentformat.openxml.wordprocessing.styles) class in the Open XML SDK. Finally, +The `AddStylesPartToPackage` example method +does the work of adding the styles part. It creates a part of the `StyleDefinitionsPart` type, adding it as a child +to the main document part. The code then appends the `Styles` root element, which is the parent element +that contains all of the styles. The `Styles` +element is represented by the class in the Open XML SDK. Finally, the code saves the part. ### [C#](#tab/cs-5) -```csharp - // Add a StylesDefinitionsPart to the document. Returns a reference to it. - public static StyleDefinitionsPart AddStylesPartToPackage(WordprocessingDocument doc) - { - StyleDefinitionsPart part; - part = doc.MainDocumentPart.AddNewPart(); - Styles root = new Styles(); - root.Save(part); - return part; - } -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet6)] ### [Visual Basic](#tab/vb-5) -```vb - ' Add a StylesDefinitionsPart to the document. Returns a reference to it. - Public Function AddStylesPartToPackage(ByVal doc As WordprocessingDocument) _ - As StyleDefinitionsPart - Dim part As StyleDefinitionsPart - part = doc.MainDocumentPart.AddNewPart(Of StyleDefinitionsPart)() - Dim root As New Styles() - root.Save(part) - Return part - End Function -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet6)] *** @@ -261,73 +162,26 @@ Styles are stored in the styles part, therefore if the styles part does not exist, the style itself cannot exist. If the styles part exists, the code verifies a matching style by calling -the **IsStyleIdInDocument** example method and +the `IsStyleIdInDocument` example method and passing the document and the styleid. If no match is found on styleid, -the code then tries to lookup the styleid by calling the **GetStyleIdFromStyleName** example method and +the code then tries to lookup the styleid by calling the `GetStyleIdFromStyleName` example method and passing it the style name. If the style does not exist, either because the styles part did not exist, or because the styles part exists, but the style does not, the -code calls the **AddNewStyle** example method +code calls the `AddNewStyle` example method to add the style. ### [C#](#tab/cs-6) -```csharp - // Get the Styles part for this document. - StyleDefinitionsPart part = - doc.MainDocumentPart.StyleDefinitionsPart; - - // If the Styles part does not exist, add it and then add the style. - if (part == null) - { - part = AddStylesPartToPackage(doc); - AddNewStyle(part, styleid, stylename); - } - else - { - // If the style is not in the document, add it. - if (IsStyleIdInDocument(doc, styleid) != true) - { - // No match on styleid, so let's try style name. - string styleidFromName = GetStyleIdFromStyleName(doc, stylename); - if (styleidFromName == null) - { - AddNewStyle(part, styleid, stylename); - } - else - styleid = styleidFromName; - } - } -``` - +[!code-csharp[](../../samples/word/apply_a_style_to_a_paragraph/cs/Program.cs#snippet7)] ### [Visual Basic](#tab/vb-6) -```vb - ' Get the Styles part for this document. - Dim part As StyleDefinitionsPart = doc.MainDocumentPart.StyleDefinitionsPart - - ' If the Styles part does not exist, add it and then add the style. - If part Is Nothing Then - part = AddStylesPartToPackage(doc) - AddNewStyle(part, styleid, stylename) - Else - ' If the style is not in the document, add it. - If IsStyleIdInDocument(doc, styleid) <> True Then - ' No match on styleid, so let's try style name. - Dim styleidFromName As String = - GetStyleIdFromStyleName(doc, stylename) - If styleidFromName Is Nothing Then - AddNewStyle(part, styleid, stylename) - Else - styleid = styleidFromName - End If - End If - End If -``` +[!code-vb[](../../samples/word/apply_a_style_to_a_paragraph/vb/Program.vb#snippet7)] *** -Within the **IsStyleInDocument** example -method, the work begins with retrieving the **Styles** element through the **Styles** property of the [StyleDefinitionsPart](/dotnet/api/documentformat.openxml.packaging.maindocumentpart.styledefinitionspart) of the main document +Within the `IsStyleInDocument` example +method, the work begins with retrieving the `Styles` element through the `Styles` property of the + of the main document part, and then determining whether any styles exist as children of that element. All style elements are stored as children of the styles element. @@ -336,81 +190,34 @@ If styles do exist, the code looks for a match on the styleid. The styleid is an attribute of the style that is used in many places in the document to refer to the style, and can be thought of as its primary identifier. Typically you use the styleid to identify a style in code. -The -[FirstOrDefault](/dotnet/api/system.linq.enumerable.firstordefault) +The method defaults to null if no match is found, so the code verifies for null to see whether a style was matched, as shown in the following excerpt. ### [C#](#tab/cs-7) -```csharp - // Return true if the style id is in the document, false otherwise. - public static bool IsStyleIdInDocument(WordprocessingDocument doc, - string styleid) - { - // Get access to the Styles element for this document. - Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles; - - // Check that there are styles and how many. - int n = s.Elements