MATLAB: How to save an Excel sheet as PDF through MATLAB

activexexcelpdf

I am using the following code:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test.xlsx'));
hWorksheet = hWorkbook.Sheets.Item(1);
hRange = hWorksheet.Range('A1:O10');
hRange.ExportAsFixedFormat('xlTypePDF','test_out.pdf');
The Excel ActiveX server allows me to do the usual stuff, but the last line doesn't do anything. Neither does it throw any error.
Does anyone know how to do this?

Best Answer

First, set the print area within the workbook itself, then you'll need to call ExportAsFixedFormat on the worksheet or workbook object. You cannot call it on the range object. Hope this helps, see below:
hExcel = actxserver('Excel.Application');
hWorkbook = hExcel.Workbooks.Open(sprintf('%s','C:\test.xlsx'));
hWorksheet = hWorkbook.Sheets.Item(1);
% print this sheet to PDF
hWorksheet.ExportAsFixedFormat('xlTypePDF','C:\test_out.pdf');