What is the difference between Excel.Dialogs and ExcelApp.Dialogs?

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

What is the difference between Excel.Dialogs and ExcelApp.Dialogs?
ADX blog: Working with Excel cell values, formulas and formatting 
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Hi,

I am studying this part of a Blog sample:
Working with Excel cell values, formulas and formatting
Paragraph: Displaying the Insert Function dialog
https://www.add-in-express.com/creating-addins-blog/2013/10/15/excel-cell-values-formulas-formatting-csharp/#Displaying_the_Insert

The code is in C#, but I translated it to VB.Net
Private Sub showInsertFunctionRibbonButton_OnClick(
          ByVal sender As System.Object, _
          ByVal control As AddinExpress.MSO.IRibbonControl, _
          ByVal pressed As System.Boolean) _
          Handles showInsertFunctionRibbonButton.OnClick

        Dim dialogs As Excel.Dialogs = Nothing                 '????
        Dim insertFunctionDialog As Excel.Dialog = Nothing

        Try
            dialogs = ExcelApp.Dialogs                          '????
            insertFunctionDialog = dialogs(Excel.XlBuiltInDialog.xlDialogFunctionWizard)
            insertFunctionDialog.Show()

        Finally
            If insertFunctionDialog IsNot Nothing Then _ 
                Marshal.ReleaseComObject(insertFunctionDialog)

            If dialogs IsNot Nothing Then Marshal.ReleaseComObject(dialogs)

        End Try

    End Sub



I notice that the property Dialogs appears in 2 different namespaces:
As ExcelApp.Dialogs and also as
Excel.Dialogs

What is the difference between these two?


If I Go To Definition, I find that they are defined slightly differently:
----
In ExcelApp.Dialogs, Dialogs is defined:
As Microsoft.Office.Interop.Excel.Dialogs
Member of Microsoft.Office.Interop.Excel.Application
----
In Excel.Dialogs, Dialogs is defined:
As Public Interface Dialogs
Member of Microsoft.Office.Interop.Excel
----



I find many other examples of properties sometimes defined in ExcelApp, sometimes in Excel.
So, there must be a very important difference between the two. But it's not very clear to me.

red


Thanks
Leon
Posted 18 Mar, 2019 08:57:02 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Leon,

Every Office application has a very long history and you must be prepared: all of them were combined in Office quite late so despite the unified UI they demonstrate significant difference and inconsistency in the programming model.

Excel and Word have built-in dialogs. PowerPoint have traces of built-in dialogs; have never used these.

In Excel you refer to a built-in dialog using using Application.Dialogs(Excel.XlBuiltInDialog.{enum item here}). To find details, I recommend that you open the VBA Object Browser (Alt+F11 to open the VBA IDE, in the IDE press F2 to open the Object Browser), select one of these and press F1 for help reference: the Application.Dialogs property, the Dialogs object, the Dialog object.

Also note that all(?) Office applications provide the FileDialog property on the Application object. You use the FileDialog object (defined on the Microsoft.Office.Core namespace since Office 2002) that such a property returns to show dialogs listed in the Microsoft.Office.Core.MsoFileDialogType enumeration; see https://docs.microsoft.com/en-us/office/vba/api/office.msofiledialogtype.

Leon Lai Kan writes:
I find many other examples of properties sometimes defined in ExcelApp, sometimes in Excel.


A property can *only* be declared on an object, such as Excel.Application. Every property returns a value of a certain type; the the property name *often* is the same as the type name. Pay attention to icons on these names; there are icons for properties, and icons for classes, etc.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Mar, 2019 04:19:46 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Hi, Andrei

Thanks a lot for your reply.

Still, I'm still a bit confused, and it may take some time before I grasp everything clearly.

Could you give me a rough rule of thumb to answer this question:
When should we use the Dialogs which is in the namespace Microsoft.Office.Interop.Excel.Application

and when should we use the Dialogs which is in the namespace Microsoft.Office.Interop.Excel?

I use Dialogs as example, but my question is valid for all other objects.
----
In Excel you refer to a built-in dialog using using Application.Dialogs

Then what do we use Microsoft.Office.Interop.Excel.Dialogs for?

----



I read a few books on VSTO, and I learn that an object in Excel can exist as a Native object, an Interop object, or a Host Object (also called a VSTO object).

Do these concepts have any relation or relevance to the question I am asking?


Best Regards,
Leon
Posted 19 Mar, 2019 04:44:46 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Leon Lai Kan writes:
Could you give me a rough rule of thumb to answer this question: When should we use the Dialogs which is in the namespace Microsoft.Office.Interop.Excel.Application
and when should we use the Dialogs which is in the namespace Microsoft.Office.Interop.Excel?


There's no Microsoft.Office.Interop.Excel.Application namespace; you only have the Microsoft.Office.Interop.Excel namespace; see the corresponding Imports statement at the top of you file.

That namespace contains a number of classes and the Application (=Microsoft.Office.Interop.Excel.Application) class is one of them. That class provides a property called Dialogs. That property returns a value of the Dialogs (=Microsoft.Office.Interop.Excel.Dialogs) type.

Summarizing, a namespace holds classes, a class holds properties and methods (collectively, these are called members), a method may have parameters and return value.

Leon Lai Kan writes:
I read a few books on VSTO, and I learn that an object in Excel can exist as a Native object, an Interop object, or a Host Object (also called a VSTO object).


I can't explain about what you/they call native or interop objects. For me, an interop is a table mapping a name to a shift in the type library. When used in the source code, the interop allows Intellisense provide you with a list of class/member/attribute names available on an interop/class/member. Once again, these are just names from the interop assemblies; these names however have the shift associated with them and the compiler retrieves the shift and embeds it in the binary code. When your add-in executes, the processor invokes a method (loaded the memory, RAM) using the location (address in RAM) of the type library plus the shift. So, for me there's no interop objects. Still, if you want to understand how .NET manages COM objects (all objects in the Excel object model are COM objects), check https://www.add-in-express.com/creating-addins-blog/2011/11/04/why-doesnt-excel-quit/. I assume native objects and interop objects may mean the COM object and RCW (see that blog).

As to the VSTO object, this belongs to the VSTO concept. In add-intion to application-level add-ins, they've allowed creating document-level add-ins (Add-in Express creates COM add-ins; these are application-level add-ins only). To do this, you use objects wrapping Office documents - Excel worksheet and Word.Document. Alas, I don't know many details about VSTO. If you are interested, you should google for them.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Mar, 2019 07:02:55 Top
Leon Lai Kan




Posts: 200
Joined: 2018-12-20
Thanks Andrei for all your great support!

You see, I am learning a lot from your samples!

I am only reading VSTO books because I can still learn a lot about Excel programming (for use with ADX) from them.



Best Regards,
Leon
Posted 19 Mar, 2019 07:17:28 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Welcome!

Will respond to your other message tomorrow.


Andrei Smolin
Add-in Express Team Leader
Posted 19 Mar, 2019 10:21:36 Top