In this Walkthrough we will use embedded F# code editor to customize active workbook

Note

You can also use C# and VB.NET to create a .NET customization, but only F# will allow you to run interactive scripts or access Excel named ranges using FCell F# type provider for named ranges.

Create Excel workbook with embedded FCell runtime

  • Click on New Doc in FCell Ribbon:

    New Doc

    You can use an existing workbook as a template or create a blank workbook. If you choose a template, a copy of the workbook will be used.

    Note

    Once you embed FCell runtime in a workbook you will not have access to its VBA Project and any existing VBA code will be removed.

    Note

    You can embed FCell runtime for 32 bit Excel, 64 bit Excel or both. Each runtime will add ca 1.5MB to your workbook.

Run interactive F# scripts

Open F# code editor and type this script

  1. FSharp Script
  2. Run the code and see output below

    FSharp Script Output
  3. Right click the output window and reset the editor

Create F# User Defined Functions:

Create a module with F# function:

  • FSharp Udf Script

Use the function as UDF in Excel

  1. Click Build in FCell Ribbon

  2. Enter the UDF in cell A1:

    FSharp Udf In Cell

Create a new ribbon and custom task pane

Use F# editor to define your ribbon and task pane

  1. Copy this template to F# code editor:

    F# Copy imageCopy
    namespace FCell.Demo
    open System
    open FCell.ManagedXll
    open FCell.Ribbon
    open System.Runtime.InteropServices
    open System.Windows.Forms
    open System.Reflection
    
    //force a new assembly version on each build
    [<assembly: AssemblyVersion("1.0.0.*")>]
    do()
    
    //implement your Custom Ribbon as Windows UserControl:
    [<ComVisible(true)>]
    type MyTaskPane() =
        inherit UserControl()
    
    [<ComVisible(true)>]
    type MyFRibbon() =
        inherit XlRibbon()
    
        //define your Ribbon customization as xml, see Office documentation for details
        override this.GetCustomUI(ribbonID : string) =
            @"
            <customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' onLoad='OnLoad' loadImage='GetImage'>
                <ribbon>
                <tabs>
                    <tab id='MyTabId' label='MyMenu'>
                    <group id='MyGroupId' label='MyGroup'>
                        <button id='MyButtonId' imageMso='PageMenu' label='MyButton'
                        size='large' onAction='OnButtonClick' />
                    </group>
                    </tab>
                </tabs>
                </ribbon>
            </customUI>"
    
        member this.OnButtonClick(control : IRibbonControl) =
            MessageBox.Show("Hello from MyButton") |> ignore
    
        //create Custom Task Pane
        override this.OnCTPFactoryAvailable() =
            let ctp = this.CreateCustomTaskPane(typeof<MyTaskPane>, "MyTaskPane")
            ctp.Visible <- true
    
        //optionally specify code which runs when this workbook opens
        [<XlWorkbookOpen>]
        static member workbookOpen () = MessageBox.Show("Workbook open!")
    
        //optionally specify code which runs when this workbook closes
        [<XlWorkbookClose>]
        static member workbookClose () = MessageBox.Show("Workbook close!")
  2. Click Build in FCell ribbon. You should see your customization:

    FSharp Ribbon

Use #if COMPILED to switch between interactive and non interactive mode.

Open F# code editor and type the code shown below. Then toggle between Compiled and Interactive mode via F#->Is Compiled in code editor menu:

  1. If Compiled
  2. If Interactive