Skip to content

Commit

Permalink
Extract snippets from remaining SpreadsheetML tutorials, extract "Pac…
Browse files Browse the repository at this point in the history
…kages and Document Parts" section, and fix multiple xmlns errors (#298)
  • Loading branch information
mikeebowen authored Jan 12, 2024
1 parent 04deec7 commit ba31743
Show file tree
Hide file tree
Showing 26 changed files with 310 additions and 1,160 deletions.
2 changes: 1 addition & 1 deletion docs/includes/spreadsheet/structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ code example.

```xml
<?xml version="1.0" encoding="UTF-8" ?>
<worksheet xmlns="https://schemas.openxmlformats.org/spreadsheetml/2006/main">
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<sheetData>
<row r="1">
<c r="A1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ The following XML code is the PresentationML that represents the relationship pa

```xml
<?xml version="1.0" encoding="utf-8"?>
<Relationships xmlns="https://schemas.openxmlformats.org/package/2006/relationships">
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Type="https://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"
Target="/ppt/slides/slide.xml"
Id="rId2" />
Expand Down
118 changes: 21 additions & 97 deletions docs/spreadsheet/how-to-add-custom-ui-to-a-spreadsheet-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ms.suite: office
ms.author: o365devx
author: o365devx
ms.topic: conceptual
ms.date: 11/01/2017
ms.date: 01/10/2024
ms.localizationpriority: high
---

Expand All @@ -24,22 +24,15 @@ this task.

## Creating Custom UI

Before using the Open XML SDK to create a ribbon customization in an Excel workbook, you must first create the customization content. Describing the XML required to create a ribbon customization is beyond the scope of this topic. In addition, you will find it far easier to use the Ribbon Designer in Visual Studio 2010 to create the customization for you. For more information about customizing the ribbon by using the Visual Studio Ribbon Designer, see [Ribbon Designer](https://msdn.microsoft.com/library/26617206-f4da-416f-a18a-d817b2d4872d(Office.15).aspx) and [Walkthrough: Creating a Custom Tab by Using the Ribbon Designer](https://msdn.microsoft.com/library/312865e6-950f-46ab-88de-fe7eb8036bfe(Office.15).aspx).
For the purposes of this demonstration, you will need an XML file that contains a customization, and the following code provides a simple customization (or you can create your own by using the Visual Studio Ribbon Designer, and then right-click to export the customization to an XML file). Copy the following content into a text file that is named AddCustomUI.xml for use as part of this example. This XML content describes a ribbon customization that includes a button labeled "Click Me!" in a group named Group1 on the **Add-Ins** tab in Excel. When you click the button, it attempts to run a macro named **SampleMacro** in the host workbook.

```xml
<customUI xmlns="https://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="Group1" label="Group1">
<button id="Button1" label="Click Me!" showImage="false" onAction="SampleMacro"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
```
Before using the Open XML SDK to create a ribbon customization in an Excel workbook, you must first create the customization content. Describing the XML required to create a ribbon customization is beyond the scope of this topic. In addition, you will find it far easier to use the Ribbon Designer in Visual Studio to create the customization for you. For more information about customizing the ribbon by using the Visual Studio Ribbon Designer, see [Ribbon Designer](https://msdn.microsoft.com/library/26617206-f4da-416f-a18a-d817b2d4872d(Office.15).aspx) and [Walkthrough: Creating a Custom Tab by Using the Ribbon Designer](https://msdn.microsoft.com/library/312865e6-950f-46ab-88de-fe7eb8036bfe(Office.15).aspx).
For the purposes of this demonstration, you will need an XML file that contains a customization, and the following code provides a simple customization (or you can create your own by using the Visual Studio Ribbon Designer, and then right-click to export the customization to an XML file). The samples below are the xml strings used in this example. This XML content describes a ribbon customization that includes a button labeled "Click Me!" in a group named Group1 on the **Add-Ins** tab in Excel. When you click the button, it attempts to run a macro named **SampleMacro** in the host workbook.

### [C#](#tab/cs-xml)
[!code-csharp[](../../samples/spreadsheet/add_custom_ui/cs/Program.cs#snippet4)]
### [Visual Basic](#tab/vb-xml)
[!code-vb[](../../samples/spreadsheet/add_custom_ui/vb/Program.vb#snippet4)]
***


## Create the Macro

Expand Down Expand Up @@ -69,60 +62,14 @@ The **AddCustomUI** method accepts two parameters:

- customUIContent*A string that contains the custom content (that is, the XML markup that describes the customization).

The following code shows the two parameters.

### [C#](#tab/cs-0)
```csharp
static public void AddCustomUI(string fileName, string customUIContent)
```

### [Visual Basic](#tab/vb-0)
```vb
Public Sub XLAddCustomUI(ByVal fileName As String,
ByVal customUIContent As String)
```
***


## Call the AddCustomUI Method

The method modifies the ribbon in an Excel workbook. To call the method, pass the file name of the workbook to modify, and a string that contains the customization XML, as shown in the following example code.

### [C#](#tab/cs-1)
```csharp
const string SAMPLEXML = "AddCustomUI.xml";
const string DEMOFILE = "AddCustomUI.xlsm";

string content = System.IO.File.OpenText(SAMPLEXML).ReadToEnd();
AddCustomUI(DEMOFILE, content);
```

### [Visual Basic](#tab/vb-1)
```vb
Const SAMPLEXML As String = "AddCustomUI.xml"
Const DEMOFILE As String = "AddCustomUI.xlsm"

Dim content As String = System.IO.File.OpenText(SAMPLEXML).ReadToEnd()
AddCustomUI(DEMOFILE, content)
```
***


## Interact with the Workbook

The sample method, **AddCustomUI**, starts by opening the requested workbook in read/write mode, as shown in the following code.

### [C#](#tab/cs-2)
```csharp
using (SpreadsheetDocument document =
SpreadsheetDocument.Open(fileName, true))
```

[!code-csharp[](../../samples/spreadsheet/add_custom_ui/cs/Program.cs#snippet1)]
### [Visual Basic](#tab/vb-2)
```vb
Using document As SpreadsheetDocument =
SpreadsheetDocument.Open(fileName, True)
```
[!code-vb[](../../samples/spreadsheet/add_custom_ui/vb/Program.vb#snippet1)]
***


Expand All @@ -131,25 +78,9 @@ The sample method, **AddCustomUI**, starts by opening the requested workbook in
Next, as shown in the following code, the sample method attempts to retrieve a reference to the single ribbon extensibility part. If the part does not yet exist, the code creates it and stores a reference to the new part.

### [C#](#tab/cs-3)
```csharp
// 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();
}
```

[!code-csharp[](../../samples/spreadsheet/add_custom_ui/cs/Program.cs#snippet2)]
### [Visual Basic](#tab/vb-3)
```vb
' 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
```
[!code-vb[](../../samples/spreadsheet/add_custom_ui/vb/Program.vb#snippet2)]
***


Expand All @@ -158,31 +89,24 @@ Next, as shown in the following code, the sample method attempts to retrieve a r
Given a reference to the ribbon extensibility part, the following code finishes by setting the part's **CustomUI** property to a new [CustomUI](https://msdn.microsoft.com/library/office/documentformat.openxml.office.customui.customui.aspx) object that contains the supplied customization. Once the customization is in place, the code saves the custom UI.

### [C#](#tab/cs-4)
```csharp
part.CustomUI = new CustomUI(customUIContent);
part.CustomUI.Save();
```

[!code-csharp[](../../samples/spreadsheet/add_custom_ui/cs/Program.cs#snippet3)]
### [Visual Basic](#tab/vb-4)
```vb
part.CustomUI = New CustomUI(customUIContent)
part.CustomUI.Save()
```
[!code-vb[](../../samples/spreadsheet/add_custom_ui/vb/Program.vb#snippet3)]
***


## Sample Code

The following is the complete **AddCustomUI** code sample in C\# and Visual Basic.
The following is the complete **AddCustomUI** code sample in C\# and Visual Basic. The first argument passed to the **AddCustomUI** should be the absolute
path to the AddCustomUI.xlsm file created from the instructions above.

### [C#](#tab/cs)
[!code-csharp[](../../samples/spreadsheet/add_custom_ui/cs/Program.cs)]
[!code-csharp[](../../samples/spreadsheet/add_custom_ui/cs/Program.cs#snippet0)]

### [Visual Basic](#tab/vb)
[!code-vb[](../../samples/spreadsheet/add_custom_ui/vb/Program.vb)]
[!code-vb[](../../samples/spreadsheet/add_custom_ui/vb/Program.vb#snippet0)]

## See also

- [Open XML SDK class library reference](/office/open-xml/open-xml-sdk)
- [Ribbon Designer](https://msdn.microsoft.com/library/26617206-f4da-416f-a18a-d817b2d4872d(Office.15).aspx)
- [Walkthrough: Creating a Custom Tab by Using the Ribbon Designer](https://msdn.microsoft.com/library/312865e6-950f-46ab-88de-fe7eb8036bfe(Office.15).aspx)

Loading

0 comments on commit ba31743

Please sign in to comment.