[英]Error: The specified value is out of range in C# power point
I am trying to calibrify the text in entire presentation but if i have some ppt that is already designed my first slide consists of almost 37 shapees . 我正在尝试对整个演示文稿中的文本进行校准,但是如果我已经设计了一些ppt,则我的第一张幻灯片将包含近37种形状。
so when i calibrify some shapen it gives an error saying " the specified value is out of range" 因此,当我校准某些形状时,会出现错误,提示“指定值超出范围”
Below is my code: 下面是我的代码:
private void Calibrify_text_Click(object sender, RibbonControlEventArgs e)
{
try
{
PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
PowerPoint.SlideRange ppslr = ppApp.ActiveWindow.Selection.SlideRange;
int slidecount = ppApp.ActiveWindow.Presentation.Slides.Count;
for(int i = 1; i <= slidecount; i++)
{
ppApp.ActiveWindow.Presentation.Slides.Range(i).Select();
ppApp.ActivePresentation.Slides.Range(i).Shapes.SelectAll();
PowerPoint.ShapeRange ppshr = ppApp.ActiveWindow.Selection.ShapeRange;
for(int j = 1; j <= ppshr.Count; j++)
{
if (ppshr[j].HasTextFrame.ToString() !="")
{
var text = ppshr[i].ToString();
if (ppshr[j].TextFrame.TextRange.Text != "")
{
var text1 = ppshr[j].TextFrame.TextRange;
text1.Font.Name = "Calibri Light";
}
}
}
}
}
catch (COMException Ex)
{
Debug.WriteLine("Some problem" + Ex.Message + Ex.StackTrace);
MessageBox.Show(Ex.Message);
}
}
In C#, indexes are 0-based (opposed to VB/VBA for example, which are 1-based). 在C#中,索引基于0(例如,与基于1的VB / VBA相反)。 That makes this line fail: 这使该行失败:
for(int i = 1; i <= slidecount; i++)
It should be: 它应该是:
for(int i = 0; i < slidecount; i++)
(You make the same mistake later on) (稍后您会犯同样的错误)
I did an example for you assuming that there are several files with several shapes on it. 我为您提供了一个示例, 假设其中有多个文件以及多个形状 。 Furthermore I did some grouping (!!) - what I think was the problem in your code. 此外,我进行了一些分组 (!!) -我认为这是您的代码中的问题。
So my presentation containts four slides , each of them contains of 因此,我的演示文稿包含四张幻灯片 ,每张幻灯片都包含
(see screenshot of my demo file below) (请参见下面的演示文件截图)
Presentation file 简报文件
In my code I check whether the current shape is a grouped element or not. 在我的代码中,我检查当前形状是否为分组元素 。 In this case I call my methode recursivley. 在这种情况下,我将我的方法称为递归。 Otherelse I check if there is a textFrame and set the according font . 另外,我检查是否存在textFrame并设置相应的字体。
Add-In Code 附加码
private void btnCalibrifyText_Click(object sender, RibbonControlEventArgs e)
{
try
{
PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
PowerPoint.SlideRange ppslr = ppApp.ActiveWindow.Selection.SlideRange;
int slidecount = ppApp.ActiveWindow.Presentation.Slides.Count;
for (int i = 1; i <= slidecount; i++)
{
ppApp.ActiveWindow.Presentation.Slides.Range(i).Select();
ppApp.ActivePresentation.Slides.Range(i).Shapes.SelectAll();
PowerPoint.ShapeRange ppshr = ppApp.ActiveWindow.Selection.ShapeRange;
// new version
foreach (PowerPoint.Shape shape in ppshr)
{
changeFont(shape);
}
}
MessageBox.Show("Done!");
}
catch (COMException comEx)
{
Debug.WriteLine("COMException: " + comEx.Message + comEx.StackTrace);
MessageBox.Show(comEx.Message);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.Message + ex.StackTrace);
MessageBox.Show(ex.Message);
}
}
private void changeFont(PowerPoint.Shape shape)
{
if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup)
{
foreach (PowerPoint.Shape childShape in shape.GroupItems)
{
changeFont(childShape);
}
}
else if (shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)
{
if (shape.TextFrame.TextRange.Text != "")
{
var text1 = shape.TextFrame.TextRange;
text1.Font.Name = "Calibri Light";
}
}
}
Worked for me. 为我工作。 Hope it does the trick for you too. 希望它也对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.