2009-05-21 5 views
1

Hier, j'essayais de convertir par lots un groupe de PPT en PDF pour un ami, et j'ai décidé de jeter un coup d'œil à PowerShell, puisqu'il est resté sur mon HD pendant un moment.PowerPoint 2007 SP2, ExportAsFixedFormat dans PowerShell?

Voici le code que j'ai trouvé.

$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('\some\file.ppt') 

$f.ExportAsFixedFormat('\some\newfile.pdf', 2) 

2 is for PDF

Puisque la méthode "force brute" ne fonctionnait pas ("incompatibilité de type") J'ai essayé d'importer le type ENUM avec

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

La chose étrange est que il jette encore une erreur "incompatibilité de type" ...

En outre, SaveAs fonctionne très bien avec

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

Qu'est-ce que je fais mal?

Mise à jour

La documentation pertinente:

Voici le message d'erreur complet

$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 
+1

Quelle est l'erreur exacte? J'ai vérifié MSDN, et la documentation sur cette méthode semble être fausse pour cet argument. Et je ne peux même pas trouver cette énumération sur MSDN! – JasonMArcher

+0

J'ai édité la question avec les détails pertinents, merci –

+0

Avez-vous essayé un appel donnant tous les paramètres facultatifs? –

Répondre

1

J'ai rencontré le même problème en Python. Essayez de spécifier l'argument PrintRange comme indiqué dans la solution par Stefan Schukat:

Ceci est un bug dans Powerpoint. Il définit "[in, optional, defaultvalue (0)] PrintRange * PrintRange" qui conduit à la génération de "PrintRange = 0" dans l'emballage python. Par conséquent, vous obtiendrez l'erreur lors de l'appel de la méthode. Donc pas de problème de makepy. Solution de contournement appelez la méthode avec PrintRange = None puisque None est un objet COM vali. E.g. presentation.ExportAsFixedFormat (pptFile + '. pdf', win32com.client.constants.ppFixedFormatTypePDF, win32com.client.constants.ppFixedFormatIntentScreen, PrintRange = Aucun) devrait fonctionner.

Source: Type mismatch when using export fuction of PowerPoint 2007


Je ne sais pas du tout, mais PowerShell ont élaboré un exemple de travail:

$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) 

Ce n'est pas une solution complète, mais exportation se fait. Il exporte en quelque sorte la présentation complète, pas seulement la diapositive. 1, comme je le pensais. P.S. Oh. Voici la même chose solution