Get shape index in powerpoint VBA
I hope this is a super simple question but for some reason i cant figure it out.
I need to delete a subset of the selected shapes on a powerpoint slide using VBA. I can do this using:
ActivePresentation.Slides(1).Shapes.Range(_Index_).Delete
Where _Index_ is either an array of shape indexes (integers) or shape names (stings).
Since the shape names are not unique (and people i am making this macro for have a nasty habit of having multiple shapes with the same name) i need to rely on the shape index number. My problem is i dont know how to get the index number of a given shape.
I can only see how i get the shape name or shape ID using:
ActiveWindow.Selection.ShapeRange(IdNum).Name
ActiveWindow.Selection.ShapeRange(IdNum).ID
So my question is: How do i get the shape index of selected shapes?
vba powerpoint powerpoint-vba
|
show 4 more comments
I hope this is a super simple question but for some reason i cant figure it out.
I need to delete a subset of the selected shapes on a powerpoint slide using VBA. I can do this using:
ActivePresentation.Slides(1).Shapes.Range(_Index_).Delete
Where _Index_ is either an array of shape indexes (integers) or shape names (stings).
Since the shape names are not unique (and people i am making this macro for have a nasty habit of having multiple shapes with the same name) i need to rely on the shape index number. My problem is i dont know how to get the index number of a given shape.
I can only see how i get the shape name or shape ID using:
ActiveWindow.Selection.ShapeRange(IdNum).Name
ActiveWindow.Selection.ShapeRange(IdNum).ID
So my question is: How do i get the shape index of selected shapes?
vba powerpoint powerpoint-vba
The id should give the order they were added to the slide.
– QHarr
Nov 10 at 20:33
Yes but how can i translate an ID into an index? The Index is always numbered 1 to number of shapes on slide while ID can be anything with gaps in between (e.g., it could be that index 1 has ID 5 and index 2 has ID 211). It seems like there should be an easy way to just get the index..
– Peter
Nov 10 at 21:08
I don't think a separate list of indices is maintained for the Shapes collection. You could write a loop over the shapes collection to assign names you can use to differentiate.
– QHarr
Nov 10 at 21:19
" My problem is i dont know how to get the index number of a given shape." If by "given" you mean selected, then ActiveWindow.Selection.ShapeRange(1)..ZOrderPosition will give you the index.
– Steve Rindsberg
Nov 11 at 16:00
Steve Rindsberg this was exactly what i needed! Thanks a lot! Im not exactly sure how this site works in terms of credit for answers but will you leave your comment as an answer so i can mark it as solved? Thanks again.
– Peter
Nov 12 at 9:03
|
show 4 more comments
I hope this is a super simple question but for some reason i cant figure it out.
I need to delete a subset of the selected shapes on a powerpoint slide using VBA. I can do this using:
ActivePresentation.Slides(1).Shapes.Range(_Index_).Delete
Where _Index_ is either an array of shape indexes (integers) or shape names (stings).
Since the shape names are not unique (and people i am making this macro for have a nasty habit of having multiple shapes with the same name) i need to rely on the shape index number. My problem is i dont know how to get the index number of a given shape.
I can only see how i get the shape name or shape ID using:
ActiveWindow.Selection.ShapeRange(IdNum).Name
ActiveWindow.Selection.ShapeRange(IdNum).ID
So my question is: How do i get the shape index of selected shapes?
vba powerpoint powerpoint-vba
I hope this is a super simple question but for some reason i cant figure it out.
I need to delete a subset of the selected shapes on a powerpoint slide using VBA. I can do this using:
ActivePresentation.Slides(1).Shapes.Range(_Index_).Delete
Where _Index_ is either an array of shape indexes (integers) or shape names (stings).
Since the shape names are not unique (and people i am making this macro for have a nasty habit of having multiple shapes with the same name) i need to rely on the shape index number. My problem is i dont know how to get the index number of a given shape.
I can only see how i get the shape name or shape ID using:
ActiveWindow.Selection.ShapeRange(IdNum).Name
ActiveWindow.Selection.ShapeRange(IdNum).ID
So my question is: How do i get the shape index of selected shapes?
vba powerpoint powerpoint-vba
vba powerpoint powerpoint-vba
edited Nov 12 at 9:11
asked Nov 10 at 19:57
Peter
32
32
The id should give the order they were added to the slide.
– QHarr
Nov 10 at 20:33
Yes but how can i translate an ID into an index? The Index is always numbered 1 to number of shapes on slide while ID can be anything with gaps in between (e.g., it could be that index 1 has ID 5 and index 2 has ID 211). It seems like there should be an easy way to just get the index..
– Peter
Nov 10 at 21:08
I don't think a separate list of indices is maintained for the Shapes collection. You could write a loop over the shapes collection to assign names you can use to differentiate.
– QHarr
Nov 10 at 21:19
" My problem is i dont know how to get the index number of a given shape." If by "given" you mean selected, then ActiveWindow.Selection.ShapeRange(1)..ZOrderPosition will give you the index.
– Steve Rindsberg
Nov 11 at 16:00
Steve Rindsberg this was exactly what i needed! Thanks a lot! Im not exactly sure how this site works in terms of credit for answers but will you leave your comment as an answer so i can mark it as solved? Thanks again.
– Peter
Nov 12 at 9:03
|
show 4 more comments
The id should give the order they were added to the slide.
– QHarr
Nov 10 at 20:33
Yes but how can i translate an ID into an index? The Index is always numbered 1 to number of shapes on slide while ID can be anything with gaps in between (e.g., it could be that index 1 has ID 5 and index 2 has ID 211). It seems like there should be an easy way to just get the index..
– Peter
Nov 10 at 21:08
I don't think a separate list of indices is maintained for the Shapes collection. You could write a loop over the shapes collection to assign names you can use to differentiate.
– QHarr
Nov 10 at 21:19
" My problem is i dont know how to get the index number of a given shape." If by "given" you mean selected, then ActiveWindow.Selection.ShapeRange(1)..ZOrderPosition will give you the index.
– Steve Rindsberg
Nov 11 at 16:00
Steve Rindsberg this was exactly what i needed! Thanks a lot! Im not exactly sure how this site works in terms of credit for answers but will you leave your comment as an answer so i can mark it as solved? Thanks again.
– Peter
Nov 12 at 9:03
The id should give the order they were added to the slide.
– QHarr
Nov 10 at 20:33
The id should give the order they were added to the slide.
– QHarr
Nov 10 at 20:33
Yes but how can i translate an ID into an index? The Index is always numbered 1 to number of shapes on slide while ID can be anything with gaps in between (e.g., it could be that index 1 has ID 5 and index 2 has ID 211). It seems like there should be an easy way to just get the index..
– Peter
Nov 10 at 21:08
Yes but how can i translate an ID into an index? The Index is always numbered 1 to number of shapes on slide while ID can be anything with gaps in between (e.g., it could be that index 1 has ID 5 and index 2 has ID 211). It seems like there should be an easy way to just get the index..
– Peter
Nov 10 at 21:08
I don't think a separate list of indices is maintained for the Shapes collection. You could write a loop over the shapes collection to assign names you can use to differentiate.
– QHarr
Nov 10 at 21:19
I don't think a separate list of indices is maintained for the Shapes collection. You could write a loop over the shapes collection to assign names you can use to differentiate.
– QHarr
Nov 10 at 21:19
" My problem is i dont know how to get the index number of a given shape." If by "given" you mean selected, then ActiveWindow.Selection.ShapeRange(1)..ZOrderPosition will give you the index.
– Steve Rindsberg
Nov 11 at 16:00
" My problem is i dont know how to get the index number of a given shape." If by "given" you mean selected, then ActiveWindow.Selection.ShapeRange(1)..ZOrderPosition will give you the index.
– Steve Rindsberg
Nov 11 at 16:00
Steve Rindsberg this was exactly what i needed! Thanks a lot! Im not exactly sure how this site works in terms of credit for answers but will you leave your comment as an answer so i can mark it as solved? Thanks again.
– Peter
Nov 12 at 9:03
Steve Rindsberg this was exactly what i needed! Thanks a lot! Im not exactly sure how this site works in terms of credit for answers but will you leave your comment as an answer so i can mark it as solved? Thanks again.
– Peter
Nov 12 at 9:03
|
show 4 more comments
2 Answers
2
active
oldest
votes
When you group a selection of shapes, the group becomes a new shape appended to the end of the previous z-order position. All of the individual shapes within the group are appended to the z-order after the group shape itself.
I can't find a way of determining which individual item within a group is selected (sub-selected, I guess we should say, since the original parent group remains selected and that's what PPT returns when you query ActiveWindow.Selection.ShapeRange(1).
To identify the currently subselected item within a group, you can use this as a starting point:
Sub WorkWithSubSelectedShapes()
' Do stuff with sub-selected shapes within a group
' Courtesy of Andy Pope
Dim oSh As Shape
Dim oGSh As Shape
Dim x As Long
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Do something with each shape in the group:
For Each oGSh In oSh.GroupItems
Debug.Print oGSh.TextFrame.TextRange.Text
Next
' Now do something with each SUB-SELECTED
' shape within the group
With ActiveWindow.Selection.ChildShapeRange
For x = 1 To .Count
Debug.Print .Item(x).Name
Debug.Print .Item(x).TextFrame.TextRange.Text
Next
End With
End Sub
Here's some code that may help generally in processing shapes/groups. It takes account of the fact that there might be groups within groups (within groups (within groups)) ...
Sub ProcessShapes()
Dim oSh As Shape
For Each oSh In ActivePresentation.Slides(1).Shapes
If oSh.Type = msoGroup Then
Debug.Print "GROUP" & vbTab & oSh.Name & vbTab & oSh.ZOrderPosition
Call DealWithGroup(oSh)
Else
Debug.Print oSh.Name & vbTab & oSh.ZOrderPosition
End If
Next
End Sub
Sub DealWithGroup(oSh As Shape)
Dim x As Long
For x = 1 To oSh.GroupItems.Count
If oSh.GroupItems(x).Type = msoGroup Then
Call DealWithGroup(oSh.GroupItems(x))
Else
Debug.Print "GROUP ITEM" & vbTab & oSh.GroupItems(x).Name & vbTab & oSh.GroupItems(x).ZOrderPosition
End If
Next
End Sub
And to answer Peter's further (excellent) question, this should work:
Sub TestIndexOf()
MsgBox IndexOf(ActiveWindow.Selection.ShapeRange(1))
End Sub
Function IndexOf(oSh As Shape) As Long
Dim x As Long
With ActiveWindow.Selection.SlideRange.Shapes
For x = 1 To .Count
If .Item(x).Name = oSh.Name Then
' Found it, report it
IndexOf = x
End If
Next
End With
End Function
1
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
add a comment |
Try the few lines of code and yow will get names of all the shapes in immediate window (Cttl+G to view Immediate window)
Dim shp As Shape, I As Integer
For Each shp In ActivePresentation.Slides(1).Shapes
I = I + 1
Debug.Print "Index=" & I & " Name= " & shp.Name & " ID= " & shp.Id & " Type= " & shp.Type
Next
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242865%2fget-shape-index-in-powerpoint-vba%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you group a selection of shapes, the group becomes a new shape appended to the end of the previous z-order position. All of the individual shapes within the group are appended to the z-order after the group shape itself.
I can't find a way of determining which individual item within a group is selected (sub-selected, I guess we should say, since the original parent group remains selected and that's what PPT returns when you query ActiveWindow.Selection.ShapeRange(1).
To identify the currently subselected item within a group, you can use this as a starting point:
Sub WorkWithSubSelectedShapes()
' Do stuff with sub-selected shapes within a group
' Courtesy of Andy Pope
Dim oSh As Shape
Dim oGSh As Shape
Dim x As Long
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Do something with each shape in the group:
For Each oGSh In oSh.GroupItems
Debug.Print oGSh.TextFrame.TextRange.Text
Next
' Now do something with each SUB-SELECTED
' shape within the group
With ActiveWindow.Selection.ChildShapeRange
For x = 1 To .Count
Debug.Print .Item(x).Name
Debug.Print .Item(x).TextFrame.TextRange.Text
Next
End With
End Sub
Here's some code that may help generally in processing shapes/groups. It takes account of the fact that there might be groups within groups (within groups (within groups)) ...
Sub ProcessShapes()
Dim oSh As Shape
For Each oSh In ActivePresentation.Slides(1).Shapes
If oSh.Type = msoGroup Then
Debug.Print "GROUP" & vbTab & oSh.Name & vbTab & oSh.ZOrderPosition
Call DealWithGroup(oSh)
Else
Debug.Print oSh.Name & vbTab & oSh.ZOrderPosition
End If
Next
End Sub
Sub DealWithGroup(oSh As Shape)
Dim x As Long
For x = 1 To oSh.GroupItems.Count
If oSh.GroupItems(x).Type = msoGroup Then
Call DealWithGroup(oSh.GroupItems(x))
Else
Debug.Print "GROUP ITEM" & vbTab & oSh.GroupItems(x).Name & vbTab & oSh.GroupItems(x).ZOrderPosition
End If
Next
End Sub
And to answer Peter's further (excellent) question, this should work:
Sub TestIndexOf()
MsgBox IndexOf(ActiveWindow.Selection.ShapeRange(1))
End Sub
Function IndexOf(oSh As Shape) As Long
Dim x As Long
With ActiveWindow.Selection.SlideRange.Shapes
For x = 1 To .Count
If .Item(x).Name = oSh.Name Then
' Found it, report it
IndexOf = x
End If
Next
End With
End Function
1
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
add a comment |
When you group a selection of shapes, the group becomes a new shape appended to the end of the previous z-order position. All of the individual shapes within the group are appended to the z-order after the group shape itself.
I can't find a way of determining which individual item within a group is selected (sub-selected, I guess we should say, since the original parent group remains selected and that's what PPT returns when you query ActiveWindow.Selection.ShapeRange(1).
To identify the currently subselected item within a group, you can use this as a starting point:
Sub WorkWithSubSelectedShapes()
' Do stuff with sub-selected shapes within a group
' Courtesy of Andy Pope
Dim oSh As Shape
Dim oGSh As Shape
Dim x As Long
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Do something with each shape in the group:
For Each oGSh In oSh.GroupItems
Debug.Print oGSh.TextFrame.TextRange.Text
Next
' Now do something with each SUB-SELECTED
' shape within the group
With ActiveWindow.Selection.ChildShapeRange
For x = 1 To .Count
Debug.Print .Item(x).Name
Debug.Print .Item(x).TextFrame.TextRange.Text
Next
End With
End Sub
Here's some code that may help generally in processing shapes/groups. It takes account of the fact that there might be groups within groups (within groups (within groups)) ...
Sub ProcessShapes()
Dim oSh As Shape
For Each oSh In ActivePresentation.Slides(1).Shapes
If oSh.Type = msoGroup Then
Debug.Print "GROUP" & vbTab & oSh.Name & vbTab & oSh.ZOrderPosition
Call DealWithGroup(oSh)
Else
Debug.Print oSh.Name & vbTab & oSh.ZOrderPosition
End If
Next
End Sub
Sub DealWithGroup(oSh As Shape)
Dim x As Long
For x = 1 To oSh.GroupItems.Count
If oSh.GroupItems(x).Type = msoGroup Then
Call DealWithGroup(oSh.GroupItems(x))
Else
Debug.Print "GROUP ITEM" & vbTab & oSh.GroupItems(x).Name & vbTab & oSh.GroupItems(x).ZOrderPosition
End If
Next
End Sub
And to answer Peter's further (excellent) question, this should work:
Sub TestIndexOf()
MsgBox IndexOf(ActiveWindow.Selection.ShapeRange(1))
End Sub
Function IndexOf(oSh As Shape) As Long
Dim x As Long
With ActiveWindow.Selection.SlideRange.Shapes
For x = 1 To .Count
If .Item(x).Name = oSh.Name Then
' Found it, report it
IndexOf = x
End If
Next
End With
End Function
1
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
add a comment |
When you group a selection of shapes, the group becomes a new shape appended to the end of the previous z-order position. All of the individual shapes within the group are appended to the z-order after the group shape itself.
I can't find a way of determining which individual item within a group is selected (sub-selected, I guess we should say, since the original parent group remains selected and that's what PPT returns when you query ActiveWindow.Selection.ShapeRange(1).
To identify the currently subselected item within a group, you can use this as a starting point:
Sub WorkWithSubSelectedShapes()
' Do stuff with sub-selected shapes within a group
' Courtesy of Andy Pope
Dim oSh As Shape
Dim oGSh As Shape
Dim x As Long
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Do something with each shape in the group:
For Each oGSh In oSh.GroupItems
Debug.Print oGSh.TextFrame.TextRange.Text
Next
' Now do something with each SUB-SELECTED
' shape within the group
With ActiveWindow.Selection.ChildShapeRange
For x = 1 To .Count
Debug.Print .Item(x).Name
Debug.Print .Item(x).TextFrame.TextRange.Text
Next
End With
End Sub
Here's some code that may help generally in processing shapes/groups. It takes account of the fact that there might be groups within groups (within groups (within groups)) ...
Sub ProcessShapes()
Dim oSh As Shape
For Each oSh In ActivePresentation.Slides(1).Shapes
If oSh.Type = msoGroup Then
Debug.Print "GROUP" & vbTab & oSh.Name & vbTab & oSh.ZOrderPosition
Call DealWithGroup(oSh)
Else
Debug.Print oSh.Name & vbTab & oSh.ZOrderPosition
End If
Next
End Sub
Sub DealWithGroup(oSh As Shape)
Dim x As Long
For x = 1 To oSh.GroupItems.Count
If oSh.GroupItems(x).Type = msoGroup Then
Call DealWithGroup(oSh.GroupItems(x))
Else
Debug.Print "GROUP ITEM" & vbTab & oSh.GroupItems(x).Name & vbTab & oSh.GroupItems(x).ZOrderPosition
End If
Next
End Sub
And to answer Peter's further (excellent) question, this should work:
Sub TestIndexOf()
MsgBox IndexOf(ActiveWindow.Selection.ShapeRange(1))
End Sub
Function IndexOf(oSh As Shape) As Long
Dim x As Long
With ActiveWindow.Selection.SlideRange.Shapes
For x = 1 To .Count
If .Item(x).Name = oSh.Name Then
' Found it, report it
IndexOf = x
End If
Next
End With
End Function
When you group a selection of shapes, the group becomes a new shape appended to the end of the previous z-order position. All of the individual shapes within the group are appended to the z-order after the group shape itself.
I can't find a way of determining which individual item within a group is selected (sub-selected, I guess we should say, since the original parent group remains selected and that's what PPT returns when you query ActiveWindow.Selection.ShapeRange(1).
To identify the currently subselected item within a group, you can use this as a starting point:
Sub WorkWithSubSelectedShapes()
' Do stuff with sub-selected shapes within a group
' Courtesy of Andy Pope
Dim oSh As Shape
Dim oGSh As Shape
Dim x As Long
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Do something with each shape in the group:
For Each oGSh In oSh.GroupItems
Debug.Print oGSh.TextFrame.TextRange.Text
Next
' Now do something with each SUB-SELECTED
' shape within the group
With ActiveWindow.Selection.ChildShapeRange
For x = 1 To .Count
Debug.Print .Item(x).Name
Debug.Print .Item(x).TextFrame.TextRange.Text
Next
End With
End Sub
Here's some code that may help generally in processing shapes/groups. It takes account of the fact that there might be groups within groups (within groups (within groups)) ...
Sub ProcessShapes()
Dim oSh As Shape
For Each oSh In ActivePresentation.Slides(1).Shapes
If oSh.Type = msoGroup Then
Debug.Print "GROUP" & vbTab & oSh.Name & vbTab & oSh.ZOrderPosition
Call DealWithGroup(oSh)
Else
Debug.Print oSh.Name & vbTab & oSh.ZOrderPosition
End If
Next
End Sub
Sub DealWithGroup(oSh As Shape)
Dim x As Long
For x = 1 To oSh.GroupItems.Count
If oSh.GroupItems(x).Type = msoGroup Then
Call DealWithGroup(oSh.GroupItems(x))
Else
Debug.Print "GROUP ITEM" & vbTab & oSh.GroupItems(x).Name & vbTab & oSh.GroupItems(x).ZOrderPosition
End If
Next
End Sub
And to answer Peter's further (excellent) question, this should work:
Sub TestIndexOf()
MsgBox IndexOf(ActiveWindow.Selection.ShapeRange(1))
End Sub
Function IndexOf(oSh As Shape) As Long
Dim x As Long
With ActiveWindow.Selection.SlideRange.Shapes
For x = 1 To .Count
If .Item(x).Name = oSh.Name Then
' Found it, report it
IndexOf = x
End If
Next
End With
End Function
edited Nov 14 at 15:15
answered Nov 12 at 15:42
Steve Rindsberg
10.1k11526
10.1k11526
1
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
add a comment |
1
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
1
1
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
Thanks @SteveRindsberg I had hoped for a more clean way (e.g., like ZOrderPosition without having to loop through shapes) but i guess this gets the job done. One small edit i would make is to change it to compare .ID instead of .Name since (as far as i understand) the ID is unique while the Name is not. Then it will also work if multiple shapes on the slide has the same name.
– Peter
Nov 14 at 22:58
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
@Peter Good point. While names are theoretically unique, PowerPoint has a habit of sometimes creating multiple same-named shape, which can cause all sorts of odd problems.
– Steve Rindsberg
Nov 15 at 19:02
add a comment |
Try the few lines of code and yow will get names of all the shapes in immediate window (Cttl+G to view Immediate window)
Dim shp As Shape, I As Integer
For Each shp In ActivePresentation.Slides(1).Shapes
I = I + 1
Debug.Print "Index=" & I & " Name= " & shp.Name & " ID= " & shp.Id & " Type= " & shp.Type
Next
add a comment |
Try the few lines of code and yow will get names of all the shapes in immediate window (Cttl+G to view Immediate window)
Dim shp As Shape, I As Integer
For Each shp In ActivePresentation.Slides(1).Shapes
I = I + 1
Debug.Print "Index=" & I & " Name= " & shp.Name & " ID= " & shp.Id & " Type= " & shp.Type
Next
add a comment |
Try the few lines of code and yow will get names of all the shapes in immediate window (Cttl+G to view Immediate window)
Dim shp As Shape, I As Integer
For Each shp In ActivePresentation.Slides(1).Shapes
I = I + 1
Debug.Print "Index=" & I & " Name= " & shp.Name & " ID= " & shp.Id & " Type= " & shp.Type
Next
Try the few lines of code and yow will get names of all the shapes in immediate window (Cttl+G to view Immediate window)
Dim shp As Shape, I As Integer
For Each shp In ActivePresentation.Slides(1).Shapes
I = I + 1
Debug.Print "Index=" & I & " Name= " & shp.Name & " ID= " & shp.Id & " Type= " & shp.Type
Next
answered Nov 11 at 0:49
Ahmed AU
67028
67028
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242865%2fget-shape-index-in-powerpoint-vba%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The id should give the order they were added to the slide.
– QHarr
Nov 10 at 20:33
Yes but how can i translate an ID into an index? The Index is always numbered 1 to number of shapes on slide while ID can be anything with gaps in between (e.g., it could be that index 1 has ID 5 and index 2 has ID 211). It seems like there should be an easy way to just get the index..
– Peter
Nov 10 at 21:08
I don't think a separate list of indices is maintained for the Shapes collection. You could write a loop over the shapes collection to assign names you can use to differentiate.
– QHarr
Nov 10 at 21:19
" My problem is i dont know how to get the index number of a given shape." If by "given" you mean selected, then ActiveWindow.Selection.ShapeRange(1)..ZOrderPosition will give you the index.
– Steve Rindsberg
Nov 11 at 16:00
Steve Rindsberg this was exactly what i needed! Thanks a lot! Im not exactly sure how this site works in terms of credit for answers but will you leave your comment as an answer so i can mark it as solved? Thanks again.
– Peter
Nov 12 at 9:03