ITeye博客

VSTO-HOWTO:如何检查文档中是否存在指定名称的Shape?

lsjlym · 2013-03-31 08:32 · Markdown

确定文档中是否存在指定名称的Shape,以避免重复添加。

在Word中:

public bool IsExistedOfShape(string strShapeName)
{
bool blnExisted = false;
//
IEnumerator etr = Globals.ThisAddIn.Application.ActiveDocument.Shapes.GetEnumerator();
//
while (etr.MoveNext())
{
if (((Word.Shape)etr.Current).Name.ToLower().Equals(strShapeName.ToLower()))
{
blnExisted = true;
break;
}
}
//
return blnExisted;
}

在Excel中:

public bool IsExistedOfShape(string strShapeName)
{
bool blnExisted = false;
//
Excel.Worksheet objActiveSheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
//
IEnumerator etr = objActiveSheet.Shapes.GetEnumerator();
while (etr.MoveNext())
{
if (((Excel.Shape)etr.Current).Name.ToLower().Equals(strShapeName.ToLower()))
{
blnExisted = true;
break;
}
}
//
return blnExisted;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。