Home > OS >  Excel JavaScript workbook open event
Excel JavaScript workbook open event

Time:12-14

Is there any way to have some code in an Excel JavaScript add-in that runs when a workbook is opened? It looks like there is no workbook open event and onActivated event specifies that it doesn't run when workbook is opened.

CodePudding user response:

For my Dev purposes I have a workbook that auto loads my Add-In, but its not the same way that VBA would normally work. You need to run the Add-In once and save a workbook to have it set to automatically load a Add-In.

So this is my workflow for having a workbook which automatically loads my code each time.

I first load my add-in on a new workbook and have the following code.

const Dev_Mode = true

Office.onReady(async function (info) {
    console.log('commands.js:Office.onReady')
    if (Dev_Mode == true) {
        Office.addin.setStartupBehavior(Office.StartupBehavior.load);
        //Office.addin.showAsTaskpane()
    }
});

Then I save that workbook. I am then able to edit my code and I have the following that I use:

const Dev_Mode = true

Office.onReady(async function (info) {
    console.log('commands.js:Office.onReady')
    if (Dev_Mode == true) {
        //Office.addin.setStartupBehavior(Office.StartupBehavior.load);
        CommandsFunc(event)
    }
});

Basically, even though users might have your Add-In installed, it doesn't "load" automatically with each new workbook. There is no way around this. Best bet would be to distribute a workbook that is setup to have it automatically load the Add-In by following the above.

CodePudding user response:

One can add code that runs every time when an workbook is opened in Office.onReady event: https://learn.microsoft.com/en-us/office/dev/add-ins/develop/run-code-on-document-open

  • Related