If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
How do you work with simple arrays in VBA?
PPT 2003/2007
I'm trying to understand arrays. I just want to capture the slides currently selected and then running some actions on each of them, which causes the selection to be lost, so I have to capture the slide numbers before the action loop. Likewise, I'm trying to do the same with selected shapes on a slide, where I want to run a loop for each selected shape, but in doing so I loose the selection so I need to first capture the selected shapes names (I guess) and then run on all of them. I've read and tried several things but it escapes me. Can someone show me how this is done? Here's a feeble attempt I made... Sub TestArray() Dim iSldSelCnt As Integer iSldSelCnt = ActiveWindow.Selection.SlideRange.Count Dim iSldSelNum() As Integer ReDim iSldSelNum(iSldSelCnt - 1) As Integer Dim iSld As Long iSld = 0 Dim oSld As Slide For Each oSld In ActiveWindow.Selection.SlideRange iSldSelNum(iSld) = oSld.SlideNumber iSld = iSld + 1 Next oSld ActivePresentation.Slides(iSldSelNum).Shapes.AddSh ape _ Type:=msoShapeRectangle, _ Left:=50, Top:=50, Width:=100, Height:=200 'ActiveWindow.Selection.SlideRange(iSldSelNum).Sha pes.AddShape _ ' Type:=msoShapeRectangle, _ ' Left:=50, Top:=50, Width:=100, Height:=200 End Sub Thanks, -Melina |
#3
|
|||
|
|||
How do you work with simple arrays in VBA?
The challenge is I need loop through objects in just the slides I
select, not all of slides. So, out of 10 slides I selected slides 2, 4, and 8. I need to loop through objects only on those slides. As I do the actions on one slide, the other slides selected loose their selection and PPT can find the next slide in the selection because it's now de-selected. The same happens with selected shapes, I noticed. When I select 3 out 7 of my slide shapes, once I start working with them, deleting, replacing, etc, PPT deselects the other shapes. Is there a way to make the routine collect the slide numbers or shape names in the selection beforehand and then loop through each of those? Thanks, -Melina On Dec 2, 3:21*pm, David Marcovitz wrote: Don't use selection to do what you want. It is rarely a good idea. I have a section in my book about arrays that might help you understand them. Instead of: For Each oSld In ActiveWindow.Selection.SlideRange Try For Each oSld In ActivePresentation.Slides Or if you have to do this with selected slides, get your parameters at the beginning, such as: start = ActiveWindow.Selection.SlideRange(1).SlideIndex end = ActiveWindow.Selection.SlideRange.Count For i = start To End ... Next i Sorry, I have class in 10 minutes so I don't have time to look over this any more. One more quick thing. Put the Dim at the top of the procedure and keep the ReDim where it is. --David On 12/2/09 4:01 PM, in article , "Mel" wrote: PPT 2003/2007 I'm trying to understand arrays. I just want to capture the slides currently selected and then running some actions on each of them, which causes the selection to be lost, so I have to capture the slide numbers before the action loop. Likewise, I'm trying to do the same with selected shapes on a slide, where I want to run a loop for each selected shape, but in doing so I loose the selection so I need to first capture the selected shapes names (I guess) and then run on all of them. I've read and tried several things but it escapes me. Can someone show me how this is done? Here's a feeble attempt I made... Sub TestArray() *Dim iSldSelCnt As Integer *iSldSelCnt = ActiveWindow.Selection.SlideRange.Count *Dim iSldSelNum() As Integer *ReDim iSldSelNum(iSldSelCnt - 1) As Integer *Dim iSld As Long *iSld = 0 *Dim oSld As Slide *For Each oSld In ActiveWindow.Selection.SlideRange * iSldSelNum(iSld) = oSld.SlideNumber * iSld = iSld + 1 *Next oSld *ActivePresentation.Slides(iSldSelNum).Shapes.AddS hape _ * Type:=msoShapeRectangle, _ * Left:=50, Top:=50, Width:=100, Height:=200 'ActiveWindow.Selection.SlideRange(iSldSelNum).Sha pes.AddShape _ ' Type:=msoShapeRectangle, _ ' Left:=50, Top:=50, Width:=100, Height:=200 End Sub Thanks, -Melina -- David M. Marcovitz Author of _Powerful PowerPoint for Educators_http://www.PowerfulPowerPoint.com/ Microsoft PowerPoint MVP Associate Professor, Loyola University Maryland |
#4
|
|||
|
|||
How do you work with simple arrays in VBA?
I tried learning about using collections instead of arrays and wrote
the following which in my tests collected slide numbers and shape names and looped through them after the selection was deselected. Any comments on using collections as opposed to arrays like I'm doing below? Thanks, Melina Sub WorkSelectedSlidesDeselected() Dim oSld As Slide Dim oSldNum As Variant Dim SelectedSlides As New Collection Dim oPrez As Presentation Set oPrez = ActivePresentation 'Note current selected slides For Each oSld In ActiveWindow.Selection.SlideRange SelectedSlides.Add oSld.SlideNumber Next oSld 'Loose slide selection oPrez.Slides(1).Select 'Loop through originally selected slides anyway For Each oSldNum In SelectedSlides ActivePresentation.Slides(oSldNum).Shapes.AddLabel _ (Orientation:=msoTextOrientationHorizontal, _ Left:=100, Top:=100, Width:=60, Height:=150) _ .TextFrame.TextRange.Text = "Hello world." Next oSldNum End Sub Sub WorkSelectedShapesDeselected() Dim oShp As Shape Dim oShpNm As Variant Dim SelectedShapes As New Collection Dim oPrez As Presentation Set oPrez = ActivePresentation Dim iCurrSld As Long iCurrSld = ActiveWindow.Selection.SlideRange.SlideNumber 'Note current selected shapes For Each oShp In ActiveWindow.Selection.ShapeRange SelectedShapes.Add oShp.Name Next oShp 'Loose shape selection ActiveWindow.Selection.Unselect 'Loop through originally selected shapes anyway For Each oShpNm In SelectedShapes oPrez.Slides(iCurrSld).Shapes(oShpNm).Flip msoFlipHorizontal Next oShpNm End Sub On Dec 2, 3:01*pm, Mel wrote: PPT 2003/2007 I'm trying to understand arrays. I just want to capture the slides currently selected and then running some actions on each of them, which causes the selection to be lost, so I have to capture the slide numbers before the action loop. Likewise, I'm trying to do the same with selected shapes on a slide, where I want to run a loop for each selected shape, but in doing so I loose the selection so I need to first capture the selected shapes names (I guess) and then run on all of them. I've read and tried several things but it escapes me. Can someone show me how this is done? Here's a feeble attempt I made... Sub TestArray() *Dim iSldSelCnt As Integer *iSldSelCnt = ActiveWindow.Selection.SlideRange.Count *Dim iSldSelNum() As Integer *ReDim iSldSelNum(iSldSelCnt - 1) As Integer *Dim iSld As Long *iSld = 0 *Dim oSld As Slide *For Each oSld In ActiveWindow.Selection.SlideRange * iSldSelNum(iSld) = oSld.SlideNumber * iSld = iSld + 1 *Next oSld *ActivePresentation.Slides(iSldSelNum).Shapes.AddS hape _ * Type:=msoShapeRectangle, _ * Left:=50, Top:=50, Width:=100, Height:=200 'ActiveWindow.Selection.SlideRange(iSldSelNum).Sha pes.AddShape _ ' Type:=msoShapeRectangle, _ ' Left:=50, Top:=50, Width:=100, Height:=200 End Sub Thanks, -Melina |
Thread Tools | |
Display Modes | |
|
|