void AdjustColWidth(const CString& strName)
{
_Application ExcelApp;
Range range,col;
COleVariant varTmp((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// Start Microsoft Excel, get _Application object,
// and attach to app object.
if(!ExcelApp.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
// Set visible.
ExcelApp.SetVisible(false);
ExcelApp.SetAlertBeforeOverwriting(false);
ExcelApp.SetDisplayAlerts(false);
Workbooks workBooks = ExcelApp.GetWorkbooks();
_Workbook workBook = workBooks.Open(strName,
varTmp, varTmp, varTmp, varTmp,
varTmp, varTmp, varTmp, varTmp,
varTmp, varTmp, varTmp, varTmp);
Worksheets workSheets = workBook.GetSheets();
_Worksheet workSheet = workSheets.GetItem(COleVariant((short)1));
col = workSheet.GetColumns();
col.AutoFit();
workBook.Save();
ExcelApp.Quit();
range.ReleaseDispatch();
workSheet.ReleaseDispatch();
workSheets.ReleaseDispatch();
workBook.ReleaseDispatch();
workBooks.ReleaseDispatch();
ExcelApp.ReleaseDispatch();
}