我是採用Office 2007的系統, powerpoint object model是不一樣的.
執行檔下載點
參考資料:
Simple Powerpoint automation using C#
MSDN Office development
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint; namespace PowerpointMerger { public partial class Form1 : Form { static int MAX_SLIDE_NUMBER = 10; string[] filepath = new string[MAX_SLIDE_NUMBER]; string templatePath; PowerPoint.Application objApp; PowerPoint.Presentations objPresSet; PowerPoint._Presentation objPresMerged; PowerPoint.Slides objSlidesMerged; //PowerPoint.Slides objSlidesSrc; //來源的ppt slides //PowerPoint.Slides objSlidesDst; //合併的目標ppt slides public Form1() { InitializeComponent(); } public void start() { //Create a new presentation based on a template. objApp = new PowerPoint.Application(); objApp.Visible = MsoTriState.msoTrue; objPresSet = objApp.Presentations; if (File.Exists(templatePath)) { // 開啟template objPresMerged = objPresSet.Open(templatePath, MsoTriState.msoTriStateMixed, MsoTriState.msoTrue, MsoTriState.msoTrue); } else { objPresMerged = objPresSet.Add(MsoTriState.msoTrue); //產生一個新的 } objSlidesMerged = objPresMerged.Slides; // 取得新的merged slides // merge slides int numSlides = 0; for (int i = 0; i < MAX_SLIDE_NUMBER; i++) { numSlides = MergeSlide(this.objSlidesMerged, filepath[i], numSlides); } // save as a copy to .. // objPresMerged.SaveCopyAs("E:\\test_new.ppt", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault, MsoTriState.msoTrue); // objApp.Quit(); } private int MergeSlide(PowerPoint.Slides objSlidesMerged, string path, int numSlides) { if (File.Exists(path)) { PowerPoint._Presentation ps = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse); // expression.InsertFromFile(FileName, Index插入點, SlideStart來源檔起始點, SlideEnd來源檔結束點) int numInserted = objSlidesMerged.InsertFromFile(path, numSlides, 1, ps.Slides.Count); numSlides += numInserted; } return numSlides; } /// <summary> /// 將檔案路徑設定到filepath陣列, 方便merge時以迴圈處理. /// </summary> /// <param name="index"></param> /// <param name="path"></param> private void setFilePath(int index, string path) { filepath[index] = path; } private void buttonStart_Click(object sender, EventArgs e) { start(); } private void btn1_Click(object sender, EventArgs e) { openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Powerpoint files (*.ppt)|*.ppt"; if (openFileDialog1.ShowDialog() == DialogResult.Cancel) return; textBox1.Text = openFileDialog1.FileName; setFilePath(0, textBox1.Text); } private void btn2_Click(object sender, EventArgs e) { openFileDialog2 = new OpenFileDialog(); openFileDialog2.Filter = "Powerpoint files (*.ppt)|*.ppt"; if (openFileDialog2.ShowDialog() == DialogResult.Cancel) return; textBox2.Text = openFileDialog2.FileName; setFilePath(1, textBox2.Text); } private void btn3_Click(object sender, EventArgs e) { openFileDialog3 = new OpenFileDialog(); openFileDialog3.Filter = "Powerpoint files (*.ppt)|*.ppt"; if (openFileDialog3.ShowDialog() == DialogResult.Cancel) return; textBox3.Text = openFileDialog3.FileName; setFilePath(2, textBox3.Text); } private void btn4_Click(object sender, EventArgs e) { openFileDialog4 = new OpenFileDialog(); openFileDialog4.Filter = "Powerpoint files (*.ppt)|*.ppt"; if (openFileDialog4.ShowDialog() == DialogResult.Cancel) return; textBox4.Text = openFileDialog4.FileName; setFilePath(3, textBox4.Text); } private void btn5_Click(object sender, EventArgs e) { openFileDialog5 = new OpenFileDialog(); openFileDialog5.Filter = "Powerpoint files (*.ppt)|*.ppt"; if (openFileDialog5.ShowDialog() == DialogResult.Cancel) return; textBox5.Text = openFileDialog5.FileName; setFilePath(4, textBox5.Text); } private void btnTemplate_Click(object sender, EventArgs e) { openFileDialogTemplate = new OpenFileDialog(); openFileDialogTemplate.Filter = "Powerpoint files (*.ppt)|*.ppt"; if (openFileDialogTemplate.ShowDialog() == DialogResult.Cancel) return; templatePath = openFileDialogTemplate.FileName; textBox6.Text = templatePath; } } }
沒有留言:
張貼留言