Solved

Excel references in VB

Posted on 2011-09-02
4
1,315 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:ElrondCT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36474398
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.
0
 
LVL 25

Assisted Solution

by:Ron Malmstead
Ron Malmstead earned 200 total points
ID: 36474425
In my project I have ....

Imports Microsoft.Office.Interop
...at top.

Then for references in project properties I have these listed...
Microsoft Excel 11.0 Object Library   - COM - 1.5.0.0
Microsoft Office 11.0 Object Library - COM - 2.3.0.0

I'm on vs2010 w/.net 4, on Win7
Office 2003 is installed as well.


0
 
LVL 15

Accepted Solution

by:
x77 earned 300 total points
ID: 36475034
You can set a namespace on Vb.Net visible to all code files in the project using "Imported Namespaces"

Do it at Project Properties, References Tab in the inferior pane.
references.png
0
 
LVL 20

Author Comment

by:ElrondCT
ID: 36475430
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question