以编程方式将PowerPoint 2003另存为PowerPoint 2007格式(pptx)

4
我需要能够以OpenXML(“ .pptx”)的格式通过编程方式保存演示文稿在PowerPoint 2003中。
我安装了Microsoft Office兼容性包。这确实使我能够从PowerPoint 2003执行“另存为PowerPoint 2007演示文稿”。
如何通过编程方式实现呢? (例如VBA)
我尝试了Presentation.SaveAs: 虽然没有PowerPoint 2003中的固有PpSaveAsFileType枚举值适用于ppSaveAsOpenXMLPresentation,但我编写了一个程序,它打印出PpSaveAsFileType值,并发现在运行时,ppSaveAsOpenXMLPresentation = 24
然而,我尝试了: SaveAs(@"c:\temp\saveas\pupik.pptx", (PpSaveAsFileType) ((int) 24), MsoTriState.msoTrue); 并收到“无效的枚举值”异常。
有没有办法让它工作?
(PS-我知道几个人已经在网上提出过这个问题,但没有提供解决方案。)
谢谢, Arie
5个回答

4

编辑 > 一些语法

据我所知,pptx格式不支持启用宏的演示文稿,因此如果您的代码在要保存的演示文稿中,它将无法工作。

我现在没有Excel 2003,但如果兼容性包在配置对话框中启用了“pptx”选项,并且您正在尝试保存另一个演示文稿,则可以使用以下内容:

 MyOtherPresentation.SaveAs "C:\Mypres", ppSaveAsDefault

请注意,这仅适用于尚未以ppt格式保存演示文稿的情况。
编辑
如果上述方法不起作用,您可以尝试另一种方法。将文件保存为旧格式,然后调用转换程序: ppcnvcom.exe 参见此处以获取示例(使用wordconv.exe,但本质相同) 确保安装了所有office升级,因为如果没有,程序会报告无错误并且不执行任何操作。
ofc 请参阅此处以获取说明 以及此处以获取良好的讨论
希望对你有所帮助!

谢谢belisarius,但是我需要加载“.ppt”并保存为“.pptx”,所以“另存为默认值”不起作用。顺便说一下 - 我的代码不在我正在尝试保存的演示文稿中,它是一个C# VSTO程序,因此我不会遇到宏的问题。 - Arie Livshin
@Arie Livshin 或许你可以尝试用另一个名称保存它。想试试吗? - Dr. belisarius
我将其保存为与原始名称不同的另一个名称。 - Arie Livshin
谢谢!我想我会按照你的建议在外部进行这个操作。 - Arie Livshin

3

一个技巧是修改应用程序在注册表中的默认保存格式,然后保存并最终恢复原始保存格式。

相关键是

Software\Microsoft\Office\11.0\PowerPoint\Options

创建名为 DefaultFormatDWORD 值,并将其设置为 0x21 以保存为 PPTX。
public void SomeMethod()
{
    ...
    using (PptxSaver pptxSaver = new PptxSaver())
    {
        presentation.SaveAs("sample.pptx")
    }
    ...
}

class PptxSaver : IDisposable
{
    private const string OptionKey = @"Software\Microsoft\Office\11.0\PowerPoint\Options";
    private const string OptionValue = "DefaultFormat";        
    private const int SaveFormatPptx = 0x21;

    private int oldFormat;

    public PptxSaver()
    {
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(OptionKey, true))
        {
            oldFormat = (int)key.GetValue(OptionValue, -1);
            key.SetValue(OptionValue, SaveFormatPptx, RegistryValueKind.DWord);
        }
    }

    public void Dispose()
    {
        // Delete the value
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(OptionKey, true))
        {
            if (oldFormat == -1)
            {
                key.DeleteValue(OptionValue);
            }
            else
            {
                key.SetValue(OptionValue, oldFormat);
            }
        }
    }       
}

@Arie Livshin 这是一个聪明的方法!在 VBA 中操作注册表有几个选项。在 http://www.excelforum.com/excel-programming/531053-read-registry-keys-and-possibly-write.html 中寻找示例。在编程之前,您可以尝试使用 regedit 手动操作。 - Dr. belisarius

0

我知道这是一个老问题,但最近我找到了解决办法:

Presentation.SaveCopyAs "c:\temp\saveas\pupik.pptx"

使用 SaveAs 之外的方法。无论原始文件是 ppt- 还是 pptx 格式,均有效。

(我无法在不重新打开演示文稿的情况下使提到的注册表更改方法起作用。)


0

对于VBA,这个可以工作:

Sub TestSaveas()
  SaveAs "c:\somefilepath\"
End sub

Private Sub SaveAs(fp As String)
   Dim dlgSaveAs As FileDialog
   Dim strMyFile As String

   Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
   With dlgSaveAs
       .InitialFileName = fp
       If .Show = -1 Then
           strMyFile = .SelectedItems(1)
           Application.ActivePresentation.SaveAs strMyFile
           'MsgBox strMyFile
           ''-- save your file to strMyFile here
       Else
           MsgBox "File not saved"
       End If
   End With
   dlgSaveAs.Execute
   Set dlgSaveAs = Nothing
End Sub

0

我使用了ppcnvcom.exe,但请注意,与相当多的帖子不同,我仅使用了-oice开关而没有使用-nme开关


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接