2013-01-16 69 views
1

我正在製作C#應用程序從我創建powerpoint演示文稿。我想應用主題我的介紹編程。我使用下面的代碼得到主題列表。但是我怎麼能夠將它們應用到一個活躍的演示文稿?以編程方式應用Powerpoint主題

String programfilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); 
String msOfficePath = "Microsoft Office\\Document Themes 14"; 
String fullPath = Path.Combine(programfilesPath, msOfficePath); 
String[] fileEntries = Directory.GetFiles(fullPath, "*.thmx", SearchOption.TopDirectoryOnly); 

任何想法如何進行?

+2

你檢查此鏈接**【如何:將主題應用於演示文稿(http://msdn.microsoft.com/en-us/library/cc850846 (v = office.14)的.aspx)**? –

+0

耶@Pilgerstofer,我看了一下該鏈接之前,但它是沒有用的,因爲它們是從一個PowerPoint文件到另一個副本的主題。這些主題文件是存儲在我上面提到的位置中的.thmx文件。 在你已經共享的鏈接中看到這段代碼.. string presentationFile = @「C:\ Users \ Public \ Documents \ myppt2.pptx」; 串themePresentation = @」 C:\用戶\公共\文件\ myppt2-theme.pptx」; ApplyThemeToPresentation(presentationFile,themePresentation); – Pankaj

回答

1

我只是發現了一些很好的例子回答:

  1. Create PowerPoint programmatically
  2. Apply Theme PowerPoint 2010

組合這些兩個導向這一切都該

using PowerPoint = Microsoft.Office.Interop.PowerPoint; 
using Core = Microsoft.Office.Core; 
// ... 

// create application object 
PowerPoint.Application pptApplication = new PowerPoint.Application(); 

PowerPoint.Slides slides; 
PowerPoint._Slide slide; 
PowerPoint.TextRange objText; 

// Create the Presentation File 
PowerPoint.Presentation pptPresentation = pptApplication.Presentations.Add(Core.MsoTriState.msoTrue); 

// APPLY THEME - for example Clarity.thmx or 
// anything within Microsoft Office\Document Themes 14  
pptPresentation.ApplyTheme(@"C:\Program Files (x86)\Microsoft Office\Document Themes 14\Clarity.thmx"); 

PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[PowerPoint.PpSlideLayout.ppLayoutText]; 

// Create new Slide 
slides = pptPresentation.Slides; 
slide = slides.AddSlide(1, customLayout); 

// Add title, modify content and so on ... 
objText = slide.Shapes[1].TextFrame.TextRange; 
objText.Text = "hello world"; 
objText.Font.Name = "Verdana"; 

pptPresentation.SaveAs(@"c:\yourPPT.pptx", PowerPoint.PpSaveAsFileType.ppSaveAsDefault, Core.MsoTriState.msoTrue); 

pptPresentation.Close(); 
pptApplication.Quit(); 
GC.Collect(); 
+0

@@ Pilgerstofer,非常感謝您的代碼,但有一個問題。第二個鏈接是VBA,在C#中沒有像ApplyTheme這樣的API。 :( – Pankaj

+0

我嘗試過了,它爲我工作得很好? –

+0

我只是 - 重新checkes我的回答能正常工作,因爲我發佈的代碼複製和粘貼,並檢查它自己 - 讓我知道如果你遇到任何錯誤! –

0

找到了我一直在尋找,我分享完整的代碼爲別人的幫助

using Microsoft.Office.Interop.PowerPoint; 

String programfilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); 
String msOfficePath = "Microsoft Office\\Document Themes 14"; 
String fullPath = Path.Combine(programfilesPath, msOfficePath); 
String themePresentationPath = fullPath + "\\" + Waveform.thmx"; 
// You can change this Waveform.thmx file to any other theme file to apply other theme. 


Application pptApplication = new Application(); 
Presentation pptPresentation = pptApplication.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoTrue); 

pptPresentation.ApplyTemplate(themePresentationPath); 
+0

Hi @Pankaj,如何使用代碼動態獲取這個Document Themes **路徑? 'String msOfficePath =「Microsoft Office \\ Document Themes 14」;' –

相關問題