Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
935 views
in Technique[技术] by (71.8m points)

pdf - PowerPoint 2007 SP2, ExportAsFixedFormat in PowerShell?

Yesterday I was trying to batch convert a group of PPTs into PDFs for a friend, and I decided to have a look at PowerShell, since it's been sitting on my HD for a while.

Here's the code I've come up with.

$p = new-object -comobject powerpoint.application

# I actually don't know why I have to set the window to visible, 
# but it doesn't work otherwise, anyway, it's not the real problem I have
$p.visible = 1 

$f = $p.presentations.open('somefile.ppt')

$f.ExportAsFixedFormat('some
ewfile.pdf', 2) 

2 is for PDF

Since the "brute force" method didn't work ("type mismatch") I tried to import the enum type with

$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat('some
ewfile.pdf', $pptypepdf) 

The strange thing here is that it still throws a "type mismatch" error...

Also, SaveAs works fine with

$f.SaveAs('some
ewfile.pdf', 32) # 32 is for PDF

What am I doing wrong?

Update

Relevant documentation:

Here's the full error message

$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat($filepath, $pptypepdf)

Exception calling "ExportAsFixedFormat" with "2" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

At line:1 char:23
+ $f.ExportAsFixedFormat <<<< ($filepath, $pptypepdf)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've come across the same problem in Python. Try specifying PrintRange argument as said in solution by Stefan Schukat:

This is a bug in Powerpoint. It defines "[in, optional, defaultvalue(0)] PrintRange* PrintRange" which leads to the generation of "PrintRange=0" in the python wrapper. Therefore you'll get the error when calling the method. So no problem of makepy. Workaround call the method with PrintRange=None since None is a vali COM object. E.g. presentation.ExportAsFixedFormat(pptFile+'.pdf', win32com.client.constants.ppFixedFormatTypePDF, win32com.client.constants.ppFixedFormatIntentScreen, PrintRange=None) should work.

Source: Type mismatch when using export fuction of PowerPoint 2007


I don't know PowerShell at all but have worked out a working example:

$p.ActivePresentation.PrintOptions.Ranges.Add(1,1)
$r = $p.ActivePresentation.PrintOptions.Ranges.Item(1)
$document.ExportAsFixedFormat('D:\ps.pdf', 2, 1, 0, 1, 1, 0, $r)

This isn't a full solution, but exporting is done. It somehow exports full presentation, not only slide no. 1, as I thought. P.S. Oh. Here's the same solution


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...