【发布时间】:2017-01-18 10:00:35
【问题描述】:
我编写了一些添加文本框、一些文本和样式的简单代码。我添加的 texbox 说“这是我的新文本”。在这个文本框中间的某个地方,我想添加另一段文字,说“更多文本”,例如“这是我的更多文本新文本”。我不确定如何实现这一点,因为我对使用互操作还很陌生,我什至不确定是否可行。
我的代码如下:
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();
【问题讨论】:
-
你真的应该使用更多的用法来使你的代码更具可读性,例如,
using Microsoft.Office.Interop.PowerPoint。另外,为什么要在顶部声明变量,例如slides,您可以在评论“创建新幻灯片”之后立即声明。 -
这段代码只是在我的文本项目中,所以有点乱
-
你永远不会通过编写杂乱的代码来节省时间。我的建议是尝试养成始终编写干净代码的习惯。从长远来看,您将获得同样快甚至更快的速度。
标签: c# interop office-interop office-addins