I've got two .net-6 console-apps:
one in C#
using Excel = Microsoft.Office.Interop.Excel;
Console.WriteLine("Hello, World!");
var xl = new Excel.Application();
var wb = xl.Workbooks.Open(@"c:\temp\test-2.xlsx");
wb.Close(0);
xl.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);
wb = null;
xl = null;
the other one in VB.NET.
Imports System
Imports Excel = Microsoft.Office.Interop.Excel
Module Program
Sub Main(args As String())
Console.WriteLine("Hello World!")
Dim xl = New Excel.Application()
Dim wb = xl.Workbooks.Open("c:\\temp\\test-2.xlsx")
wb.Close(0)
xl.Quit()
'System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(xl)
'wb = Nothing
'xl = Nothing
End Sub
End Module
The project configuration is in both consoles is the same:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>OfficeInteropTest</RootNamespace>
</PropertyGroup>
<ItemGroup>
<COMReference Include="Microsoft.Office.Interop.Excel">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>9</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>00020813-0000-0000-c000-000000000046</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
</Project>
When I run each of them the C# app does not close Excel after the console exits while the VB.NET app does even though I commented out the last four lines.
So, I was wondering what do they do differently and why does it look like VB.NET is better with Office interop?