473,545 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# and Excel Object range

Hello,

I've got a problem with access to special ranges. I renamed some cells (for
example "C4" in "CUSTOM.GENERAL .VPRIM"). This is necessary, because I have to
read out only these cells in active worksheet. The method
get_range("Rang eName", Missing.Value) is ok as far as I have an existing
RangeName.

My program compares the root of a customized XML file with the range of an
Excel file. But some root names don't exist in the Excel file. If I deliver a
wrong RangeName to the get_range() Method I get an Exception. So, I have to
check the existance of a RangeName at first.

Can anybody help me? Info: Excel 11.0 reference.

Many Thanks - Maik
Nov 17 '05 #1
10 26574
Just put a Try...Catch around it. If the range isn't named, you can't use it, correct?

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Maik" <Ma**@discussio ns.microsoft.co m> wrote in message news:35******** *************** ***********@mic rosoft.com...
Hello,

I've got a problem with access to special ranges. I renamed some cells (for
example "C4" in "CUSTOM.GENERAL .VPRIM"). This is necessary, because I have to
read out only these cells in active worksheet. The method
get_range("Rang eName", Missing.Value) is ok as far as I have an existing
RangeName.

My program compares the root of a customized XML file with the range of an
Excel file. But some root names don't exist in the Excel file. If I deliver a
wrong RangeName to the get_range() Method I get an Exception. So, I have to
check the existance of a RangeName at first.

Can anybody help me? Info: Excel 11.0 reference.

Many Thanks - Maik

Nov 17 '05 #2
Hello Dave,

yes, it's correct, but the function delivers min. 20 non-existent ranges to
the get_range method. Is it possible to call the catch handler for 20 times?
Nov 17 '05 #3
You can place the code within a method and take in the parameters that the get_range method takes. I'm assuming that when you say,
"but the function delivers min. 20 non-existent ranges " that you have to check each range using the get_range function seperately
anyway:

public object GetRange(object data)
{
try
{
return excel.get_Range (data);
}
catch (System.Runtime .InteropService s.COMException)
{
return null;
}
}

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Maik" <Ma**@discussio ns.microsoft.co m> wrote in message news:70******** *************** ***********@mic rosoft.com...
Hello Dave,

yes, it's correct, but the function delivers min. 20 non-existent ranges to
the get_range method. Is it possible to call the catch handler for 20 times?

Nov 17 '05 #4
Hello Dave,

thanks for your code. The program runs. But is there any other alternative
to solve the problem. I've heard about a code with VB but the translation to
C# don't run with Office primary interop assemblies.
Nov 17 '05 #5
Hi,

I found a VBA code solving the problem.

Sub FindRangeName()
Dim n As Name
For Each n In ActiveWorkbook. Names
If n.Name = "CUSTOM.GENERAL " Then
MsgBox "Name " & n.Name & " exist, Refer to cell " & _
n.RefersToRange .Address
End If
Next
End Sub

But a translation to C# with Office XP PIA's don't work.
Nov 17 '05 #6
> But a translation to C# with Office XP PIA's don't work.

I've heard this before in a different thread. How is this possible?
A C# Project can't reference an Office XP PIA?

I don't get it. Can you please explain what the issue is?

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Maik" <Ma**@discussio ns.microsoft.co m> wrote in message news:26******** *************** ***********@mic rosoft.com...
Hi,

I found a VBA code solving the problem.

Sub FindRangeName()
Dim n As Name
For Each n In ActiveWorkbook. Names
If n.Name = "CUSTOM.GENERAL " Then
MsgBox "Name " & n.Name & " exist, Refer to cell " & _
n.RefersToRange .Address
End If
Next
End Sub

But a translation to C# with Office XP PIA's don't work.

Nov 17 '05 #7
Hello Dave,
here is a cutout of my code. I work with Office XP PIA at work and with
Office 2003 PIA at home. I will get an
"System.Runtime .InteropService s.COMException" exception if I trie the foreach
loop "foreach(Excel. Name name in workBook.Names) ".
I posted this message also to
<http://www.codeproject .com/csharp/csharp_excel.as p?df=100&forumi d=23997&select= 1093302&msg=109 3302>

I don't know why it isn't working.
Excel.Applicati on excelApp = null;
Excel.Workbook workBook = null;
System.Globaliz ation.CultureIn fo oldCI =
System.Threadin g.Thread.Curren tThread.Current Culture;
object missing = Missing.Value;

private bool StartExcel()
{
try
{
if(excelApp == null)
{
excelApp = new Excel.Applicati on();
excelApp.Visibl e = true;

// Workaround to prevent a known bug
int nId = excelApp.Langua geSettings.get_ LanguageID(
Microsoft.Offic e.Core.MsoAppLa nguageID.msoLan guageIDInstall) ;
System.Threadin g.Thread.Curren tThread.Current Culture =
new System.Globaliz ation.CultureIn fo(nId);
return true;
}
else
{
MessageBox.Show ("Excel is been started");
return false;
}
}
catch (Exception theException)
{
Debug.WriteLine (theException.M essage);
return false;
}
}

private bool OpenWorkbook(st ring filename)
{
try
{
workBook =
excelApp.Workbo oks.Open(filena me,0,false,5,"" ,"",false,Excel .XlPlatform.xlW indows,
"",true,false,0 ,true,false,fal se);
return true;
}
catch
{
Debug.WriteLine ("Could not open Workbook!");
return false;
}
}
void ReadFromSheet()
{
Excel.Worksheet sheet;
Excel.Range range;
sheet = (Excel.Workshee t) workBook.Active Sheet;
string rangeName = "CUSTOM.GENERAL ";

try
{
foreach(Excel.N ame name in workBook.Names)
{
if(name.Name == "CUSTOM.GENERAL ")
{
MessageBox.Show ("Name" + name.Name + "exist, refers to cell" +
name.RefersToRa nge);
range = sheet.get_Range (rangeName,miss ing);
range.Select();
}
}
}
catch(System.Ru ntime.InteropSe rvices.COMExcep tion e)
{
Debug.WriteLine (e.Message);
}
catch(System.Nu llReferenceExce ption e)
{
Debug.WriteLine (e.Message);
}
catch(System.Ex ception e)
{
Debug.WriteLine (e.Message);
}

Nov 17 '05 #8
That's interesting; thank you for the response.

The code you have sent will work as-is when converted directly to VB.NET?

I got it to work for me by making some changes (using a more verbose method):
Excel._Workbook book = app.Application .ActiveWorkbook as Excel._Workbook ;

for (int i = 1; i < book.Names.Coun t; i++)

{

System.Windows. Forms.MessageBo x.Show(book.Nam es.Item(i, Type.Missing, Type.Missing).N ame);

}
--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"Maik" <Ma**@discussio ns.microsoft.co m> wrote in message news:F1******** *************** ***********@mic rosoft.com...
Hello Dave,
here is a cutout of my code. I work with Office XP PIA at work and with
Office 2003 PIA at home. I will get an
"System.Runtime .InteropService s.COMException" exception if I trie the foreach
loop "foreach(Excel. Name name in workBook.Names) ".
I posted this message also to
<http://www.codeproject .com/csharp/csharp_excel.as p?df=100&forumi d=23997&select= 1093302&msg=109 3302>

I don't know why it isn't working.
Excel.Applicati on excelApp = null;
Excel.Workbook workBook = null;
System.Globaliz ation.CultureIn fo oldCI =
System.Threadin g.Thread.Curren tThread.Current Culture;
object missing = Missing.Value;

private bool StartExcel()
{
try
{
if(excelApp == null)
{
excelApp = new Excel.Applicati on();
excelApp.Visibl e = true;

// Workaround to prevent a known bug
int nId = excelApp.Langua geSettings.get_ LanguageID(
Microsoft.Offic e.Core.MsoAppLa nguageID.msoLan guageIDInstall) ;
System.Threadin g.Thread.Curren tThread.Current Culture =
new System.Globaliz ation.CultureIn fo(nId);
return true;
}
else
{
MessageBox.Show ("Excel is been started");
return false;
}
}
catch (Exception theException)
{
Debug.WriteLine (theException.M essage);
return false;
}
}

private bool OpenWorkbook(st ring filename)
{
try
{
workBook =
excelApp.Workbo oks.Open(filena me,0,false,5,"" ,"",false,Excel .XlPlatform.xlW indows,
"",true,false,0 ,true,false,fal se);
return true;
}
catch
{
Debug.WriteLine ("Could not open Workbook!");
return false;
}
}
void ReadFromSheet()
{
Excel.Worksheet sheet;
Excel.Range range;
sheet = (Excel.Workshee t) workBook.Active Sheet;
string rangeName = "CUSTOM.GENERAL ";

try
{
foreach(Excel.N ame name in workBook.Names)
{
if(name.Name == "CUSTOM.GENERAL ")
{
MessageBox.Show ("Name" + name.Name + "exist, refers to cell" +
name.RefersToRa nge);
range = sheet.get_Range (rangeName,miss ing);
range.Select();
}
}
}
catch(System.Ru ntime.InteropSe rvices.COMExcep tion e)
{
Debug.WriteLine (e.Message);
}
catch(System.Nu llReferenceExce ption e)
{
Debug.WriteLine (e.Message);
}
catch(System.Ex ception e)
{
Debug.WriteLine (e.Message);
}

Nov 17 '05 #9
I haven't translated it to VB.NET. I only searched in other forums for code.
But all forums shows working code in VB. I translated this VB code for myself
in hope that it will be running. But it doesn't run.
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7010
by: Hema S | last post by:
Hi, I want to use excel object in my asp.net application. When excel application is installed its working fine. When i uninstal the excel application and refer the relevant dll explicitly... i am getting excel9.olb class id not found. How to register this excel9.olb library. Thanks in advance.
2
1806
by: Darren Barrick via .NET 247 | last post by:
Hello All! I am using Office Automation in one of my applications in orderto read/write to an excel spreadsheet from a VB.Net application. I thus create the application with ExcelApplication = New Excel.Application() I wish to keep my application hidden, and thus I use
3
7995
by: Nick Douglas | last post by:
Hello, I'd like to programmatically reference the Excel Object Library from an Access form using VB. I am using this code: Application.VBE.vbprojects("VBAProject").References.AddFromFile "(file location)" ....but get a 'subscript out of range' message. Can anyone help?
3
8874
by: Bart Filipski | last post by:
Hello, Does anyone know how looks the syntax of SORT method in Excel object ( i mean precisely in Excel.Workbook.Worksheet.Cells.Sort(...) ) I have tryied in VBA and it works great, but I don't know how to build a Keys for this method in C# and how to pass this arguments in this method. Any example howto use this method would be nice....
3
6417
by: eye5600 | last post by:
I want to read/write Excel files but I am having trouble getting started. Using MS article 302084 as a cookbook, I try to add the reference for MS Excel Object Library. Two versions are listed, 5 & 11. If I use 11, then then all references to Excel fail. If I use 5, the next step in the example fails: Excel.Application oXL = new...
4
4351
by: Norton | last post by:
I type the following code to open/close an excel appz Dim oExcel As Excel.Application oExcel = new Excel.Application .... .... ... If Not oExcel Is Nothing Then oExcel.Quit() If Not oExcel Is Nothing Then oExcel = Nothing
4
1758
by: Mr.Doubt | last post by:
I've a widows application, that makes use of an EXCEL COM object, to run a few macros available in a EXCEL file. The object is created when the app is started and is closed when the app is closed. The app also has a OLEDB connection to the same file to fetch data from the file and display it on a datagrid. Now my trouble is that, when I...
3
7155
by: rlntemp-gng | last post by:
RE: Access 2003/Excel 2003 Problem: After I close the Access application completely, I go out to the Task Manager and there is an Excel.exe object still sitting out there. My Access application creates an Excel file (MyTestFile.XLS) just fine per the code submitted here. This code closes the Excel file created and close the Excel...
1
1372
by: Scott M. | last post by:
Many methods return objects when they are called. With Excel these objects are placed in memory and must be destroyed via ReleaseComObject as you have done with your NAR method, but the line: objExcel.Workbooks.Open(Page.MapPath("reports\BLCost.xls")) could be the culprit since the Open method returns a reference to a Workbook object that...
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.