This code example shows how to include WPF controls and forms in a Word Office Ribbonbar / Menu application.
A VSTO Addin for Word is created and then a WPF User Control is inserted into the project.
By changing the entry: UserControl to Window, the WPF Control is fully embedded in the Word Ribbonbar application.
Add WPF UserContro
Then add a WPF UserControl to the VSTO application
Under Project-> Add-> New Item-> WPF-> User Control (WPF)
Change UserControl to Window
In the WPF UserControl you change the <UserControl> tag to <Window>
Rename UserControl to Window
<Window x:Class="wpf_in_Word_addin.FormWPF" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:wpf_in_Word_addin" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid Background="Red"> <Button Content="Button" HorizontalAlignment="Left" Margin="105,123,0,0" VerticalAlignment="Top" Width="201" Height="194"/>
<Label Content="This is a WPF FORM or UserControl" HorizontalAlignment="Left" Margin="67,43,0,0" VerticalAlignment="Top" Height="42" Width="71"/>
</Grid> </Window>
|
C # Code Class UserControl to Window
In the codebehind file formWPF.xaml.cs you also change: UserControl to: Window
using System.Windows;
namespace wpf_in_Word_addin { //*Change UserControl to myForm : Window public partial class FormWPF : Window { public FormWPF() { InitializeComponent(); } } }
|
Open WPF mold
Then you can open the WPF form in the Office application in the Button-Click event of the button in the ribbon bar
With WPFform.show ()
using Microsoft.Office.Tools.Ribbon;
namespace wpf_in_Word_addin { public partial class Ribbon1 { private void Ribbon1_Load(object sender, RibbonUIEventArgs e) {
}
private void button1_Click(object sender, RibbonControlEventArgs e) { //--< open WPF Form >-- FormWPF form = new FormWPF(); form.Show(); //--</ open WPF Form >-- } } }
|
Tutorial in Youtube
Subject:
MS Word, Excel Access Outlook PowerPoint
VSTO Ribbonbar Addin Addins Interop Applications
Office 2013 2016 Office 365