Link to home
Start Free TrialLog in
Avatar of ElrondCT
ElrondCTFlag for United States of America

asked on

Excel references in VB

I've recently upgraded my computer from Win XP to Win7, and reinstalled VB 2008 on the new system. I'm having problems with my VB code that does automation with Excel. Even though I've reset the reference to Excel, I'm getting "Name 'Excel' is not declared" errors, unless I give a command like

Imports Excel = Microsoft.Office.Interop.Excel

That seems odd, but it's manageable. More seriously, though, a number of Excel constants are now giving errors. I've had code like:

                    .Range("B4").HorizontalAlignment = Excel.Constants.xlLeft

But this is now giving me the error 'Constants' is ambiguous in the namespace 'Microsoft.Office.Interop.Excel'. Intellisense is not even showing Constants as a class in the Excel namespace.

I suspect the issue may be related to moving to a new version of Excel. In XP, I was using Office 10.0 as the reference for my code (with late binding, so the software will connect properly with whichever version of Excel is actually installed on the customer's computer). But Office XP isn't supported on Win7, so I've installed Office 2007 and am using the reference to Office 11.0. (Both 11.0 and 12.0 were listed as options in the Reference box, and I chose the lower-numbered one hoping for more compatibility. I haven't been able to figure out how to delete that reference so I can try 12.0 instead.) I'm wondering if Excel may have restructured its constants. However, if I go into the VBA windows of Excel 2007 and search for xlLeft in the Object Browser, it's listed as a member of the Constants class, which is itself a member of Excel. So the reference seems to be correct, but it's not working.

What do I need to do to use standard Excel constants for settings while automating VB/Excel interaction?
Avatar of x77
x77
Flag of Spain image

On Vb project  propertyes , go to References Tab.

Then revise imported namespaces and include Microsoft.office.interop.Excel

Note that you can also include

   Imports Excel = Microsoft.Office.Interop.Excel

On each code file that requires it.
SOLUTION
Avatar of Ron Malmstead
Ron Malmstead
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ElrondCT

ASKER

It looks like the problem was that I had created a reference to the .NET component Microsoft.Office.Interop.Excel. I shouldn't have done that; I needed to use only the COM component Microsoft Excl 12.0 Object Library. The References tab of the project helped me discover that; once I eliminated the .NET component, all my error messages disappeared.