Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move samples to .cs and .vb files #167

Merged
merged 15 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 4 additions & 79 deletions docs/how-to-add-custom-ui-to-a-spreadsheet-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,86 +169,11 @@ Given a reference to the ribbon extensibility part, the following code finishes

The following is the complete **AddCustomUI** code sample in C\# and Visual Basic.

```csharp
static public void AddCustomUI(string fileName, string customUIContent)
{
// Add a custom UI part to the document.
// Use this sample XML to test:
//<customUI xmlns="https://schemas.microsoft.com/office/2006/01/customui">
// <ribbon>
// <tabs>
// <tab idMso="TabAddIns">
// <group id="Group1" label="Group1">
// <button id="Button1" label="Button1"
// showImage="false" onAction="SampleMacro"/>
// </group>
// </tab>
// </tabs>
// </ribbon>
//</customUI>

// In the sample XLSM file, create a module and create a procedure
// named SampleMacro, using this
// signature: Public Sub SampleMacro(control As IRibbonControl)
// Add some code, and then save and close the XLSM file. Run this
// example to add a button to the Add-Ins tab that calls the macro,
// given the XML content above in the AddCustomUI.xml file.

using (SpreadsheetDocument document =
SpreadsheetDocument.Open(fileName, true))
{
// You can have only a single ribbon extensibility part.
// If the part doesn't exist, create it.
var part = document.RibbonExtensibilityPart;
if (part == null)
{
part = document.AddRibbonExtensibilityPart();
}
part.CustomUI = new CustomUI(customUIContent);
part.CustomUI.Save();
}
}
```
### [C#](#tab/cs)
[!code-csharp[](../samples/spreadsheet/add_custom_ui/cs/Program.cs)]

```vb
Public Sub XLAddCustomUI(ByVal fileName As String,
ByVal customUIContent As String)
' Add a custom UI part to the document.
' Use this sample XML to test:

'<customUI xmlns="https://schemas.microsoft.com/office/2006/01/customui">
' <ribbon>
' <tabs>
' <tab idMso="TabAddIns">
' <group id="Group1" label="Group1">
' <button id="Button1" label="Button1"
' showImage="false" onAction="SampleMacro"/>
' </group>
' </tab>
' </tabs>
' </ribbon>
'</customUI>

' In the sample XLSM file, create a module and create a procedure
' named SampleMacro, using this signature:
' Public Sub SampleMacro(control As IRibbonControl)
' Add some code, and then save and close the XLSM file. Run this
' example to add a button to the Add-Ins tab that calls the macro,
' given the XML content above in the AddCustomUI.xml file.

Using document As SpreadsheetDocument =
SpreadsheetDocument.Open(fileName, True)
' You can have only a single ribbon extensibility part.
' If the part doesn't exist, add it.
Dim part = document.RibbonExtensibilityPart
If part Is Nothing Then
part = document.AddRibbonExtensibilityPart
End If
part.CustomUI = New CustomUI(customUIContent)
part.CustomUI.Save()
End Using
End Sub
```
### [Visual Basic](#tab/vb)
[!code-vb[](../samples/spreadsheet/add_custom_ui/vb/Program.vb)]

## See also

Expand Down
124 changes: 4 additions & 120 deletions docs/how-to-add-tables-to-word-processing-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,127 +257,11 @@ 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.

```csharp
// Take the data from a two-dimensional array and build a table at the
// end of the supplied document.
public static void AddTable(string fileName, string[,] data)
{
using (var document = WordprocessingDocument.Open(fileName, true))
{

var doc = document.MainDocumentPart.Document;

Table table = new Table();

TableProperties props = new TableProperties(
new TableBorders(
new TopBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new BottomBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new LeftBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new RightBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}));

table.AppendChild<TableProperties>(props);

for (var i = 0; i <= data.GetUpperBound(0); i++)
{
var tr = new TableRow();
for (var j = 0; j <= data.GetUpperBound(1); j++)
{
var tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text(data[i, j]))));

// Assume you want columns that are automatically sized.
tc.Append(new TableCellProperties(
new TableCellWidth { Type = TableWidthUnitValues.Auto }));

tr.Append(tc);
}
table.Append(tr);
}
doc.Body.Append(table);
doc.Save();
}
}
```
### [C#](#tab/cs)
[!code-csharp[](../samples/word/add_tables/cs/Program.cs)]

```vb
' Take the data from a two-dimensional array and build a table at the
' end of the supplied document.
Public Sub AddTable(ByVal fileName As String,
ByVal data(,) As String)
Using document = WordprocessingDocument.Open(fileName, True)

Dim doc = document.MainDocumentPart.Document

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)

For i = 0 To UBound(data, 1)
Dim tr As New TableRow
For j = 0 To UBound(data, 2)
Dim tc As New TableCell
tc.Append(New Paragraph(New Run(New Text(data(i, j)))))

' Assume you want columns that are automatically sized.
tc.Append(New TableCellProperties(
New TableCellWidth With {.Type = TableWidthUnitValues.Auto}))

tr.Append(tc)
Next
table.Append(tr)
Next
doc.Body.Append(table)
doc.Save()
End Using
End Sub
```
### [Visual Basic](#tab/vb)
[!code-vb[](../samples/word/add_tables/vb/Program.vb)]

## See also

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,7 @@ ms.localizationpriority: high

This topic shows how to use the Open XML SDK for Office to programmatically change text in a table in an existing word processing document.

The following assembly directives are required to compile the code in this topic.

```csharp
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
```

```vb
Imports System.Linq
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing
```

## Open the Existing Document

Expand Down Expand Up @@ -197,65 +185,11 @@ example."

Following is the complete code example.

```csharp
// Change the text in a table in a word processing document.
public static void ChangeTextInCell(string filepath, string txt)
{
// Use the file name and path passed in as an argument to
// open an existing document.
using (WordprocessingDocument doc =
WordprocessingDocument.Open(filepath, true))
{
// Find the first table in the document.
Table table =
doc.MainDocumentPart.Document.Body.Elements<Table>().First();

// Find the second row in the table.
TableRow row = table.Elements<TableRow>().ElementAt(1);

// Find the third cell in the row.
TableCell cell = row.Elements<TableCell>().ElementAt(2);

// Find the first paragraph in the table cell.
Paragraph p = cell.Elements<Paragraph>().First();

// Find the first run in the paragraph.
Run r = p.Elements<Run>().First();

// Set the text for the run.
Text t = r.Elements<Text>().First();
t.Text = txt;
}
}
```
### [C#](#tab/cs)
[!code-csharp[](../samples/word/change_text_a_table/cs/Program.cs)]

```vb
' Change the text in a table in a word processing document.
Public Sub ChangeTextInCell(ByVal filepath As String, ByVal txt As String)
' Use the file name and path passed in as an argument to
' Open an existing document.
Using doc As WordprocessingDocument = WordprocessingDocument.Open(filepath, True)
' Find the first table in the document.
Dim table As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table)().First()

' Find the second row in the table.
Dim row As TableRow = table.Elements(Of TableRow)().ElementAt(1)

' Find the third cell in the row.
Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(2)

' Find the first paragraph in the table cell.
Dim p As Paragraph = cell.Elements(Of Paragraph)().First()

' Find the first run in the paragraph.
Dim r As Run = p.Elements(Of Run)().First()

' Set the text for the run.
Dim t As Text = r.Elements(Of Text)().First()
t.Text = txt
End Using
End Sub
```
### [Visual Basic](#tab/vb)
[!code-vb[](../samples/word/change_text_a_table/vb/Program.vb)]

## See also

Expand Down
Loading