From 57643482edaa0572d1500161666ef6e4c266a5d5 Mon Sep 17 00:00:00 2001 From: Michael Bowen Date: Tue, 17 Sep 2024 10:36:53 -0700 Subject: [PATCH] closes #314 --- .../cs/Program.cs | 13 ++- .../vb/Program.vb | 14 ++- .../working_with_presentations/cs/Program.cs | 15 ++-- .../working_with_presentations/vb/Program.vb | 14 +-- .../cs/Program.cs | 51 ++++++----- .../vb/Program.vb | 58 ++++++------- .../open_from_a_stream/cs/Program.cs | 74 ++++++++-------- .../open_from_a_stream/vb/Program.vb | 72 ++++++++-------- .../spreadsheet/structure_ofml/cs/Program.cs | 32 ++++--- .../spreadsheet/structure_ofml/vb/Program.vb | 38 ++++---- .../working_with_sheets/cs/Program.cs | 69 ++++++++------- .../working_with_sheets/vb/Program.vb | 86 +++++++++---------- .../cs/Program.cs | 62 +++++++------ .../vb/Program.vb | 60 +++++++------ .../word/open_and_add_text_to/cs/Program.cs | 41 +++++---- .../word/open_and_add_text_to/vb/Program.vb | 60 +++++++------ samples/word/open_from_a_stream/cs/Program.cs | 38 ++++---- samples/word/open_from_a_stream/vb/Program.vb | 53 ++++++------ samples/word/validate/cs/Program.cs | 2 - samples/word/validate/vb/Program.vb | 2 - 20 files changed, 414 insertions(+), 440 deletions(-) diff --git a/samples/presentation/create_by_providing_a_file_name/cs/Program.cs b/samples/presentation/create_by_providing_a_file_name/cs/Program.cs index bc21e9e6..9524d8ae 100644 --- a/samples/presentation/create_by_providing_a_file_name/cs/Program.cs +++ b/samples/presentation/create_by_providing_a_file_name/cs/Program.cs @@ -11,14 +11,13 @@ static void CreatePresentation(string filepath) { // Create a presentation at a specified file path. The presentation document type is pptx, by default. - PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation); - PresentationPart presentationPart = presentationDoc.AddPresentationPart(); - presentationPart.Presentation = new Presentation(); + using (PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation)) + { + PresentationPart presentationPart = presentationDoc.AddPresentationPart(); + presentationPart.Presentation = new Presentation(); - CreatePresentationParts(presentationPart); - - //Dispose the presentation handle - presentationDoc.Dispose(); + CreatePresentationParts(presentationPart); + } } static void CreatePresentationParts(PresentationPart presentationPart) diff --git a/samples/presentation/create_by_providing_a_file_name/vb/Program.vb b/samples/presentation/create_by_providing_a_file_name/vb/Program.vb index b19d40cf..e46b8539 100644 --- a/samples/presentation/create_by_providing_a_file_name/vb/Program.vb +++ b/samples/presentation/create_by_providing_a_file_name/vb/Program.vb @@ -9,21 +9,19 @@ Namespace CreatePresentationDocument Class Program Public Shared Sub Main(ByVal args As String()) - Dim filepath As String = "C:\Users\username\Documents\PresentationFromFilename.pptx" + Dim filepath As String = args(0) CreatePresentation(filepath) End Sub Public Shared Sub CreatePresentation(ByVal filepath As String) ' Create a presentation at a specified file path. The presentation document type is pptx, by default. - Dim presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation) - Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart() - presentationPart.Presentation = New Presentation() + Using presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation) + Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart() + presentationPart.Presentation = New Presentation() - CreatePresentationParts(presentationPart) - - 'Dispose the presentation handle - presentationDoc.Dispose() + CreatePresentationParts(presentationPart) + End Using End Sub Private Shared Sub CreatePresentationParts(ByVal presentationPart As PresentationPart) diff --git a/samples/presentation/working_with_presentations/cs/Program.cs b/samples/presentation/working_with_presentations/cs/Program.cs index a1660ed0..6d48d9c7 100644 --- a/samples/presentation/working_with_presentations/cs/Program.cs +++ b/samples/presentation/working_with_presentations/cs/Program.cs @@ -2,17 +2,18 @@ using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; +CreatePresentation(args[0]); + static void CreatePresentation(string filepath) { // Create a presentation at a specified file path. The presentation document type is pptx, by default. - PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation); - PresentationPart presentationPart = presentationDoc.AddPresentationPart(); - presentationPart.Presentation = new Presentation(); - - CreatePresentationParts(presentationPart); + using (PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation)) + { + PresentationPart presentationPart = presentationDoc.AddPresentationPart(); + presentationPart.Presentation = new Presentation(); - // Dispose the presentation handle. - presentationDoc.Dispose(); + CreatePresentationParts(presentationPart); + } } static void CreatePresentationParts(PresentationPart presentationPart) { diff --git a/samples/presentation/working_with_presentations/vb/Program.vb b/samples/presentation/working_with_presentations/vb/Program.vb index 53bbf88a..600d640e 100644 --- a/samples/presentation/working_with_presentations/vb/Program.vb +++ b/samples/presentation/working_with_presentations/vb/Program.vb @@ -5,19 +5,19 @@ Imports DocumentFormat.OpenXml.Presentation Module MyModule Sub Main(args As String()) + Dim filepath As String = args(0) + CreatePresentation(filepath) End Sub Sub CreatePresentation(ByVal filepath As String) ' Create a presentation at a specified file path. The presentation document type is pptx, by default. - Dim presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation) - Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart() - presentationPart.Presentation = New Presentation() + Using presentationDoc As PresentationDocument = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation) + Dim presentationPart As PresentationPart = presentationDoc.AddPresentationPart() + presentationPart.Presentation = New Presentation() - CreatePresentationPartsFromPresentation(presentationPart) - - ' Dispose the presentation handle. - presentationDoc.Dispose() + CreatePresentationPartsFromPresentation(presentationPart) + End Using End Sub Sub CreatePresentationPartsFromPresentation(ByVal presentationPart As PresentationPart) Dim slideMasterIdList1 As New SlideMasterIdList(New SlideMasterId() With { diff --git a/samples/spreadsheet/create_by_providing_a_file_name/cs/Program.cs b/samples/spreadsheet/create_by_providing_a_file_name/cs/Program.cs index d569d83b..800eba42 100644 --- a/samples/spreadsheet/create_by_providing_a_file_name/cs/Program.cs +++ b/samples/spreadsheet/create_by_providing_a_file_name/cs/Program.cs @@ -9,32 +9,31 @@ static void CreateSpreadsheetWorkbook(string filepath) // // Create a spreadsheet document by supplying the filepath. // By default, AutoSave = true, Editable = true, and Type = xlsx. - SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook); - // - - // - // Add a WorkbookPart to the document. - WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart(); - workbookPart.Workbook = new Workbook(); - // - - // - // Add a WorksheetPart to the WorkbookPart. - WorksheetPart worksheetPart = workbookPart.AddNewPart(); - worksheetPart.Worksheet = new Worksheet(new SheetData()); - - // Add Sheets to the Workbook. - Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); - - // Append a new worksheet and associate it with the workbook. - Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" }; - sheets.Append(sheet); - // - - workbookPart.Workbook.Save(); - - // Dispose the document. - spreadsheetDocument.Dispose(); + using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)) + { + // + + // + // Add a WorkbookPart to the document. + WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart(); + workbookPart.Workbook = new Workbook(); + // + + // + // Add a WorksheetPart to the WorkbookPart. + WorksheetPart worksheetPart = workbookPart.AddNewPart(); + worksheetPart.Worksheet = new Worksheet(new SheetData()); + + // Add Sheets to the Workbook. + Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); + + // Append a new worksheet and associate it with the workbook. + Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" }; + sheets.Append(sheet); + // + + workbookPart.Workbook.Save(); + } } // diff --git a/samples/spreadsheet/create_by_providing_a_file_name/vb/Program.vb b/samples/spreadsheet/create_by_providing_a_file_name/vb/Program.vb index 993986e8..27d5e823 100644 --- a/samples/spreadsheet/create_by_providing_a_file_name/vb/Program.vb +++ b/samples/spreadsheet/create_by_providing_a_file_name/vb/Program.vb @@ -15,36 +15,34 @@ Module MyModule ' ' Create a spreadsheet document by supplying the filepath. ' By default, AutoSave = true, Editable = true, and Type = xlsx. - Dim spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook) - ' - - ' - ' Add a WorkbookPart to the document. - Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart - workbookpart.Workbook = New Workbook - ' - - ' - ' Add a WorksheetPart to the WorkbookPart. - Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)() - worksheetPart.Worksheet = New Worksheet(New SheetData()) - - ' Add Sheets to the Workbook. - Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets()) - - ' Append a new worksheet and associate it with the workbook. - Dim sheet As Sheet = New Sheet - sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart) - sheet.SheetId = 1 - sheet.Name = "mySheet" - - sheets.Append(sheet) - ' - - workbookpart.Workbook.Save() - - ' Dispose the document. - spreadsheetDocument.Dispose() + Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook) + ' + + ' + ' Add a WorkbookPart to the document. + Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart + workbookpart.Workbook = New Workbook + ' + + ' + ' Add a WorksheetPart to the WorkbookPart. + Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)() + worksheetPart.Worksheet = New Worksheet(New SheetData()) + + ' Add Sheets to the Workbook. + Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets()) + + ' Append a new worksheet and associate it with the workbook. + Dim sheet As Sheet = New Sheet + sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart) + sheet.SheetId = 1 + sheet.Name = "mySheet" + + sheets.Append(sheet) + ' + + workbookpart.Workbook.Save() + End Using End Sub ' diff --git a/samples/spreadsheet/open_from_a_stream/cs/Program.cs b/samples/spreadsheet/open_from_a_stream/cs/Program.cs index 4abdb0fb..6b7c8c53 100644 --- a/samples/spreadsheet/open_from_a_stream/cs/Program.cs +++ b/samples/spreadsheet/open_from_a_stream/cs/Program.cs @@ -7,56 +7,58 @@ static void OpenAndAddToSpreadsheetStream(Stream stream) { // Open a SpreadsheetDocument based on a stream. - SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true); - - if (spreadsheetDocument is not null) + using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true)) { - // Get or create the WorkbookPart - WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart ?? spreadsheetDocument.AddWorkbookPart(); - // - - // Add a new worksheet. - WorksheetPart newWorksheetPart = workbookPart.AddNewPart(); - newWorksheetPart.Worksheet = new Worksheet(new SheetData()); - newWorksheetPart.Worksheet.Save(); - - // + if (spreadsheetDocument is not null) + { + // Get or create the WorkbookPart + WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart ?? spreadsheetDocument.AddWorkbookPart(); - Workbook workbook = workbookPart.Workbook ?? new Workbook(); + // - if (workbookPart.Workbook is null) - { - workbookPart.Workbook = workbook; - } + // Add a new worksheet. + WorksheetPart newWorksheetPart = workbookPart.AddNewPart(); + newWorksheetPart.Worksheet = new Worksheet(new SheetData()); + newWorksheetPart.Worksheet.Save(); - Sheets sheets = workbook.GetFirstChild() ?? workbook.AppendChild(new Sheets()); - string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart); + // - // Get a unique ID for the new worksheet. - uint sheetId = 1; + Workbook workbook = workbookPart.Workbook ?? new Workbook(); - if (sheets.Elements().Count() > 0) - { - sheetId = (sheets.Elements().Select(s => s.SheetId?.Value).Max() + 1) ?? (uint)sheets.Elements().Count() + 1; - } + if (workbookPart.Workbook is null) + { + workbookPart.Workbook = workbook; + } - // Give the new worksheet a name. - string sheetName = "Sheet" + sheetId; + Sheets sheets = workbook.GetFirstChild() ?? workbook.AppendChild(new Sheets()); + string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart); - // Append the new worksheet and associate it with the workbook. - Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; - sheets.Append(sheet); - workbookPart.Workbook.Save(); + // Get a unique ID for the new worksheet. + uint sheetId = 1; - // Dispose the document handle. - spreadsheetDocument.Dispose(); + if (sheets.Elements().Count() > 0) + { + sheetId = (sheets.Elements().Select(s => s.SheetId?.Value).Max() + 1) ?? (uint)sheets.Elements().Count() + 1; + } + + // Give the new worksheet a name. + string sheetName = "Sheet" + sheetId; + + // Append the new worksheet and associate it with the workbook. + Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; + sheets.Append(sheet); + workbookPart.Workbook.Save(); + } } } // // -var fileStream = File.Open(args[0], FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); -OpenAndAddToSpreadsheetStream(fileStream); +//Stream fileStream = File.Open(args[0], FileMode.Open); +using (FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.ReadWrite)) +{ + OpenAndAddToSpreadsheetStream(fileStream); +} // \ No newline at end of file diff --git a/samples/spreadsheet/open_from_a_stream/vb/Program.vb b/samples/spreadsheet/open_from_a_stream/vb/Program.vb index b850d939..74429bf6 100644 --- a/samples/spreadsheet/open_from_a_stream/vb/Program.vb +++ b/samples/spreadsheet/open_from_a_stream/vb/Program.vb @@ -7,8 +7,9 @@ Module Program Sub Main(args As String()) ' - Dim fileStream As FileStream = New FileStream(args(0), FileMode.OpenOrCreate, FileAccess.ReadWrite) - OpenAndAddToSpreadsheetStream(fileStream) + Using fileStream As FileStream = New FileStream(args(0), FileMode.OpenOrCreate, FileAccess.ReadWrite) + OpenAndAddToSpreadsheetStream(fileStream) + End Using ' End Sub @@ -17,41 +18,38 @@ Module Program Public Sub OpenAndAddToSpreadsheetStream(ByVal stream As Stream) ' Open a SpreadsheetDocument based on a stream. - Dim mySpreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(stream, True) - - ' - - ' Add a new worksheet. - Dim newWorksheetPart As WorksheetPart = mySpreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)() - newWorksheetPart.Worksheet = New Worksheet(New SheetData()) - newWorksheetPart.Worksheet.Save() - - ' - - Dim sheets As Sheets = mySpreadsheetDocument.WorkbookPart.Workbook.GetFirstChild(Of Sheets)() - Dim relationshipId As String = mySpreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart) - - ' Get a unique ID for the new worksheet. - Dim sheetId As UInteger = 1 - If (sheets.Elements(Of Sheet).Count > 0) Then - sheetId = sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1 - End If - - ' Give the new worksheet a name. - Dim sheetName As String = ("Sheet" + sheetId.ToString()) - - ' Append the new worksheet and associate it with the workbook. - Dim sheet As Sheet = New Sheet - sheet.Id = relationshipId - sheet.SheetId = sheetId - sheet.Name = sheetName - sheets.Append(sheet) - mySpreadsheetDocument.WorkbookPart.Workbook.Save() - - 'Dispose the document handle. - mySpreadsheetDocument.Dispose() - - 'Caller must close the stream. + Using mySpreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Open(stream, True) + + ' + + ' Add a new worksheet. + Dim newWorksheetPart As WorksheetPart = mySpreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)() + newWorksheetPart.Worksheet = New Worksheet(New SheetData()) + newWorksheetPart.Worksheet.Save() + + ' + + Dim sheets As Sheets = mySpreadsheetDocument.WorkbookPart.Workbook.GetFirstChild(Of Sheets)() + Dim relationshipId As String = mySpreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart) + + ' Get a unique ID for the new worksheet. + Dim sheetId As UInteger = 1 + If (sheets.Elements(Of Sheet).Count > 0) Then + sheetId = sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1 + End If + + ' Give the new worksheet a name. + Dim sheetName As String = ("Sheet" + sheetId.ToString()) + + ' Append the new worksheet and associate it with the workbook. + Dim sheet As Sheet = New Sheet + sheet.Id = relationshipId + sheet.SheetId = sheetId + sheet.Name = sheetName + sheets.Append(sheet) + mySpreadsheetDocument.WorkbookPart.Workbook.Save() + 'Caller must close the stream. + End Using End Sub End Module diff --git a/samples/spreadsheet/structure_ofml/cs/Program.cs b/samples/spreadsheet/structure_ofml/cs/Program.cs index 58299873..cff2e0a7 100644 --- a/samples/spreadsheet/structure_ofml/cs/Program.cs +++ b/samples/spreadsheet/structure_ofml/cs/Program.cs @@ -8,25 +8,23 @@ static void CreateSpreadsheetWorkbook(string filepath) { // Create a spreadsheet document by supplying the filepath. // By default, AutoSave = true, Editable = true, and Type = xlsx. - SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook); + using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)) + { + // Add a WorkbookPart to the document. + WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart(); + workbookPart.Workbook = new Workbook(); - // Add a WorkbookPart to the document. - WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart(); - workbookPart.Workbook = new Workbook(); + // Add a WorksheetPart to the WorkbookPart. + WorksheetPart worksheetPart = workbookPart.AddNewPart(); + worksheetPart.Worksheet = new Worksheet(new SheetData()); - // Add a WorksheetPart to the WorkbookPart. - WorksheetPart worksheetPart = workbookPart.AddNewPart(); - worksheetPart.Worksheet = new Worksheet(new SheetData()); + // Add Sheets to the Workbook. + Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); - // Add Sheets to the Workbook. - Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); + // Append a new worksheet and associate it with the workbook. + Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" }; + sheets.Append(sheet); - // Append a new worksheet and associate it with the workbook. - Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" }; - sheets.Append(sheet); - - workbookPart.Workbook.Save(); - - // Dispose the document. - spreadsheetDocument.Dispose(); + workbookPart.Workbook.Save(); + } } diff --git a/samples/spreadsheet/structure_ofml/vb/Program.vb b/samples/spreadsheet/structure_ofml/vb/Program.vb index cf703b0f..cdf73f91 100644 --- a/samples/spreadsheet/structure_ofml/vb/Program.vb +++ b/samples/spreadsheet/structure_ofml/vb/Program.vb @@ -5,35 +5,35 @@ Imports DocumentFormat.OpenXml.Spreadsheet Module MyModule Sub Main(args As String()) + Dim filepath As String = args(0) + CreateSpreadsheetWorkbook(filepath) End Sub Public Sub CreateSpreadsheetWorkbook(ByVal filepath As String) ' Create a spreadsheet document by supplying the filepath. ' By default, AutoSave = true, Editable = true, and Type = xlsx. - Dim spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook) + Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook) - ' Add a WorkbookPart to the document. - Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart - workbookpart.Workbook = New Workbook + ' Add a WorkbookPart to the document. + Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart + workbookpart.Workbook = New Workbook - ' Add a WorksheetPart to the WorkbookPart. - Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)() - worksheetPart.Worksheet = New Worksheet(New SheetData()) + ' Add a WorksheetPart to the WorkbookPart. + Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)() + worksheetPart.Worksheet = New Worksheet(New SheetData()) - ' Add Sheets to the Workbook. - Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets()) + ' Add Sheets to the Workbook. + Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets()) - ' Append a new worksheet and associate it with the workbook. - Dim sheet As Sheet = New Sheet - sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart) - sheet.SheetId = 1 - sheet.Name = "mySheet" + ' Append a new worksheet and associate it with the workbook. + Dim sheet As Sheet = New Sheet + sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart) + sheet.SheetId = 1 + sheet.Name = "mySheet" - sheets.Append(sheet) + sheets.Append(sheet) - workbookpart.Workbook.Save() - - ' Dispose the document. - spreadsheetDocument.Dispose() + workbookpart.Workbook.Save() + End Using End Sub End Module \ No newline at end of file diff --git a/samples/spreadsheet/working_with_sheets/cs/Program.cs b/samples/spreadsheet/working_with_sheets/cs/Program.cs index 88d24a52..807885de 100644 --- a/samples/spreadsheet/working_with_sheets/cs/Program.cs +++ b/samples/spreadsheet/working_with_sheets/cs/Program.cs @@ -8,51 +8,50 @@ static void CreateSpreadsheetWorkbook(string filepath) { // Create a spreadsheet document by supplying the filepath. // By default, AutoSave = true, Editable = true, and Type = xlsx. - SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook); + using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook)) + { - // Add a WorkbookPart to the document. - WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart(); - workbookPart.Workbook = new Workbook(); + // Add a WorkbookPart to the document. + WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart(); + workbookPart.Workbook = new Workbook(); - // Add a WorksheetPart to the WorkbookPart. - WorksheetPart worksheetPart = workbookPart.AddNewPart(); - worksheetPart.Worksheet = new Worksheet(new SheetData()); + // Add a WorksheetPart to the WorkbookPart. + WorksheetPart worksheetPart = workbookPart.AddNewPart(); + worksheetPart.Worksheet = new Worksheet(new SheetData()); - // Add Sheets to the Workbook. - Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); + // Add Sheets to the Workbook. + Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); - // Append a new worksheet and associate it with the workbook. - Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" }; - sheets.Append(sheet); + // Append a new worksheet and associate it with the workbook. + Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" }; + sheets.Append(sheet); - // Get the sheetData cell table. - SheetData sheetData = worksheetPart.Worksheet.GetFirstChild() ?? worksheetPart.Worksheet.AppendChild(new SheetData()); + // Get the sheetData cell table. + SheetData sheetData = worksheetPart.Worksheet.GetFirstChild() ?? worksheetPart.Worksheet.AppendChild(new SheetData()); - // Add a row to the cell table. - Row row; - row = new Row() { RowIndex = 1 }; - sheetData.Append(row); + // Add a row to the cell table. + Row row; + row = new Row() { RowIndex = 1 }; + sheetData.Append(row); - // In the new row, find the column location to insert a cell in A1. - Cell? refCell = null; + // In the new row, find the column location to insert a cell in A1. + Cell? refCell = null; - foreach (Cell cell in row.Elements()) - { - if (string.Compare(cell.CellReference?.Value, "A1", true) > 0) + foreach (Cell cell in row.Elements()) { - refCell = cell; - break; + if (string.Compare(cell.CellReference?.Value, "A1", true) > 0) + { + refCell = cell; + break; + } } - } - // Add the cell to the cell table at A1. - Cell newCell = new Cell() { CellReference = "A1" }; - row.InsertBefore(newCell, refCell); + // Add the cell to the cell table at A1. + Cell newCell = new Cell() { CellReference = "A1" }; + row.InsertBefore(newCell, refCell); - // Set the cell value to be a numeric value of 100. - newCell.CellValue = new CellValue("100"); - newCell.DataType = new EnumValue(CellValues.Number); - - // Dispose the document. - spreadsheetDocument.Dispose(); + // Set the cell value to be a numeric value of 100. + newCell.CellValue = new CellValue("100"); + newCell.DataType = new EnumValue(CellValues.Number); + } } diff --git a/samples/spreadsheet/working_with_sheets/vb/Program.vb b/samples/spreadsheet/working_with_sheets/vb/Program.vb index 256551a0..4427a033 100644 --- a/samples/spreadsheet/working_with_sheets/vb/Program.vb +++ b/samples/spreadsheet/working_with_sheets/vb/Program.vb @@ -10,49 +10,47 @@ Module MyModule Public Sub CreateSpreadsheetWorkbookWithNumValue(ByVal filepath As String) ' Create a spreadsheet document by supplying the filepath. ' By default, AutoSave = true, Editable = true, and Type = xlsx. - Dim spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook) - - ' Add a WorkbookPart to the document. - Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart() - workbookpart.Workbook = New Workbook() - - ' Add a WorksheetPart to the WorkbookPart. - Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)() - worksheetPart.Worksheet = New Worksheet(New SheetData()) - - ' Add Sheets to the Workbook. - Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets()) - - ' Append a new worksheet and associate it with the workbook. - Dim sheet As New Sheet() With {.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), .SheetId = 1, .Name = "mySheet"} - sheets.Append(sheet) - - ' Get the sheetData cell table. - Dim sheetData As SheetData = worksheetPart.Worksheet.GetFirstChild(Of SheetData)() - - ' Add a row to the cell table. - Dim row As Row - row = New Row() With {.RowIndex = 1} - sheetData.Append(row) - - ' In the new row, find the column location to insert a cell in A1. - Dim refCell As Cell = Nothing - For Each cell As Cell In row.Elements(Of Cell)() - If String.Compare(cell.CellReference.Value, "A1", True) > 0 Then - refCell = cell - Exit For - End If - Next - - ' Add the cell to the cell table at A1. - Dim newCell As New Cell() With {.CellReference = "A1"} - row.InsertBefore(newCell, refCell) - - ' Set the cell value to be a numeric value of 100. - newCell.CellValue = New CellValue("100") - newCell.DataType = New EnumValue(Of CellValues)(CellValues.Number) - - ' Dispose the document. - spreadsheetDocument.Dispose() + Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook) + + ' Add a WorkbookPart to the document. + Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart() + workbookpart.Workbook = New Workbook() + + ' Add a WorksheetPart to the WorkbookPart. + Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)() + worksheetPart.Worksheet = New Worksheet(New SheetData()) + + ' Add Sheets to the Workbook. + Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets()) + + ' Append a new worksheet and associate it with the workbook. + Dim sheet As New Sheet() With {.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), .SheetId = 1, .Name = "mySheet"} + sheets.Append(sheet) + + ' Get the sheetData cell table. + Dim sheetData As SheetData = worksheetPart.Worksheet.GetFirstChild(Of SheetData)() + + ' Add a row to the cell table. + Dim row As Row + row = New Row() With {.RowIndex = 1} + sheetData.Append(row) + + ' In the new row, find the column location to insert a cell in A1. + Dim refCell As Cell = Nothing + For Each cell As Cell In row.Elements(Of Cell)() + If String.Compare(cell.CellReference.Value, "A1", True) > 0 Then + refCell = cell + Exit For + End If + Next + + ' Add the cell to the cell table at A1. + Dim newCell As New Cell() With {.CellReference = "A1"} + row.InsertBefore(newCell, refCell) + + ' Set the cell value to be a numeric value of 100. + newCell.CellValue = New CellValue("100") + newCell.DataType = New EnumValue(Of CellValues)(CellValues.Number) + End Using End Sub End Module \ No newline at end of file diff --git a/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs b/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs index aab8e89a..1a3a28bc 100644 --- a/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs +++ b/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/cs/Program.cs @@ -9,38 +9,36 @@ static void AddNewPart(string document) { // Create a new word processing document. - WordprocessingDocument wordDoc = - WordprocessingDocument.Create(document, - WordprocessingDocumentType.Document); - - // Add the MainDocumentPart part in the new word processing document. - var mainDocPart = wordDoc.AddNewPart -("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "rId1"); - mainDocPart.Document = new Document(); - - // Add the CustomFilePropertiesPart part in the new word processing document. - var customFilePropPart = wordDoc.AddCustomFilePropertiesPart(); - customFilePropPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); - - // Add the CoreFilePropertiesPart part in the new word processing document. - var coreFilePropPart = wordDoc.AddCoreFilePropertiesPart(); - using (XmlTextWriter writer = new -XmlTextWriter(coreFilePropPart.GetStream(FileMode.Create), System.Text.Encoding.UTF8)) + using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document)) { - writer.WriteRaw("\r\n"); - writer.Flush(); - } - - // Add the DigitalSignatureOriginPart part in the new word processing document. - wordDoc.AddNewPart("rId4"); - - // Add the ExtendedFilePropertiesPart part in the new word processing document. - var extendedFilePropPart = wordDoc.AddNewPart("rId5"); - extendedFilePropPart.Properties = -new DocumentFormat.OpenXml.ExtendedProperties.Properties(); - // Add the ThumbnailPart part in the new word processing document. - wordDoc.AddNewPart("image/jpeg", "rId6"); - - wordDoc.Dispose(); + // Add the MainDocumentPart part in the new word processing document. + var mainDocPart = wordDoc.AddNewPart + ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "rId1"); + mainDocPart.Document = new Document(); + + // Add the CustomFilePropertiesPart part in the new word processing document. + var customFilePropPart = wordDoc.AddCustomFilePropertiesPart(); + customFilePropPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); + + // Add the CoreFilePropertiesPart part in the new word processing document. + var coreFilePropPart = wordDoc.AddCoreFilePropertiesPart(); + using (XmlTextWriter writer = new + XmlTextWriter(coreFilePropPart.GetStream(FileMode.Create), System.Text.Encoding.UTF8)) + { + writer.WriteRaw("\r\n"); + writer.Flush(); + } + + // Add the DigitalSignatureOriginPart part in the new word processing document. + wordDoc.AddNewPart("rId4"); + + // Add the ExtendedFilePropertiesPart part in the new word processing document. + var extendedFilePropPart = wordDoc.AddNewPart("rId5"); + extendedFilePropPart.Properties = + new DocumentFormat.OpenXml.ExtendedProperties.Properties(); + + // Add the ThumbnailPart part in the new word processing document. + wordDoc.AddNewPart("image/jpeg", "rId6"); + } } diff --git a/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb b/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb index 0d6a8ac4..43cb1e79 100644 --- a/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb +++ b/samples/word/add_a_new_part_that_receives_a_relationship_id_to_a_package/vb/Program.vb @@ -12,39 +12,37 @@ Module MyModule Public Sub AddNewPart(ByVal document As String) ' Create a new word processing document. - Dim wordDoc As WordprocessingDocument = - WordprocessingDocument.Create(document, WordprocessingDocumentType.Document) + Using wordDoc As WordprocessingDocument = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document) - ' Add the MainDocumentPart part in the new word processing document. - Dim mainDocPart = wordDoc.AddNewPart(Of MainDocumentPart) _ + ' Add the MainDocumentPart part in the new word processing document. + Dim mainDocPart = wordDoc.AddNewPart(Of MainDocumentPart) _ ("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "rId1") - mainDocPart.Document = New Document() - - ' Add the CustomFilePropertiesPart part in the new word processing document. - Dim customFilePropPart = wordDoc.AddCustomFilePropertiesPart() - customFilePropPart.Properties = New DocumentFormat.OpenXml.CustomProperties.Properties() - - ' Add the CoreFilePropertiesPart part in the new word processing document. - Dim coreFilePropPart = wordDoc.AddCoreFilePropertiesPart() - Using writer As New XmlTextWriter(coreFilePropPart.GetStream(FileMode.Create), - System.Text.Encoding.UTF8) - writer.WriteRaw( - "" & vbCr & vbLf & - "") - writer.Flush() + mainDocPart.Document = New Document() + + ' Add the CustomFilePropertiesPart part in the new word processing document. + Dim customFilePropPart = wordDoc.AddCustomFilePropertiesPart() + customFilePropPart.Properties = New DocumentFormat.OpenXml.CustomProperties.Properties() + + ' Add the CoreFilePropertiesPart part in the new word processing document. + Dim coreFilePropPart = wordDoc.AddCoreFilePropertiesPart() + Using writer As New XmlTextWriter(coreFilePropPart.GetStream(FileMode.Create), + System.Text.Encoding.UTF8) + writer.WriteRaw( + "" & vbCr & vbLf & + "") + writer.Flush() + End Using + + ' Add the DigitalSignatureOriginPart part in the new word processing document. + wordDoc.AddNewPart(Of DigitalSignatureOriginPart)("rId4") + + ' Add the ExtendedFilePropertiesPart part in the new word processing document. + Dim extendedFilePropPart = wordDoc.AddNewPart(Of ExtendedFilePropertiesPart)("rId5") + extendedFilePropPart.Properties = + New DocumentFormat.OpenXml.ExtendedProperties.Properties() + + ' Add the ThumbnailPart part in the new word processing document. + wordDoc.AddNewPart(Of ThumbnailPart)("image/jpeg", "rId6") End Using - - ' Add the DigitalSignatureOriginPart part in the new word processing document. - wordDoc.AddNewPart(Of DigitalSignatureOriginPart)("rId4") - - ' Add the ExtendedFilePropertiesPart part in the new word processing document. - Dim extendedFilePropPart = wordDoc.AddNewPart(Of ExtendedFilePropertiesPart)("rId5") - extendedFilePropPart.Properties = - New DocumentFormat.OpenXml.ExtendedProperties.Properties() - - ' Add the ThumbnailPart part in the new word processing document. - wordDoc.AddNewPart(Of ThumbnailPart)("image/jpeg", "rId6") - - wordDoc.Dispose() End Sub End Module \ No newline at end of file diff --git a/samples/word/open_and_add_text_to/cs/Program.cs b/samples/word/open_and_add_text_to/cs/Program.cs index bd6e85d8..97410ecb 100644 --- a/samples/word/open_and_add_text_to/cs/Program.cs +++ b/samples/word/open_and_add_text_to/cs/Program.cs @@ -7,31 +7,30 @@ static void OpenAndAddTextToWordDocument(string filepath, string txt) { // // Open a WordprocessingDocument for editing using the filepath. - WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true); - - if (wordprocessingDocument is null) + using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true)) { - throw new ArgumentNullException(nameof(wordprocessingDocument)); - } - // - // - // Assign a reference to the existing document body. - MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart(); - mainDocumentPart.Document ??= new Document(); - mainDocumentPart.Document.Body ??= mainDocumentPart.Document.AppendChild(new Body()); - Body body = wordprocessingDocument.MainDocumentPart!.Document!.Body!; - // + if (wordprocessingDocument is null) + { + throw new ArgumentNullException(nameof(wordprocessingDocument)); + } + // - // - // Add new text. - Paragraph para = body.AppendChild(new Paragraph()); - Run run = para.AppendChild(new Run()); - run.AppendChild(new Text(txt)); - // + // + // Assign a reference to the existing document body. + MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart(); + mainDocumentPart.Document ??= new Document(); + mainDocumentPart.Document.Body ??= mainDocumentPart.Document.AppendChild(new Body()); + Body body = wordprocessingDocument.MainDocumentPart!.Document!.Body!; + // - // There is no using block, so Dispose the handle explicitly. - wordprocessingDocument.Dispose(); + // + // Add new text. + Paragraph para = body.AppendChild(new Paragraph()); + Run run = para.AppendChild(new Run()); + run.AppendChild(new Text(txt)); + // + } } // diff --git a/samples/word/open_and_add_text_to/vb/Program.vb b/samples/word/open_and_add_text_to/vb/Program.vb index d8dc105f..3930fbda 100644 --- a/samples/word/open_and_add_text_to/vb/Program.vb +++ b/samples/word/open_and_add_text_to/vb/Program.vb @@ -18,37 +18,35 @@ Module MyModule ' ' Open a WordprocessingDocument for editing using the filepath. - Dim wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(filepath, True) - - If wordprocessingDocument Is Nothing Then - Throw New ArgumentNullException(NameOf(wordprocessingDocument)) - End If - ' - - ' - ' Assign a reference to the existing document body. - Dim mainDocumentPart As MainDocumentPart = If(wordprocessingDocument.MainDocumentPart, wordprocessingDocument.AddMainDocumentPart()) - - If wordprocessingDocument.MainDocumentPart.Document Is Nothing Then - wordprocessingDocument.MainDocumentPart.Document = New Document() - End If - - If wordprocessingDocument.MainDocumentPart.Document.Body Is Nothing Then - wordprocessingDocument.MainDocumentPart.Document.Body = New Body() - End If - - Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body - ' - - ' - ' Add new text. - Dim para As Paragraph = body.AppendChild(New Paragraph) - Dim run As Run = para.AppendChild(New Run) - run.AppendChild(New Text(txt)) - ' - - ' There is not using, so Dispose the handle explicitly. - wordprocessingDocument.Dispose() + Using wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(filepath, True) + + If wordprocessingDocument Is Nothing Then + Throw New ArgumentNullException(NameOf(wordprocessingDocument)) + End If + ' + + ' + ' Assign a reference to the existing document body. + Dim mainDocumentPart As MainDocumentPart = If(wordprocessingDocument.MainDocumentPart, wordprocessingDocument.AddMainDocumentPart()) + + If wordprocessingDocument.MainDocumentPart.Document Is Nothing Then + wordprocessingDocument.MainDocumentPart.Document = New Document() + End If + + If wordprocessingDocument.MainDocumentPart.Document.Body Is Nothing Then + wordprocessingDocument.MainDocumentPart.Document.Body = New Body() + End If + + Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body + ' + + ' + ' Add new text. + Dim para As Paragraph = body.AppendChild(New Paragraph) + Dim run As Run = para.AppendChild(New Run) + run.AppendChild(New Text(txt)) + ' + End Using End Sub End Module ' diff --git a/samples/word/open_from_a_stream/cs/Program.cs b/samples/word/open_from_a_stream/cs/Program.cs index 60326a64..c219fcf0 100644 --- a/samples/word/open_from_a_stream/cs/Program.cs +++ b/samples/word/open_from_a_stream/cs/Program.cs @@ -8,26 +8,24 @@ static void OpenAndAddToWordprocessingStream(Stream stream, string txt) { // // Open a WordProcessingDocument based on a stream. - WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true); - // - - // - // Assign a reference to the document body. - MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart(); - mainDocumentPart.Document ??= new Document(); - Body body = mainDocumentPart.Document.Body ?? mainDocumentPart.Document.AppendChild(new Body()); - // - - // - // Add new text. - Paragraph para = body.AppendChild(new Paragraph()); - Run run = para.AppendChild(new Run()); - run.AppendChild(new Text(txt)); - // - - // Dispose the document handle. - wordprocessingDocument.Dispose(); - + using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true)) + { + // + + // + // Assign a reference to the document body. + MainDocumentPart mainDocumentPart = wordprocessingDocument.MainDocumentPart ?? wordprocessingDocument.AddMainDocumentPart(); + mainDocumentPart.Document ??= new Document(); + Body body = mainDocumentPart.Document.Body ?? mainDocumentPart.Document.AppendChild(new Body()); + // + + // + // Add new text. + Paragraph para = body.AppendChild(new Paragraph()); + Run run = para.AppendChild(new Run()); + run.AppendChild(new Text(txt)); + // + } // Caller must close the stream. } // diff --git a/samples/word/open_from_a_stream/vb/Program.vb b/samples/word/open_from_a_stream/vb/Program.vb index 4a6cda00..af5c65b2 100644 --- a/samples/word/open_from_a_stream/vb/Program.vb +++ b/samples/word/open_from_a_stream/vb/Program.vb @@ -21,34 +21,31 @@ Module MyModule ' ' Open a WordProcessingDocument based on a stream. - Dim wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(stream, True) - ' - - ' - ' Assign a reference to the document body. - Dim mainDocumentPart As MainDocumentPart = If(wordprocessingDocument.MainDocumentPart, wordprocessingDocument.AddMainDocumentPart()) - - If wordprocessingDocument.MainDocumentPart.Document Is Nothing Then - wordprocessingDocument.MainDocumentPart.Document = New Document() - End If - - If wordprocessingDocument.MainDocumentPart.Document.Body Is Nothing Then - wordprocessingDocument.MainDocumentPart.Document.Body = New Body() - End If - - Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body - ' - - ' - ' Add new text. - Dim para As Paragraph = body.AppendChild(New Paragraph) - Dim run As Run = para.AppendChild(New Run) - run.AppendChild(New Text(txt)) - ' - - ' Dispose the document handle. - wordprocessingDocument.Dispose() - + Using wordprocessingDocument As WordprocessingDocument = WordprocessingDocument.Open(stream, True) + ' + + ' + ' Assign a reference to the document body. + Dim mainDocumentPart As MainDocumentPart = If(wordprocessingDocument.MainDocumentPart, wordprocessingDocument.AddMainDocumentPart()) + + If wordprocessingDocument.MainDocumentPart.Document Is Nothing Then + wordprocessingDocument.MainDocumentPart.Document = New Document() + End If + + If wordprocessingDocument.MainDocumentPart.Document.Body Is Nothing Then + wordprocessingDocument.MainDocumentPart.Document.Body = New Body() + End If + + Dim body As Body = wordprocessingDocument.MainDocumentPart.Document.Body + ' + + ' + ' Add new text. + Dim para As Paragraph = body.AppendChild(New Paragraph) + Dim run As Run = para.AppendChild(New Run) + run.AppendChild(New Text(txt)) + ' + End Using ' Caller must close the stream. End Sub End Module diff --git a/samples/word/validate/cs/Program.cs b/samples/word/validate/cs/Program.cs index 44bc1ba3..e40db291 100644 --- a/samples/word/validate/cs/Program.cs +++ b/samples/word/validate/cs/Program.cs @@ -39,8 +39,6 @@ static void ValidateWordDocument(string filepath) { Console.WriteLine(ex.Message); } - - wordprocessingDocument.Dispose(); } } diff --git a/samples/word/validate/vb/Program.vb b/samples/word/validate/vb/Program.vb index 1bf1365c..e2289271 100644 --- a/samples/word/validate/vb/Program.vb +++ b/samples/word/validate/vb/Program.vb @@ -30,8 +30,6 @@ Module Program Catch ex As Exception Console.WriteLine(ex.Message) End Try - - wordprocessingDocument__1.Dispose() End Using End Sub