c# - Microsoft.Office.Interop.Excel for Office 2007 -
i want try windows form application converts office file (excel, word, powerpoint) pdf file. client's pc not install visual studio , office version 2007. application uses microsoft.office.iterop.excel.dll
covert pdf format. dll file cannot found on client's pc , error has occurred following.
system.augumentexception: value not fall within expected range. @ microsoft.office.interop.excel._workbook.exportasfixedfromat(.......)
how can solve problem?
my code following
public bool exportworkbooktopdf(string workbookpath, string outputpath) { // if either required string null or empty, stop , bail out if (string.isnullorempty(workbookpath) || string.isnullorempty(outputpath)) { return false; } // create com objects microsoft.office.interop.excel.application excelapplication; microsoft.office.interop.excel.workbook excelworkbook; // create new instance of excel excelapplication = new microsoft.office.interop.excel.application(); // make process invisible user excelapplication.screenupdating = false; // make process silent excelapplication.displayalerts = false; // open workbook wish export pdf excelworkbook = excelapplication.workbooks.open(workbookpath); messagebox.show(workbookpath); // if workbook failed open, stop, clean up, , bail out if (excelworkbook == null) { excelapplication.quit(); excelapplication = null; excelworkbook = null; messagebox.show("in null"); return false; } var exportsuccessful = true; try { // call excel's native export function (valid in office 2007 , office 2010, afaik) excelworkbook.exportasfixedformat(microsoft.office.interop.excel.xlfixedformattype.xltypepdf, outputpath); } catch (system.exception ex) { messagebox.show(ex.message); // mark export failed return value... exportsuccessful = false; // exceptions here, if wish... // messagebox.show... } { // close workbook, quit excel, , clean regardless of results... excelworkbook.close(); excelapplication.quit(); excelapplication = null; excelworkbook = null; } // can use following method automatically open pdf after export if wish // make sure file exists first... if (system.io.file.exists(outputpath)) { messagebox.show(outputpath); system.diagnostics.process.start(outputpath); } return exportsuccessful; }
as said in comments, each client needs install 2007 microsoft office add-in: microsoft save pdf. doesn't matter if on windows 8.1 or have pdf reader installed. need add-in write pdfs office 2007. (you don't need add-in office 2010 or 2013.)
Comments
Post a Comment