Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Save as pdf Microsoft Excel from Matlab

254 views
Skip to first unread message

Audric

unread,
Feb 28, 2012, 5:26:10 AM2/28/12
to
Hello,

I need to export some sheets of an Excel file into a pdf file. This has to be done very often so I would like to automate it from Matlab (part of a larger application).

From this page http://www.mathworks.nl/help/techdoc/ref/f16-5702.html#f16-14631 I see that there is no "save as" function for office documents. My idea is maybe to write a vba code with as arguments the names of the sheet that would be called from my Matlab application. Is this possible to call vba code from Matlab?

I have found this following code from this site https://www.quantnet.com/forum/threads/interface-between-matlab-vba-excel.2090/

%Open a COM server on Matlab
Excel = actxserver('Excel.Application');

Workbook = Excel.Workbooks.Open('c:\Code\Book1.xls');
x = fseminf(@(x) callFunc1(x,Excel),x0,ntheta,seminfcon);

function x = callFunc1(param,Excel)

x=Excel.Run('Func1',param);
Excel.Quit;

Is this possible? And how? If there is another way to do it, I am open to everything.

Thanks a lot in advance !

Audric

unread,
Feb 28, 2012, 11:04:13 AM2/28/12
to
Apparently I can use one the following :

Excel workbook.SaveAs

or

Excel workbook.Printout

Anyone can help me to define the parameters?

Thanks in advance

Audric

unread,
Feb 29, 2012, 8:08:11 AM2/29/12
to
I am now using

Excel.ActiveWorkbook.PrintOut(1,1,1,'False', 'PDFCreator', 'False', 'False', filename)

with PDFCreator installed on my laptop.

A pdf is created on the right place but I cannot read it.

Can someone help me?

Audric

unread,
Mar 9, 2012, 12:41:18 PM3/9/12
to
I am the only one to participate to this thread but still I continue to ask for help.

I can print Excel files selecting my sheets without a problem but I cannot call the PDFCreator to get my pdf. I wonder if I should not add something to be able to use PDFCreator saving functionalities

I give you my latest code

sheets = {'page1', 'page2', 'page3'}
k = 1;
for i = ExcelWorkbook.Sheets.count
aaa = get(ExcelWorkbook.Sheets, 'item', i);
bbb = get(aaa, 'Name');
if strcmp(bbb, sheets(k))
invoke(aaa, 'select', 'false');
if k < numel(sheets)
k = k + 1;
end
end
end
Excel.ActiveWindow.SelectedSheets.PrintOut([],[],[],'False', 'PDFCreator', 'True', 'False', filenamepdf);

The pdf produced is corrupted. I have played already with the false and true options. With default printer I can print without problem. With preview mode I can select the name of my file from a window and my pdf is ok (if I don t give a filenamepdf).

Thanks for your help

ImageAnalyst

unread,
Mar 9, 2012, 4:46:00 PM3/9/12
to
Do you have Adobe Acrobat installed? Acrobat installs a PDF toolbar
in MS office apps. If so you could probably instruct Excel (via
ActiveX commands) to save the workbook as a PDF (via the Acrobat Excel
plugin). I haven't done it but it seems reasonable.

Audric

unread,
Mar 15, 2012, 11:49:17 AM3/15/12
to
Yes but I don t see how to use ActiveX commands with PDFCreator and Adobe Acrobat is not free and not an option.

Should I start with something like this

pdf = actxserver('PDFCreator.clsPDFCreator'); ???

I have also found this VBA code but I cannot adapt it

Sub PrintToPDF_Early()
'Author : Ken Puls ([url=http://www.excelguru.ca]Excelguru.ca | Tips and pointers for Excel and other MS Office applications[/url])
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from [url=http://sourceforge.net/projects/pdfcreator/]SourceForge.net: PDFCreator[/url])
' Designed for early bind, set reference to PDFCreator
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
'/// Change the output file name here! ///
sPDFName = "testPDF.pdf"
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
Set pdfjob = New PDFCreator.clsPDFCreator
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Print the document to PDF
ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until PDF creator is finished then release the objects
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
End Sub

I am sorry if my question is stupid...

ImageAnalyst <imagea...@mailinator.com> wrote in message <d1140ff5-12a3-495c...@a37g2000yqm.googlegroups.com>...

ImageAnalyst

unread,
Mar 15, 2012, 12:47:40 PM3/15/12
to
Sure, take a stab at converting that to ActiveX programming and see
how it goes. Use your Excel example as guidelines for how to do this.
0 new messages