none
'New' Cannot be Used As Interface Error In VB and Excel

    Question

  • Hello All,

    I have the code below:

    Imports Excel = Microsoft.Office.Interop.Excel

    Public Class NOD_Compare

    Public EXL As New Excel.Application

    at the top of my program and I am getting the errors below:


     Warning 1 Namespace or type specified in the Imports 'Microsoft.Office.Interop.Excel' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. K:\LP_Software\NOD_Compare\NOD_Compare\NODComplete.vb 1 9 NOD_Compare

    Error 2 'New' cannot be used on an interface. K:\LP_Software\NOD_Compare\NOD_Compare\NODComplete.vb 5 23 NOD_Compare

    Error 6 'XlDirection' is not a member of 'Excel'. K:\LP_Software\NOD_Compare\NOD_Compare\NODComplete.vb 168 53 NOD_Compare

    My code was working fine until I tried to build the program and these errors came up.  I tried referencing different libraries but I cannot get rid of these errors. All of my code in that of a Window's form.

    Any help would be GREATLY appreciated!!!
    Thanks

    Wednesday, June 11, 2008 11:34 PM

Answers

  •  My thoughts are that you havent added a reference to the Excel object model class Library - result in the Warning and not a member errors.

    How can your code work fine with these compile Errors.   With these errors it wouldnt work at all.

    PS. Is there any reason you are creating an alias for the namespace with the same name as the class.  ie. Imports Excel = Microsoft.Office.Interop.Excel rather than just Imports Microsoft.Office.Interop
    Wednesday, June 11, 2008 11:49 PM
  •  
    Hi NSJAOK.  I have to agree with spotty.  The first error you indicate has the exact wording as that of not having added the reference to the Excel library.  You say you have tried 3 different ones -- can you post the names of the 3 you tried?  Pretty much you want to be looking in the COM tab of the references dialog, under Microsoft Excel.  The 11.0 version is for Office 2003 and the 5.0 version covers most of what's out there earlier than 2003.  However, you might do well downloading the 10.0 Primary Interop Libraries (the XP verion) and using the Browse tab on references to navigate to where you save the files to.

    As for error 2:  you do not want to be creating new instances of the many interfaces in the Excel namespace.  Just stick to the ones that do not start with "I" or an underscore "_".   Maybe this is just the compiler doing the best it can with a bad reference, but if that error is on line 5 and the code you posted is from the top, then it does not look as though you wrote that wrong when instantiating the Application object.

    As for error 3 -- I'll assume you pasted in some code from somewhere else.  You will need to use the more explicit name for xlDirection, something like....

            Excel.XlDirection.xlDown()

    Thursday, June 12, 2008 12:30 AM

All replies

  •  My thoughts are that you havent added a reference to the Excel object model class Library - result in the Warning and not a member errors.

    How can your code work fine with these compile Errors.   With these errors it wouldnt work at all.

    PS. Is there any reason you are creating an alias for the namespace with the same name as the class.  ie. Imports Excel = Microsoft.Office.Interop.Excel rather than just Imports Microsoft.Office.Interop
    Wednesday, June 11, 2008 11:49 PM
  •  
    Hi NSJAOK.  I have to agree with spotty.  The first error you indicate has the exact wording as that of not having added the reference to the Excel library.  You say you have tried 3 different ones -- can you post the names of the 3 you tried?  Pretty much you want to be looking in the COM tab of the references dialog, under Microsoft Excel.  The 11.0 version is for Office 2003 and the 5.0 version covers most of what's out there earlier than 2003.  However, you might do well downloading the 10.0 Primary Interop Libraries (the XP verion) and using the Browse tab on references to navigate to where you save the files to.

    As for error 2:  you do not want to be creating new instances of the many interfaces in the Excel namespace.  Just stick to the ones that do not start with "I" or an underscore "_".   Maybe this is just the compiler doing the best it can with a bad reference, but if that error is on line 5 and the code you posted is from the top, then it does not look as though you wrote that wrong when instantiating the Application object.

    As for error 3 -- I'll assume you pasted in some code from somewhere else.  You will need to use the more explicit name for xlDirection, something like....

            Excel.XlDirection.xlDown()

    Thursday, June 12, 2008 12:30 AM