Skip to content

Commit 9e74945

Browse files
authored
Merge pull request #328 from mikeebowen/general-snippets
Extract code snippets and update documentation for v3
2 parents fdee2a7 + 2d92911 commit 9e74945

File tree

241 files changed

+7978
-10286
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+7978
-10286
lines changed

docs/general/diagnosticids.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ ms.suite: office
55
ms.author: o365devx
66
author: o365devx
77
ms.topic: conceptual
8-
ms.date: 11/01/2017
8+
ms.date: 01/08/2025
99
ms.localizationpriority: high
1010
---
1111

1212
# Diagnostic IDs
1313

14-
Diagnostic IDs are used to identify APIs or patterns that can raise compiler warnings or errors. This can be done via [ObsoleteAttribute](/dotnet/api/system.obsoleteattribute.diagnosticid) or [ExperimentalAttribute](/dotnet/api/system.diagnostics.codeanalysis.experimentalattribute). These can be suppressed at the consumer level for each diagnostic id.
14+
Diagnostic IDs are used to identify APIs or patterns that can raise compiler warnings or errors. This can be done via <xref:System.ObsoleteAttribute.DiagnosticId*> or <xref:System.Diagnostics.CodeAnalysis.ExperimentalAttribute>. These can be suppressed at the consumer level for each diagnostic id.
1515

1616
## Experimental APIs
1717

1818
### OOXML0001
1919

2020
**Title**: IPackage related APIs are currently experimental
2121

22-
As of v3.0, a new abstraction layer was added in between `System.IO.Packaging` and `DocumentFormat.OpenXml.Packaging.OpenXmlPackage`. This is currently experimental, but can be used if needed. This will be stabalized in a future release, and may or may not require code changes.
22+
As of v3.0, a new abstraction layer was added in between `System.IO.Packaging` and `DocumentFormat.OpenXml.Packaging.OpenXmlPackage`. This is currently experimental, but can be used if needed. This will be stabilized in a future release, and may or may not require code changes.
2323

2424
## Suppress warnings
2525

docs/general/how-to-add-a-new-document-part-that-receives-a-relationship-id-to-a-package.md

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ ms.suite: office
1111
ms.author: o365devx
1212
author: o365devx
1313
ms.topic: conceptual
14-
ms.date: 11/01/2017
14+
ms.date: 01/08/2025
1515
ms.localizationpriority: medium
1616
---
1717

1818
# Add a new document part that receives a relationship ID to a package
1919

2020
This topic shows how to use the classes in the Open XML SDK for
21-
Office to add a document part (file) that receives a relationship **Id** parameter for a word
21+
Office to add a document part (file) that receives a relationship `Id` parameter for a word
2222
processing document.
2323

2424

@@ -32,39 +32,51 @@ processing document.
3232
[!include[Structure](../includes/word/structure.md)]
3333

3434
-----------------------------------------------------------------------------
35-
## Sample Code
36-
The following code, adds a new document part that contains custom XML
37-
from an external file and then populates the document part. You can call
38-
the method **AddNewPart** by using a call like
39-
the following code example.
40-
41-
### [C#](#tab/cs-0)
42-
```csharp
43-
string document = @"C:\Users\Public\Documents\MyPkg1.docx";
44-
AddNewPart(document);
45-
```
46-
47-
### [Visual Basic](#tab/vb-0)
48-
```vb
49-
Dim document As String = "C:\Users\Public\Documents\MyPkg1.docx"
50-
AddNewPart(document)
51-
```
35+
36+
## How the Sample Code Works
37+
38+
The sample code, in this how-to, starts by passing in a parameter that represents the path to the Word document. It then creates
39+
a new WordprocessingDocument object within a using statement.
40+
41+
### [C#](#tab/cs-1)
42+
[!code-csharp[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs#snippet1)]
43+
44+
### [Visual Basic](#tab/vb-1)
45+
[!code-vb[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb#snippet1)]
46+
***
47+
48+
It then adds the MainDocumentPart part in the new word processing document, with the relationship ID, rId1. It also adds the `CustomFilePropertiesPart` part and a `CoreFilePropertiesPart` in the new word processing document.
49+
50+
### [C#](#tab/cs-2)
51+
[!code-csharp[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs#snippet2)]
52+
53+
### [Visual Basic](#tab/vb-2)
54+
[!code-vb[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb#snippet2)]
5255
***
5356

57+
The code then adds the `DigitalSignatureOriginPart` part, the `ExtendedFilePropertiesPart` part, and the `ThumbnailPart` part in the new word processing document with realtionship IDs rId4, rId5, and rId6.
5458

55-
The following is the complete code example in both C\# and Visual Basic.
59+
> [!NOTE]
60+
> The <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument.AddNewPart*> method creates a relationship from the current document part to the new document part. This method returns the new document part. Also, you can use the <a href="xref:DocumentFormat.OpenXml.Packaging.DataPart.FeedData*?displayProperty=nameWithType"/> method to fill the document part.
61+
62+
## Sample Code
63+
64+
The following code, adds a new document part that contains custom XML
65+
from an external file and then populates the document part. Below is the
66+
complete code example in both C\# and Visual Basic.
5667

5768
### [C#](#tab/cs)
58-
[!code-csharp[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs)]
69+
[!code-csharp[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs#snippet0)]
5970

6071
### [Visual Basic](#tab/vb)
61-
[!code-vb[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb)]
72+
[!code-vb[](../../samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb#snippet0)]
73+
***
6274

6375
-----------------------------------------------------------------------------
64-
## See also
76+
## See also
6577

6678

6779
- [Open XML SDK class library reference](/office/open-xml/open-xml-sdk)
68-
80+
6981

7082

docs/general/how-to-add-a-new-document-part-to-a-package.md

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.suite: office
99
ms.author: o365devx
1010
author: o365devx
1111
ms.topic: conceptual
12-
ms.date: 03/22/2022
12+
ms.date: 01/08/2025
1313
ms.localizationpriority: medium
1414
---
1515

@@ -23,77 +23,43 @@ This topic shows how to use the classes in the Open XML SDK for Office to add a
2323

2424
The code starts with opening a package file by passing a file name to one of the overloaded <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open%2A> methods of the <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument> that takes a string and a Boolean value that specifies whether the file should be opened for editing or for read-only access. In this case, the Boolean value is `true` specifying that the file should be opened in read/write mode.
2525

26-
### [C#](#tab/cs-0)
27-
```csharp
28-
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
29-
{
30-
// Insert other code here.
31-
}
32-
```
33-
34-
### [Visual Basic](#tab/vb-0)
35-
```vb
36-
Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, True)
37-
' Insert other code here.
38-
End Using
39-
```
26+
### [C#](#tab/cs-1)
27+
[!code-csharp[](../../samples/word/add_a_new_part_to_a_package/cs/Program.cs#snippet1)]
28+
29+
### [Visual Basic](#tab/vb-1)
30+
[!code-vb[](../../samples/word/add_a_new_part_to_a_package/vb/Program.vb#snippet1)]
4031
***
4132

4233

43-
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 <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument> class in the Open XML SDK
44-
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**.
34+
[!include[Using Statement](../includes/word/using-statement.md)]
4535

4636
[!include[Structure](../includes/word/structure.md)]
4737

4838
## How the sample code works
4939

50-
After opening the document for editing, in the **using** statement, as a <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument> object, the code creates a reference to the **MainDocumentPart** part and adds a new custom XML part. It then reads the contents of the external
51-
file that contains the custom XML and writes it to the **CustomXmlPart** part.
40+
After opening the document for editing, in the `using` statement, as a <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument> object, the code creates a reference to the `MainDocumentPart` part and adds a new custom XML part. It then reads the contents of the external
41+
file that contains the custom XML and writes it to the `CustomXmlPart` part.
5242

5343
> [!NOTE]
5444
> To use the new document part in the document, add a link to the document part in the relationship part for the new part.
5545
56-
### [C#](#tab/cs-1)
57-
```csharp
58-
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
59-
CustomXmlPart myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
60-
61-
using (FileStream stream = new FileStream(fileName, FileMode.Open))
62-
{
63-
myXmlPart.FeedData(stream);
64-
}
65-
```
66-
67-
### [Visual Basic](#tab/vb-1)
68-
```vb
69-
Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
70-
71-
Dim myXmlPart As CustomXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml)
46+
### [C#](#tab/cs-2)
47+
[!code-csharp[](../../samples/word/add_a_new_part_to_a_package/cs/Program.cs#snippet2)]
7248

73-
Using stream As New FileStream(fileName, FileMode.Open)
74-
myXmlPart.FeedData(stream)
75-
End Using
76-
```
49+
### [Visual Basic](#tab/vb-2)
50+
[!code-vb[](../../samples/word/add_a_new_part_to_a_package/vb/Program.vb#snippet2)]
7751
***
7852

7953

8054
## Sample code
8155

82-
The following code adds a new document part that contains custom XML from an external file and then populates the part. To call the AddCustomXmlPart method in your program, use the following example that modifies the file "myPkg2.docx" by adding a new document part to it.
56+
The following code adds a new document part that contains custom XML from an external file and then populates the part. To call the `AddCustomXmlPart` method in your program, use the following example that modifies a file by adding a new document part to it.
8357

84-
### [C#](#tab/cs-2)
85-
```csharp
86-
string document = @"C:\Users\Public\Documents\myPkg2.docx";
87-
string fileName = @"C:\Users\Public\Documents\myXML.xml";
88-
AddNewPart(document, fileName);
89-
```
58+
### [C#](#tab/cs-3)
59+
[!code-csharp[](../../samples/word/add_a_new_part_to_a_package/cs/Program.cs#snippet3)]
9060

91-
### [Visual Basic](#tab/vb-2)
92-
```vb
93-
Dim document As String = "C:\Users\Public\Documents\myPkg2.docx"
94-
Dim fileName As String = "C:\Users\Public\Documents\myXML.xml"
95-
AddNewPart(document, fileName)
96-
```
61+
### [Visual Basic](#tab/vb-3)
62+
[!code-vb[](../../samples/word/add_a_new_part_to_a_package/vb/Program.vb#snippet3)]
9763
***
9864

9965

@@ -103,10 +69,11 @@ The following code adds a new document part that contains custom XML from an ext
10369
Following is the complete code example in both C\# and Visual Basic.
10470

10571
### [C#](#tab/cs)
106-
[!code-csharp[](../../samples/word/add_a_new_part_to_a_package/cs/Program.cs)]
72+
[!code-csharp[](../../samples/word/add_a_new_part_to_a_package/cs/Program.cs#snippet0)]
10773

10874
### [Visual Basic](#tab/vb)
109-
[!code-vb[](../../samples/word/add_a_new_part_to_a_package/vb/Program.vb)]
75+
[!code-vb[](../../samples/word/add_a_new_part_to_a_package/vb/Program.vb#snippet0)]
76+
***
11077

11178
## See also
11279

0 commit comments

Comments
 (0)