---
title: "VSTO-HOWTO：如何检查文档中是否存在指定名称的Shape？"
author: "lsjlym"
date: "2013-03-31T08:32:00Z"
tags: []
slug: "vsto-howto-shape"
draft: false
lastmod: "2015-09-08T20:24:22Z"
id: 2241823
url: "https://lsjlym.iteye.com/blog/2241823"
---

确定文档中是否存在指定名称的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;  
}