Hi,
I've a big problem! Reading newsgroup or article give me a solution
that does not work.
I just need to merge two cells in Excel. As it didn't work, I create
just a little program that do the following:
Excel.Application excelApp = null;
Excel.Workbooks books = null;
Excel.Workbook book = null;
Excel.Worksheet sheet = null;
Excel.Range excelCells = null;
try
{
excelApp = new Excel.ApplicationClass();
books = excelApp.Workbooks;
book = books.Open("test.xls",Type.Missing, false, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
sheet = (Excel.Worksheet) book.ActiveSheet;
excelCells = (Excel.Range)sheet.get_Range("A1" , "B1");
excelCells.Merge(Type.Missing);
book.SaveAs("test.xls", Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlShared,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
}
catch
{
throw;
}
It raised no exception and my cells are not merged!
Can anyone has a solution?
Thanks,
Tanguy
RobinS - 04 Jan 2007 23:56 GMT
This will work in VB. (I figured you'd rather have a VB response than
no response at all.)
Dim xlRange as Excel.Range = _
CType(xlSheet.Range(xlSheet.Cells(1,1), _
XlSheet.Cells(2,1)), Excel.Range)
xlRange.MergeCells = True
'you *must* uninstantiate your excel range objects or excel
' will still be listed as a running process after the user
' closes Excel
xlRange = Nothing
I think this is the C# version, but am no C# expert,
which is why I posted the VB code above.
Excel.Range xlRange =
Excel.Range(xlSheet.Range(xlSheet,Cells(1,1),
xlSheet.Cells(2,1)));
xlRange.MergeCells;
//you *must* uninstantiate your excel range objects or excel
// will still be listed as a running process after the user
// closes Excel
xlRange = Null;
I have noticed that doing OLE Automation with Excel and having Option
Strict On requires me to cast my excel objects to the right type *a*
*lot*.
Good luck.
Robin S.
-------------------------------
> Hi,
>
[quoted text clipped - 42 lines]
>
> Tanguy
Tanguy92 - 05 Jan 2007 08:15 GMT
Thanks but you tell me to use myRange.MergeCells = true whereas
MergeCells is a property that just indicates if the range contains
merged cells. It does not make the action to merge the cells.
Thanks anyway but it's not the response I need
Cindy M. - 05 Jan 2007 13:29 GMT
Hi Tanguy92,
> Thanks but you tell me to use myRange.MergeCells = true whereas
> MergeCells is a property that just indicates if the range contains
> merged cells. It does not make the action to merge the cells.
See the MERGE method of the Range object.
Please note that the best place to get help about what object,
properties and methods to use to perform a task is the
excel.programming newsgroup. You'll most likely get VBA code, there,
but that's easily "translated" to a .NET language. If you have
problems doing that, then the office.developer.automation newsgroup
is the right place to ask.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :-)
RobinS - 05 Jan 2007 21:51 GMT
It's a read/write property. It worked for me; I tried it out
before I posted it. Did you try it out?
Robin S.
------------------------
> Thanks but you tell me to use myRange.MergeCells = true whereas
> MergeCells is a property that just indicates if the range contains
> merged cells. It does not make the action to merge the cells.
>
> Thanks anyway but it's not the response I need