Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a C#, for you experts out there, what is the recommend way to write a FORM based Microsoft.Office.Interop.Powerpoint application so that a file is only opened ONCE. After this the user can perform multiple button_clicks to cause changes to be made to the file. Then finally the user saves and closes the file by another button_click. (ie multiple access to the open file, through multiple button_clicks).

All the examples online perform the entire process "Open>do stuff>Save", within a single method. This is what I commonly see:

C#
private void button1_Click(object sender, EventArgs e)
{
      Using (PresentationDocument mypptx = PresenationDocument.Open(Myfilename,true))
     {
        //do stuff
        //do stuff
        Mypptx.PresentationPart.Presentation.Save();
        Mypptx.Close();
     }
 }


In the above example you are forced to open and close the file within every "Public void Button#_Click(...) method?


Instead I am looking for something like this in Pseudo code:

C#
using System;
using SystemIO;
using System.Windows.Forms;
using DocumentFormat.OpenXml;
using DocumentFormat.Presentaton;
using Microsoft.Office.Interop.Powerpoint;
using Powerpoint = Microsoft.Office.Interop.Powerpoint;

namespace Test1
{
    public partial class Form1 : Form
    {
      //can not open the file here as the Form is not open and you want the user to select the file name from the OpenFileDialog after they click a button on the form

        public Form1()
        {
            InitializeComponent();          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog1.ShowDialog();
            ...
            //Open the pptx file  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //do stuff to the open pptx file
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            //Save the pptx file
            //close the pptx file
        }
    }
}
Posted
Updated 29-Jul-15 16:24pm
v2
Comments
PIEBALDconsult 29-Jul-15 22:25pm    
You can, but it's not recommended. You'll need a field to hold a reference to the file.

Instead of

C#
Using (PresentationDocument mypptx = PresenationDocument.Open(Myfilename,true))
{
    //do stuff
    //do stuff
    Mypptx.PresentationPart.Presentation.Save();
    Mypptx.Close();
}


Do something like this, as PIEBALDconsultant suggested.

C#
using System;
using SystemIO;
using System.Windows.Forms;
using DocumentFormat.OpenXml;
using DocumentFormat.Presentaton;
using Microsoft.Office.Interop.Powerpoint;
using Powerpoint = Microsoft.Office.Interop.Powerpoint;

namespace Test1
{
    public partial class Form1 : Form
    {
        private PresentationDocument mypptx;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog1.ShowDialog();
            ...
            //Open the pptx file
            this.mypptx = PresenationDocument.Open(Myfilename,true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(null != this.mypptx)
            {
                //do stuff to the open pptx file
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            //Save the pptx file
            //close the pptx file
            if(null != this.mypptx)
            {
                this.mypptx.PresentationPart.Presentation.Save();
                this.mypptx.Close();
            }
        }
    }
}
 
Share this answer
 
Comments
Philippe Mori 30-Jul-15 12:41pm    
And also handle the case where the form is closed without clicking on button 3.
Thanks guys! I realize I will need to code around the conflicting logic issues, but this gets me started.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900