Skip to content

Latest commit

 

History

History
183 lines (150 loc) · 6.99 KB

kb-common-create-excel-addin.md

File metadata and controls

183 lines (150 loc) · 6.99 KB
title description page_title type slug position tags res_type ticketid
Integration With Visual Studio Tools for Microsoft Office (VSTO) Templates
Create an Excel/Word/PowerPoint/Outlook VSTO template with Telerik UI for WPF components.
Telerik UI for WPF Integration With Microsoft VSTO Templates
how-to
kb-common-create-excel-addin
0
excel, addin, wpf, vsto
kb
1597957

Environment

Product Version 2023.3.1114
Product UI for WPF

Description

How to create an add-in for Microsoft Office with Telerik UI for WPF.

Solution

The following example shows how to create an Excel VSTO Add-in application and display a RadWindow and RadGridView elements. It is setup with the NoXaml version of the assemblies in mind.

  1. Open Visual Studio and choose the Excel VSTO Add-in template:

{{ site.framework_name }}

To create a VSTO project, you will need to install the Office/SharePoint Development package from the Visual Studio Installer application.

Office/SharePoint development package

{{ site.framework_name }} Office/SharePoint development package

  1. Create a new UserControl that will hold the Telerik UI for WPF controls.

    [XAML] WPF UserControl with Telerik UI for WPF components

    {{region kb-common-create-excel-addin-0}} <UserControl.Resources> <ResourceDictionary.MergedDictionaries> </ResourceDictionary.MergedDictionaries> </UserControl.Resources> <telerik:RadButton Content="RadButton" Click="RadButton_Click"/> {{endregion}}

The above RadButton will create a new RadWindow instance that will host a RadGridView control when its Click event occurs.

#### __[C#] RadButton Click event handler__
{{region kb-common-create-excel-addin-1}}
	private void RadButton_Click(object sender, RoutedEventArgs e)
	{
	    RadWindow radWindow = new RadWindow()
	    {
	        Header = "RadWindow with RadGridView",
	        WindowStartupLocation = WindowStartupLocation.CenterScreen,
	        Owner = this,
	        Width = 400,
	        Height = 300,
	    };	
	    this.SetupResources(radWindow);	
	    RadGridView radGridView = new RadGridView() { ItemsSource = this.peopleViewModel.People };
	    radWindow.Content = radGridView;	
	    radWindow.ShowDialog();
	}
{{endregion}}

The SetupResources method from the above code snippet merges the resource dictionaries required by the RadGridView and RadWindow controls. The example utilizes the Office2019 theme.

#### __[C#] SetupResources method's implementation__
{{region kb-common-create-excel-addin-2}}
	private void SetupResources(RadWindow radWindow)
	{
	    radWindow.Resources.MergedDictionaries.Clear();

	    radWindow.Resources.MergedDictionaries.Add(new ResourceDictionary()
	    {
	        Source = new Uri("/Telerik.Windows.Themes.Office2019;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
	    });
	    radWindow.Resources.MergedDictionaries.Add(new ResourceDictionary()
	    {
	        Source = new Uri("/Telerik.Windows.Themes.Office2019;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
	    });
	    radWindow.Resources.MergedDictionaries.Add(new ResourceDictionary()
	    {
	        Source = new Uri("/Telerik.Windows.Themes.Office2019;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute)
	    });
	    radWindow.Resources.MergedDictionaries.Add(new ResourceDictionary()
	    {
	        Source = new Uri("/Telerik.Windows.Themes.Office2019;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
	    });
	    radWindow.Resources.MergedDictionaries.Add(new ResourceDictionary()
	    {
	        Source = new Uri("/Telerik.Windows.Themes.Office2019;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute)
	    });
	}
{{endregion}}
  1. Create a WindowsForms UserControl that will hold the WPF UserControl in its Child property.

    [C#] WindowsForms UserControl

    {{region kb-common-create-excel-addin-3}} public partial class WinFormsUserControl : UserControl { private ElementHost ctrlHost;

         public WinFormsUserControl()
         {
             InitializeComponent();
    
             ctrlHost = new ElementHost();
             ctrlHost.Child = new WPFUserControl();
             this.Controls.Add(ctrlHost);
         }
     }
    

    {{endregion}}

  2. Open the add-in class generated by the Excel VSTO Add-in template and create new CustomTaskPane instance in the event handler for the Startup event. This CustomTaskPane will hold the WindowsForms UserControl.

    [C#] AddIn class implementation

    {{region kb-common-create-excel-addin-4}} public partial class MyAddIn { private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane; private WinFormsUserControl userControl;

         private void OnMyAddInStartup(object sender, System.EventArgs e)
         {
             userControl = new WinFormsUserControl();
    
             myCustomTaskPane = this.CustomTaskPanes.Add(userControl, "My Task Pane");
             myCustomTaskPane.Visible = true;
         }
    
         private void OnMyAddInShutdown(object sender, System.EventArgs e)
         {
         }
    
         #region VSTO generated code
    
         /// <summary>
         /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
         /// </summary>
         private void InternalStartup()
         {
             this.Startup += new System.EventHandler(OnMyAddInStartup);
             this.Shutdown += new System.EventHandler(OnMyAddInShutdown);
         }
     
         #endregion
     }
    

    {{endregion}}

Excel VSTO Add-in with a WinForms UserControl that hosts the WPFUserControl

{{ site.framework_name }} Excel VSTO Add-in with a WinForms UserControl that hosts the WPFUserControl

Excel VSTO Add-in with Telerik UI for WPF components

{{ site.framework_name }} Excel VSTO Add-in with Telerik UI for WPF components