2017-01-18 3 views
0

텍스트 상자, 텍스트 및 스타일을 추가하는 간단한 코드를 작성했습니다. 내가 추가 한 texbox는 "This is my new text"라고 말합니다. 이 텍스트 상자의 중간 부분에서 "더 많은 텍스트"를 말하고있는 다른 텍스트를 추가하고 싶습니다. 예를 들어 "이것은 더 많은 텍스트 새 텍스트"입니다. 나는 interop을 사용하는 것에 상당히 익숙해 어떻게 그것을 달성 할 수 있었는지 잘 모르겠다. 그리고 가능하다면 나는 확신 할 수 없다. 다음과 같이interop을 사용하여 PowerPoint에서 문장 중간에 텍스트 추가

내 코드는 다음과 같습니다

Application pptApplication = new Application(); 

Microsoft.Office.Interop.PowerPoint.Slides slides; 
Microsoft.Office.Interop.PowerPoint.Slide slide; 
Microsoft.Office.Interop.PowerPoint.Shape shp; 
Font2 font2; 
Microsoft.Office.Interop.PowerPoint.TextRange objText; 

TextRange2 objText2; 

// Create the Presentation File 
Presentation pptPresentation = pptApplication.Presentations.Open(@"C:\Users\Craig\Documents\AddText.pptx"); 

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

// Create new Slide 
slides = pptPresentation.Slides; 
Microsoft.Office.Interop.PowerPoint.Slide firstSlide = Globals.ThisAddIn 
    .Application.ActivePresentation.Slides[1]; 
shp= firstSlide.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, Left: 30, Top: 30, Width:340, Height: 340); 

shp.TextFrame.TextRange.Text = "This is my new text"; 
shp.TextEffect.FontName = "Arial"; 
shp.TextEffect.FontSize = 32; 

pptPresentation.Save(); 
+0

'using Microsoft.Office.Interop.PowerPoint'와 같이 코드를 더 읽기 쉽게 만들려면 실제로 많이 사용해야합니다. 또한 변수 상단에 변수를 모두 선언하는 이유는 무엇입니까 (예 : '슬라이드', '새 슬라이드 만들기'에 대한 주석 바로 뒤에 선언 할 수 있음). – EluciusFTW

+0

이 코드는 필자의 텍스트 프로젝트에 포함 된 것입니다. 조금 더러워졌습니다. –

+0

지저분한 코드를 작성하여 시간을 절약 할 수 있습니다. 내 조언은 항상 깨끗한 코드를 작성하는 습관을 얻는 것입니다. 장기적으로는 더 빨리 또는 더 빨리 얻을 수 있습니다. – EluciusFTW

답변

0

다음은 작동합니다. 이 코드를 특별히 테스트하지는 않았지만 요즘에는 PowerPoint interop에서 다른 작업을하고 있습니다.

shp.TextFrame.TextRange.Text = "This is my new text"; 
var start = shp.TextFrame.TextRange.Find("This is my "); 
start.InsertAfter("more text ");