.NET 5 VB Late bound variables unavailable at run time due to Public member 'Visible' on type 'Application' not found

I've searched the forums for something similar to this, but most of the issues I've seen do not have access to the variables in the debugger and have issues with casting the object.

I'm running a C# application which calls a VB class called wrapper.cs, which uses MS Word objects. This code was tried and tested and worked fine with .NET 4.8 but now breaks in .NET 5.

I have a variable: Protected m_wdApp As Object

When the class is instantiated, it calls:

 Public Sub New() m_wdApp = CreateObject("Word.Application") m_wdApp.Visible = False m_outputFormat = DOC_FORMAT End Sub 

This throws the following exception:enter image description here

Now the above would be all very fine and well, if I wasn't able to actually look at the contents of the m_wrdApp and see that yes, they are all intact and are all visible in my watch Windows.

enter image description hereUsing the Immediate window, attempting to access any of these attributes from the Word object throws an error (as you'd expect).

However, if I ask VS to "add a watch" to one of the attributes, for instance this "Visible" which we currently need, it comes out with this convoluted watch name:enter image description here

I should note that all of this worked fine before upgrading to .NET 5, so I'm wondering if this is either a bug or there are some new rules for dealing with late bound variables.

4

1 Answer

As @dbasnett and @charlieface mentioned above, we should be using Interop.

We added Imports Microsoft.Office.Interop

We also changed

Protected m_wdApp As Object

to...

Protected m_wdApp As Word.Application

And needed to update variables such as

Dim inlineShape As Object 

to

Dim inlineShape As Word.InlineShape

and...

For Each hl As Object In m_wdApp.ActiveDocument.Hyperlinks

to:

For Each hl As Word.Hyperlink In m_wdApp.ActiveDocument.Hyperlinks

I couldn't see why moving away from late binding would impact us and I'm assuming the move the Interop is in line with .NET 5 coding guidelines, with their many improvements in Interop code in recent years.

Many thanks for this tip guys!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like