views:

457

answers:

2

I need to be able to save Presentations (programatically) in PowerPoint 2003 as OpenXML (".pptx"). I installed the Microsoft Office Compatibility Pack. This indeed allows me to perform "Save as PowerPoint 2007 Presentation" from PowerPoint 2003.

How can I do this programmatically? (e.g. VBA)

I tried Presentation.SaveAs: While there is no inherent PpSaveAsFileType enum value in PowerPoint 2003 for ppSaveAsOpenXMLPresentation, I made a program which prints the PpSaveAsFileType values and found out that during run-time, ppSaveAsOpenXMLPresentation = 24.

However, I tried: SaveAs(@"c:\temp\saveas\pupik.pptx", (PpSaveAsFileType) ((int) 24), MsoTriState.msoTrue);

And got an "Invalid Enumeration Value" Exception

Any ideas how to make this work?

(PS - I am aware that this question was already asked by several people on the web, but no solutions were offered.)

Thanks, Arie

+4  A: 

Edit > Some grammar

AFAIK the pptx format does not support macro-enabled presentations, so if your code is in the presentation you are trying to save, it will not work.

I don't have Excel 2003 at hand now, but if the Compatibility Pack enabled the option "pptx" in the Configuration Dialog, Default Save Format, and you are trying to save ANOTHER presentation I guess you can if you use something like:

 MyOtherPresentation.SaveAs "C:\Mypres", ppSaveAsDefault

Please note that this may work only if the presentation had not been saved before in ppt format

EDIT

If the above doesn't work, you could try a different approach. Save the file in the old format and the call a conversion program:

ppcnvcom.exe
See here for an example (using wordconv.exe, but essentially the same)
Be sure to have all the office upgrades installed, because if not the program ends reporting no error and doing nothing.

ofc
See here for instructions
And here for a good discussion

HTH!

belisarius
Thanks belisarius, however what I need is to load ".ppt" and save as ".pptx", so unfortunately "save as default" would not work.BTW - my code is not in the presentation I am trying to save, it is a C# VSTO program, so I don't have a problem with Macros.
Arie Livshin
@Arie Livshin Perhaps it may work if you save it with another name. Care to try?
belisarius
I do save it to another name than the original.
Arie Livshin
@Arie Livshin And ... what was the result???
belisarius
I mean that's what I do from the beginning. It doesn't work.
Arie Livshin
Thanks! I think I'll do this externally as you suggest.
Arie Livshin
+3  A: 

A trick is to modify the default save format of the application in the Registry, then save and finally to restore the original save format again.

The relevant key is

Software\Microsoft\Office\11.0\PowerPoint\Options

Create a DWORD value with name DefaultFormat and set it to 0x21 to save as 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);
            }
        }
    }       
}
0xA3
@Arie Livshin This is a clever approach! To manipulate the registry in VBA there are several options. Look for examples in http://www.excelforum.com/excel-programming/531053-read-registry-keys-and-possibly-write.html And you could try it manually with regedit before programming.
belisarius