Excel Column Width

G

Guest

I am creating an excel file and a I am wanting to set a column with to
something, say 9.20. When I run the Macro recorder, I get code which looks
like the following:
Columns("C:C").Select
Selection.ColumnWidth = 8.6

If I create an instance the the Sheet object, I would have expected that I
could just say...

mySheet.Columns["C:C"].Select
mySheet.ColumnWidth = 9.20

But it does not seem to like that. I am also guessing that I should not use
the "Select", as there is no reason to activate a selection, I believe that
this inefficient. What is the proper way to do this... And why is it that
the code above will not work. I am using C#, so I guess I should be using
[] instead of ( ).

Thanks in advance for your assistnace!!!
 
M

Martin Dechev

Hi,

Try this:

using Microsoft.Office.Interop.Excel;

ApplicationClass NewExcelApplication =
new ApplicationClass();
NewExcelApplication.DisplayAlerts = false;
Workbook book =
NewExcelApplication.Workbooks.Open(@"C:\Book1.xls",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Worksheet sheet1 =
(Worksheet)book.Worksheets.get_Item(1);
((Range)sheet1.Columns["C", Type.Missing]).ColumnWidth = 30;
book.Save();
NewExcelApplication.Quit();


Greetings
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top