0

To create an instance of excel using VB.Net, we can write the following piece of code:

Imports System
Imports Microsoft.Office.Interop.Excel
Imports Word = Microsoft.Office.Interop.Word

Module Program
    'declaration statements
    Dim oXL As Microsoft.Office.Interop.Excel.Application
    Dim oWB As Workbook
    Dim oSheet As Worksheet
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Sub Main(args As String())
        'Console.WriteLine("Hello World!")
        oXL = CreateObject("Excel.Application")
        oXL.Visible = True
        oXL.DisplayAlerts = True
        Console.WriteLine("Type returned by CreateObject: {0}", oXL.GetType())
        oWB = oXL.Workbooks.Add

        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add
        Console.ReadLine()
        oXL.Quit()          'Not getting the warning
        oWord.Quit(False)   'Not getting the warning
    End Sub
End Module

Till here all is good. However I also came across the following statement in the MSDN Link:

Use the Application property to return the Application object.

I checked and found that the APPLICATION property is a member of _Application interface, the usage criteria of which as per this link is mentioned below.

Use this primary interface only when the method you want to use shares the same name as an event of the COM object

However this statement is not clear to me. So can someone please explain (if possible with an example) when should I use the Application property of the _Application interface to return an Application object? Also is the object returned by the Application property any different from the one being returned by createObject in the statement oXL = CreateObject("Excel.Application")?

Sougata
  • 319
  • 1
  • 10
  • It is a fanciful explanation, but unlikely to be accurate when the object model was designed long, long before .NET was envisioned. There is a [kernel of truth](https://stackoverflow.com/questions/8303969/how-to-eliminate-warning-about-ambiguity) to it, but imo they designed the property to make it easy to, say, pass a WorkSheet argument to a method and still easily get a reference to add a new sheet. – Hans Passant Dec 06 '21 at 16:07
  • @HansPassant I went through the link that you mentioned. Also found another link on the same topic: https://stackoverflow.com/questions/40766117/what-is-the-difference-between-application-and-application. However I am not able to view the warning at my end - i have updated the code in my question. Why is it so? Has the conflict been resolved in newer versions of VB.net or am I missing anything? – Sougata Dec 14 '21 at 07:54
  • @HansPassant by warning I mean the warning mentioned in both question threads arising because the compiler seems to be warning that there are two QUIT entities, one a method and the other an event. – Sougata Dec 14 '21 at 08:01
  • You are using the interfaces, no ambiguity. The OP didn't document their problem well and probably used the C# equivalent of Dim oWord As Word.ApplicationClass. I don't know if the modern Roslyn-based C# compiler still generates this warning, don't have Office installed anymore. – Hans Passant Dec 14 '21 at 08:55

0 Answers0