如何在vba中找到命名形状的索引号 [英] How to find the index number of a named shape in vba

查看:91
本文介绍了如何在vba中找到命名形状的索引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了以下代码

for i=1 to activedocument.Shapes.count : debug.Print activedocument.shapes(i).name : next

得到一个形状列表,但缺少一个形状

And got a list of shapes, however a shape was missing

我选择了一个未包含的形状,并使用 debug.print selection.Shaperange.name

I selected a shape not included, and got the name with debug.print selection.Shaperange.name

我怎样才能做类似的事情来找到它的索引,并找出这里发生了什么?

How can I do something similar to find its index, and find out what is going on here?

下面的代码找到我所有的形状

The following code finds me all my shapes

Dim i
Dim rngStory As Word.Range

For Each rngStory In ActiveDocument.StoryRanges
    For Each i In rngStory.ShapeRange
        Debug.Print rngStory.StoryType & ": " & i.name
    Next
Next

问题是 activedocument.shapes 只返回主要故事中的形状.上面的代码循环遍历所有范围,从而找到所有形状.

The problem was that activedocument.shapes only returns shapes in the main story. The above code cycles through all the ranges, and so finds all the shapes.

然而,这仍然没有回答如何获得形状索引的核心问题.

However, this still doesn't answer the central question of how to get a shape index.

推荐答案

这对我有用.指定工作表名称和控件名称

This works for me. Specify the worksheet name and name of the control

词:

Public Function GetShapeIndex(ByVal strName As String) As Long

    GetShapeIndex = ActiveDocument.Shapes(strName).ID

End Function

Excel:

Public Function GetShapeIndex(ByVal strWorksheet As String, ByVal strName As String) As Long

    GetShapeIndex = Worksheets(strWorksheet).Shapes(strName).ID

End Function

这篇关于如何在vba中找到命名形状的索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆