LuckyLuke82
LuckyLuke82

Reputation: 606

Microsoft Word Library reference - interference in code

I'm trying to create Microsoft Word documents from my context menu on form, but problem is that when I add reference and Imports statement for Microsoft Word, my form code get's errors. Looks like certain synthax means something else when referencing Word Library. So I need either replacement for these synthaxes or a suggestion of how I could still use Word reference without changing anything in code (I tried Imports statements in module or new class, but that doesn't work I guess). Here are my lines that get's errors:

Dim ptLowerLeft As New Point(-62, btnSender.Height) 'A code for setting position of Listview under button - "Point" is what gets error

 ListView1.View = View.Details 'Changing view of my Listview - "Details" get's error : "Details is not a member of View (Microsoft.Office.Interop.Word.View) 

Reference is Microsoft Word 15.0 Object Library, with Imports Microsoft.Office.Interop.Word statement in my form.

What to do ?

Upvotes: 1

Views: 548

Answers (1)

Dirk Vollmar
Dirk Vollmar

Reputation: 176269

The problem here is that the types Point and View exist both in the Microsoft.Office.Interop.Word and in the System.Windows.Forms or in the System.Drawing namespace. If all of these namespaces are imported the compiler can no longer uniquely identify the type you want to use in your code.

There are two ways to resolve this conflict:

  • Don't import one of the namespace and use the fully qualified name. In your case, you could omit the import of the Microsoft.Office.Interop.Word namespace and then use the full type name when using the interop types.

    Imports System.Drawing
    Imports System.Windows.Forms
    
    ' in your code you can address Word types using the fully-qualified name:
    Dim app as New Microsoft.Office.Interop.Word.Application
    Dim doc as Microsoft.Office.Interop.Word.Document = app.Documents.Add()
    
  • The first option can get messy because you will always have to use the fully qualified type names everywhere. This can be simplified by using a namespace alias:

    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Word = Microsoft.Office.Interop.Word
    
    ' in your code you can address Word types using the alias:
    Dim app as New Word.Application
    Dim doc as Word.Document = app.Documents.Add()
    

Upvotes: 2

Related Questions