2010-06-29 112 views
4

我需要能夠在PowerPoint 2003中將演示文稿(以編程方式)另存爲OpenXML(「.pptx」)。 我安裝了Microsoft Office兼容包。這確實使我可以從PowerPoint 2003執行「另存爲PowerPoint 2007演示文稿」。以編程方式保存爲PowerPoint 2007(pptx),從PowerPoint 2003

如何以編程方式執行此操作? (例如VBA)

我試圖Presentation.SaveAs: 雖然有在PowerPoint 2003爲ppSaveAsOpenXMLPresentation沒有固有PpSaveAsFileType枚舉值,我做了一個程序,它打印出PpSaveAsFileType值,結果發現,在運行時,ppSaveAsOpenXMLPresentation = 24

不過,我想: SaveAs(@"c:\temp\saveas\pupik.pptx", (PpSaveAsFileType) ((int) 24), MsoTriState.msoTrue);

,並得到一個「無效枚舉值」異常

任何想法如何使這項工作?

(PS - 我知道這個問題已經由幾個人在網絡上問過,但都沒有提供任何解決方案)

感謝, 阿里

回答

4

編輯>一些語法

AFAIK pptx格式不支持啓用宏的演示文稿,因此,如果您的代碼位於您試圖保存的演示文稿中,則不起作用。

我沒有手頭有Excel 2003了,但如果兼容包啓用該選項「PPTX」在配置對話框,默認保存格式,你要保存另一個演示我想你可以的,如果你使用類似:

MyOtherPresentation.SaveAs "C:\Mypres", ppSaveAsDefault 

請注意,如果演示文稿以前沒有在ppt格式保存,這可能只是工作

編輯

如果上述不起作用,您可以嘗試不同的方法。保存在老格式文件並調用一個轉換程序:

ppcnvcom.exe
爲例見here(使用wordconv.exe,但本質上是相同的)
一定要擁有所有的辦公室升級安裝,因爲如果不是程序結束 報告沒有錯誤,什麼都不做。

OFC
here的說明
here有一個良好的討論

HTH!

+0

感謝貝利薩留,但我需要的是加載「.PPT」,保存爲「.PPTX」所以不幸的是「保存爲默認」不起作用。 順便說一句 - 我的代碼不在演示文稿中我試圖保存,它是一個C#VSTO程序,所以我沒有宏的問題。 – 2010-06-30 09:09:51

+0

@Arie Livshin如果你用另一個名字保存它,它可能會工作。謹慎嘗試? – 2010-06-30 11:49:48

+0

我把它保存到原來的名字以外。 – 2010-06-30 14:20:00

3

一個竅門是在註冊表中修改應用程序的默認保存格式,然後保存並最後再次恢復原始保存格式。

相關的關鍵是

Software\Microsoft\Office\11.0\PowerPoint\Options 

與名DefaultFormat創建DWORD值,並將其設置爲0×21另存爲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); 
      } 
     } 
    }  
} 
+0

@Arie Livshin這是一個聰明的方法!要在VBA中操作註冊表,有幾個選項。請在http://www.excelforum.com/excel-programming/531053-read-registry-keys-and-possibly-write.html中查找示例。在編程之前,您可以使用regedit手動嘗試。 – 2010-07-06 17:05:42

0

我用ppcnvcom.exe但請注意,與相當數量的職位我只使用-oice開關沒有-NMe開關

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

我知道這是一個老問題,但我解決了最近使用的問題:

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

而不是SaveAs。無論原始格式是ppt還是pptx格式,都可以很好地工作。

(我無法得到提及爲我工作的註冊表的改變方法,無需重新打開演示文稿。)

相關問題