how to get worbookname in adxExcelAppEvents_WorkbookActivate event

Add-in Express™ Support Service
That's what is more important than anything else

how to get worbookname in adxExcelAppEvents_WorkbookActivate event
 
Pavel


Guest


Hello, looking for most direct way of getting what was the workbook name that has been just activated during processing of the event handler adxExcelAppEvents1_WorkbookActivate(object sender, object hostObj)
I am using C#.

Thanks Pavel
Posted 09 May, 2018 11:20:54 Top
Andrei Smolin


Add-in Express team


Posts: 19138
Joined: 2006-05-11
Hello Pavel,

You can cast hostObj to Excel.Workbook and call Workbook.Name or Workbook.FullName.

Does this work?


Andrei Smolin
Add-in Express Team Leader
Posted 10 May, 2018 00:34:47 Top
Pavel


Guest


Hello Andrei,

That's what I tried before I posted the topic to forum I but i am getting:

'(Microsoft.Office.Interop.Excel.Workbook)hostObj' threw an exception of type 'System.InvalidCastException' Microsoft.Office.Interop.Excel.Workbook {System.InvalidCastException}

Pavel
Posted 10 May, 2018 02:54:18 Top
Andrei Smolin


Add-in Express team


Posts: 19138
Joined: 2006-05-11
Hello Pavel,

Below is the complete code of the add-in module of my test add-in that works as expected:

using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;
using AddinExpress.MSO;
using Excel = Microsoft.Office.Interop.Excel;

namespace MyAddin95
{
    /// <summary>
    ///   Add-in Express Add-in Module
    /// </summary>
    [GuidAttribute("2CCD50A8-4C94-4A98-BA1C-34DBEB4654AB"), ProgId("MyAddin95.AddinModule")]
    public partial class AddinModule : AddinExpress.MSO.ADXAddinModule
    {
        public AddinModule()
        {
            Application.EnableVisualStyles();
            InitializeComponent();
            // Please add any initialization code to the AddinInitialize event handler
        }
 
        #region Add-in Express automatic code
 
        // Required by Add-in Express - do not modify
        // the methods within this region
 
        public override System.ComponentModel.IContainer GetContainer()
        {
            if (components == null)
                components = new System.ComponentModel.Container();
            return components;
        }
 
        [ComRegisterFunctionAttribute]
        public static void AddinRegister(Type t)
        {
            AddinExpress.MSO.ADXAddinModule.ADXRegister(t);
        }
 
        [ComUnregisterFunctionAttribute]
        public static void AddinUnregister(Type t)
        {
            AddinExpress.MSO.ADXAddinModule.ADXUnregister(t);
        }
 
        public override void UninstallControls()
        {
            base.UninstallControls();
        }

        #endregion

        public static new AddinModule CurrentInstance 
        {
            get
            {
                return AddinExpress.MSO.ADXAddinModule.CurrentInstance as AddinModule;
            }
        }

        public Excel._Application ExcelApp
        {
            get
            {
                return (HostApplication as Excel._Application);
            }
        }

        private void adxExcelAppEvents1_WorkbookActivate(object sender, object hostObj) {
            Excel.Workbook wbk = hostObj as Excel.Workbook;
            System.Diagnostics.Debug.WriteLine("!!! adxExcelAppEvents1_WorkbookActivate. " + wbk.FullName);
        }
    }
}



Andrei Smolin
Add-in Express Team Leader
Posted 10 May, 2018 04:52:52 Top
Pavel


Guest


Hello Andrei,
When using as instead of casting i am getting null instead of casting exception.
There must be some other problem.
Any idea ?


BR

Pavel
Posted 10 May, 2018 05:27:16 Top
Andrei Smolin


Add-in Express team


Posts: 19138
Joined: 2006-05-11
Pavel,

This is my sample project:
http://temp.add-in-express.com/support/MyAddin95-ForPavel.zip.


Andrei Smolin
Add-in Express Team Leader
Posted 10 May, 2018 05:39:33 Top
Pavel


Guest


Hello Andrei,

Thanks it works. I tried the casting using as only in VS studio watch and there it was reporting null.

One more question regarding casting the event args IS there a way how to tell to which type i can cast the hostObj / sender parameters in any of the adx app events. I mean it makes sense that in the case of the workbook activate one should cast to workbook. But what about other events , is there some documentation ? I tried to find this in the "Add-in Express .NET.chm" but with no success.

Thanks a lot

Pavel
Posted 10 May, 2018 06:28:26 Top
Andrei Smolin


Add-in Express team


Posts: 19138
Joined: 2006-05-11
Pavel,

Yes, correct, you can guess what type is returned. Also, you can start Excel, open the VBA IDE (Alt+F11), open the VBA Object browser, locate the WorkbookActivate event and find the type of the COM object passed to the event handler. Finally, you can find that type programmatically: add a reference to Microsoft.VisualBasic.DLL and use

private void whatType(object obj) // thank you, Matt!
{
    System.Diagnostics.Debug.WriteLine(Microsoft.VisualBasic.Information.TypeName(obj));
}


In your case, this method should print "Workbook".


Andrei Smolin
Add-in Express Team Leader
Posted 11 May, 2018 03:32:58 Top
Pavel


Guest


Hello Andrei,

thanks a lot ! :-)

BR

Pavel
Posted 11 May, 2018 04:34:48 Top
Andrei Smolin


Add-in Express team


Posts: 19138
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 11 May, 2018 05:02:01 Top