PowerPoint 2007 Service Pack 2, ExportAsFixedFormat dans PowerShell?
-
23-08-2019 - |
Question
Hier, je cherchais à convertir par lots un groupe de fichiers PDF en PPT pour un ami, et je décidé de jeter un oeil à PowerShell, car il est assis sur mon HD pendant un certain temps.
Voici le code que je suis venu avec.
$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)
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 cela 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:
- PpFixedFormatType Enumeration
- ExportAsFixedFormat Méthode
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
Pas de solution correcte
Autres conseils
Je suis tombé sur le même problème en Python. Essayez spécifier argument PrintRange
comme dit en solution par Stefan Schukat:
Ceci est un bogue dans Powerpoint. Il définit « [in, en option, defaultvalue (0)] * PrintRange PrintRange » qui conduit à la génération de "PrintRange = 0" dans l'emballage de python. Par conséquent, vous aurez la erreur lors de l'appel de la méthode. Donc, pas de problème de makepy. solution de contournement appeler la méthode avec PrintRange = Aucun Puisqu'aucun est un objet COM Vali. Par exemple. presentation.ExportAsFixedFormat (pptFile + 'pdf', win32com.client.constants.ppFixedFormatTypePDF, win32com.client.constants.ppFixedFormatIntentScreen, PrintRange = Aucun) devrait fonctionner.
Source: incompatibilité de type lors de l'utilisation auj à l'exportation de PowerPoint 2007
Je ne sais pas du tout PowerShell mais qui ont travaillé 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 l'exportation est fait. Elle exporte en quelque sorte la présentation complète, non seulement glisser pas. 1, comme je pensais. Post-scriptum Oh. Voici la même solution