Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel.Interop Not Saving Column Width Changes
Hi,
I need to open an Excel file using Interop, resize a column and then save the file, insuring that the worksheet retains the newly resized column width. As I step through the code all is working fine. But when I reopen the file the new column width has not been saved. If I open the file manually, resize the column, then save, the file retains the new column width. Any suggestions? I should also add that I tried reading the column width after the AutoFit was called and then explicitly set the column width to that value, then I saved the file This also did not work. Here is the code I used. I have done this in Winforms. Thanks for your help. Mark using Excel=Microsoft.Office.Interop.Excel; namespace ExcelWindowsTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, System.EventArgs e) { Excel.Application excel; Excel.Workbook book; Excel.Worksheet sheet; Excel.Range range; try { excel = new Excel.Application(); excel.Visible = true; book = (Excel.Workbook)(excel.Workbooks.Add(@"C:\Document s and Settings\Mark\My Documents\Visual Studio 2005\Projects\ExcelWindowsTest\ExcelWindowsTest\Po rtDeliveryWorkBook.xls")); excel.DisplayAlerts = false; sheet = (Excel.Worksheet)book.ActiveSheet; range = (Excel.Range)sheet.Columns["A", Type.Missing]; range.AutoFit(); book.Save(); excel.Quit(); } catch (Exception theException) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } } -- Thanks |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel.Interop Not Saving Column Width Changes
Not sure if it will help but try:
range.EntireColumn.AutoFit Also I don't know if it matters that you are using an Excel reserved name "Range" as a variable name, but I'd try changing it to "Rng" or something (also maybe for "Excel" and "Sheet"). hth, Doug "Geomar" wrote in message ... Hi, I need to open an Excel file using Interop, resize a column and then save the file, insuring that the worksheet retains the newly resized column width. As I step through the code all is working fine. But when I reopen the file the new column width has not been saved. If I open the file manually, resize the column, then save, the file retains the new column width. Any suggestions? I should also add that I tried reading the column width after the AutoFit was called and then explicitly set the column width to that value, then I saved the file This also did not work. Here is the code I used. I have done this in Winforms. Thanks for your help. Mark using Excel=Microsoft.Office.Interop.Excel; namespace ExcelWindowsTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, System.EventArgs e) { Excel.Application excel; Excel.Workbook book; Excel.Worksheet sheet; Excel.Range range; try { excel = new Excel.Application(); excel.Visible = true; book = (Excel.Workbook)(excel.Workbooks.Add(@"C:\Document s and Settings\Mark\My Documents\Visual Studio 2005\Projects\ExcelWindowsTest\ExcelWindowsTest\Po rtDeliveryWorkBook.xls")); excel.DisplayAlerts = false; sheet = (Excel.Worksheet)book.ActiveSheet; range = (Excel.Range)sheet.Columns["A", Type.Missing]; range.AutoFit(); book.Save(); excel.Quit(); } catch (Exception theException) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } } -- Thanks |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Excel.Interop Not Saving Column Width Changes
I tried renaming the variables and using range.EntireColumn.AutoFit.
Unfortunately, this did not work. Perhaps Excel.Interop just can't do this. I do not know. If anyone has another suggestion it would be greatly appreciated. Thanks for your time. "Doug Glancy" wrote: Not sure if it will help but try: range.EntireColumn.AutoFit Also I don't know if it matters that you are using an Excel reserved name "Range" as a variable name, but I'd try changing it to "Rng" or something (also maybe for "Excel" and "Sheet"). hth, Doug "Geomar" wrote in message ... Hi, I need to open an Excel file using Interop, resize a column and then save the file, insuring that the worksheet retains the newly resized column width. As I step through the code all is working fine. But when I reopen the file the new column width has not been saved. If I open the file manually, resize the column, then save, the file retains the new column width. Any suggestions? I should also add that I tried reading the column width after the AutoFit was called and then explicitly set the column width to that value, then I saved the file This also did not work. Here is the code I used. I have done this in Winforms. Thanks for your help. Mark using Excel=Microsoft.Office.Interop.Excel; namespace ExcelWindowsTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, System.EventArgs e) { Excel.Application excel; Excel.Workbook book; Excel.Worksheet sheet; Excel.Range range; try { excel = new Excel.Application(); excel.Visible = true; book = (Excel.Workbook)(excel.Workbooks.Add(@"C:\Document s and Settings\Mark\My Documents\Visual Studio 2005\Projects\ExcelWindowsTest\ExcelWindowsTest\Po rtDeliveryWorkBook.xls")); excel.DisplayAlerts = false; sheet = (Excel.Worksheet)book.ActiveSheet; range = (Excel.Range)sheet.Columns["A", Type.Missing]; range.AutoFit(); book.Save(); excel.Quit(); } catch (Exception theException) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, theException.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, theException.Source); MessageBox.Show(errorMessage, "Error"); } } -- Thanks |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Change Cell Width WITHOUT changing Column width | Excel Discussion (Misc queries) | |||
Column width not saving | Excel Discussion (Misc queries) | |||
Set Column Width Based On Total Width Of Other Columns | Excel Programming | |||
Excel Interop Catch Event for Saving a file as xls and as xml? | Excel Programming |