Search code examples
vb.netvisual-studiovscode-debuggerimmediate-mode

Can't access class members of Word class in immediate mode


I am writing a vb.net desktop application which automates ms-word. Defining variables of types taken from the word object model, I cannot access member elements of those classes in immediate mode, when debugging. I CAN access them from my code, when it is running.

Private Sub BlackOut(aRange As Word.Range, CharLimit As Integer)

  MsgBox(aRange.Characters.Count) 'This works

End Sub

Running the Code and accessing aRange.Characters.Count works just fine. When setting a breakpoint and typing the line ? aRance.Characters.Count in the Immediate Window, however, I get the error message:

error BC30456: 'Characters' is not a member of 'Range'.

Strangely, typing ? aRange.Text works in the Immediate Window.

I haven't encountered that before. Can anyone help me out of this?


Solution

  • This is due to the "Embed Interop Types" setting on the Word Interop Assemblyreferences (Interop.Microsoft.Office.Core, Interop.Microsoft.Office.Interop.Word, and Interop.VBIDE) being set to True. When this setting is True, the compiler extracts only the type definitions used in your code and embeds it into your program. This is useful in that it eliminates the need to include these Dll's along with your program, but makes it difficult to "edit and continue" during debug as the full type information is no longer available to the debug session. The solution is to not embed the assemblies while you are in development.

    From the Solution Explorer, click on the "Show All Files" button such that the "References" node is displayed. Click on the node to expand it. Hold down the Control Key and select the three files as show in the following image.

    Word Interop Files

    With those files selected, press the "F4" key to show their properties.

    Assembly Properties

    Change the "Embed Interop Types" to False (doing this should also change "Copy Local" to True.

    When you are ready to publish your application, change those properties back.