PDA

View Full Version : Check if shape is in specific location in slide



jonofisher
08-02-2007, 07:02 AM
Hi - I posted something last night with respect to sorting slides absed on a shape value and have been trying to break the problem down into parts and find answers for each part.

My first requirement is to check whether there is a shape at a certain spot on the slide. I have no idea how to do this. Could someone give me a pointer? I know it must be something involving "if shape.....left 156.....right 516.75 (co-ordinates i want)... but have no idea how to write it.

Thanks

jFish

TrippyTom
08-02-2007, 10:28 AM
I think it would be easier to name your shape when you create it, then check for the name in your if/then statement.

I did something like this when I wanted to add/remove guidelines on a slide (because I got sick of people moving them around all the time). Below is the code I used.


Sub GuidesAdd()
On Error Resume Next
Dim shp As Shape
Dim sld As Slide
Dim mySlide As Integer
Dim I As Long
'using the page setup of 10" wide (0-720) midpoint = 5.0 inches
' 7.5" tall (0-540) midpoint = 3.75 inches
' 1" = 72 pixels
' Position Values:
' x1 = 25.2 y1 = 48.96
' x2 = 342.72 y2 = 65.52
' x3 = 377.28 y3 = 102.96
' x4 = 694.80 y4 = 122.40
' y5 = 298.08
' y6 = 474.48
mySlide = ActiveWindow.View.Slide.SlideIndex
I = mySlide
'this part checks to see if the lines exist first, and if they do,
'it doesn't add more on top of the same location
For Each shp In ActivePresentation.Slides(mySlide).Shapes.Range
Select Case shp.Name
Case Is = "y1" & mySlide
shp.Delete
Exit Sub
End Select
Next shp
'This part draws the lines (top, left, width, height)
For Each sld In ActivePresentation.Slides(mySlide)
DoLine I, "y1", 48.96, 0, 720, 0
DoLine I, "y2", 65.52, 0, 720, 0
DoLine I, "y3", 102.96, 0, 720, 0
DoLine I, "y4", 122.4, 0, 720, 0
DoLine I, "y5", 298.08, 0, 720, 0
DoLine I, "y6", 474.48, 0, 720, 0
DoLine I, "x1", 0, 25.2, 0, 540
DoLine I, "x2", 0, 342.72, 0, 540
DoLine I, "x3", 0, 377.28, 0, 540
DoLine I, "x4", 0, 694.8, 0, 540
Next
End Sub


You may notice, that part where it checks to see if the lines exist first. Hopefully that helps your question a little bit.

TrippyTom
08-02-2007, 10:33 AM
oh by the way, here's the DoLine process it calls in the above macro:


Sub DoLine(I As Long, Nm As String, Tp As Single, Lft As Single, Wdth As Single, Ht As Single)
Dim myColor As Long
myColor = RGB(255, 0, 0) 'red <-- Change this value to whatever you want if you don't like red
With ActivePresentation.Slides(I).Shapes.AddShape(Type:=msoShapeRectangle, Top:=Tp, Left:=Lft, width:=Wdth, height:=Ht)
.Name = Nm & CStr(I)
.Line.ForeColor.RGB = myColor
End With
End Sub

jonofisher
08-02-2007, 10:49 AM
Hi Trippy - Thanks for the suggestion. I have actually seen that kind of macro before (custom gridlines) and wondered how that worked..

Unfortunately i can't name the boxes because i need to be able to run this macro on lots of old presentations, where the boxes are in the right spot, but not named.

But i have made some progress. I have succeeded in moving my slides to the back of the presentation, and am now having a few problems with matching. Any further suggestions on my (awfully written) code would be appreciated:


Sub sort()
Dim sld As Slide
Dim Preslength As Long
Dim sh As Shape
Dim sh2 As Shape
Dim txtrng As Long
'if slide has a shape at 156 and 516.75 then move the slide to the back of the presentation. These slides are referred
'to in comments as "facing pages"
For Each sld In ActivePresentation.Slides
For Each sh In sld.Shapes
If sh.Left = 156 And sh.Top = 516.75 Then
sld.MoveTo 7 'instead of being 7, needs to move to end of presentation - define preslength variable?
End If
Next
Next
'now for each slide with a text box at that position, select text in box
For Each sld In ActivePresentation.Slides
For Each sh In sld.Shapes
If sh.Left = 156 And sh.Top = 516.75 Then
txtbox = sh.TextFrame.TextRange
txtrng2 = sh.TextFrame.TextRange.Text
fpno = Mid(txtrng2, 13, 4) 'this picks up the relevant bit of text in the facing page text box - starts at character 13
MsgBox (fpno)
compare
End If
Next
Next
End Sub

Sub compare()
Dim sh2 As Shape
For Each sld In ActivePresentation.Slides
For Each sh2 In sld.Shapes
'find the text (secnum) that we want to compare the facing page text number to
If sh2.Left = 156 And sh2.Top = 78 Then
txtrng = sh2.TextFrame.TextRange.Text
txtpos = InStr(1, txtrng, " ")
secnum = Left(txtrng, txtpos - 1)
MsgBox (secnum)
End If
'if secnum = fpno, then replace the text in the text box of fpno with the pgae number of the slide which secnum is on
'this is where i need some real help
'sldno = sld.SlideNumber
' If secnum = fpno Then
' With sh.TextFrame.TextRange
' .Text = sldno.Value
' End With
' End If
Next
Next
End Sub



original post below:
the situation: i would like to sort slides based on whether they have a certain shape at a certain position, and also based on matching the text in the shape to something. I will try and explain:
1. if slide has a shape (will refer to this as BOXA) at left 156, top 516.75 then send slide to back of presentation. Loop through all slides so we end up with all slides with BOXA on them at the back of the presentation - SUCCESSFULLY DONE NOW
2. For each slide
a. if there is a BOX A (ie at 156,516.75) then get the text in BOX A (this will be a section number - ie 2.1 or 2.5) - GOT THIS I THINK
b. loop through all slides and compare to the first characters (this is a section number - ie 2.1 or 2.5) of the box in position 156,78 (lets call this BoxB) (I am thinking you have to select text in BoxB, do a left function of amount of characters before the first space, and match on that)
i. if it?s the same, then take the slide number of the matched slide and put that slide number in BOX A (as text)
3. We will therefore end up with all the BOX A slides at the back, and in each BOXA will be the text of the page number which corresponds to the matched slide at the front end of the document
4. Finally sort all the Box A slides to make sure they are in correct order (numbers ascending) at the back of the document

for context, i am trying to get appendix type of slides to match to the slide number in the main part of a presentation. (ie only appendix slides have Box A on them). When writing a presentation we start off having these appendix slides in the main document, but then want to put them to the back and make sure they reference the correct number
I would appreciate any thoughts on this or any pointers on how to get started. I very much appreciate any assistance - if this works it will automate something that i spend an inordinate and frustrating amount of time doing!

Cheers

TrippyTom
08-02-2007, 11:23 AM
Could you post a sample of the PPT file you're testing this on? It's kind of hard to visualize.

jonofisher
08-02-2007, 01:49 PM
Hi

Please find test file attached.

The macro "sort" currently moves the slides OK, and also gets the text values i need to compare. But i am now a bit lost when working out how to match them.

Any assistance would be greatly appreciated.

Cheers

JFish.

TrippyTom
08-02-2007, 08:12 PM
Please, can someone help this guy out? I've looked at it for hours and I think I finally understand what he needs, but can't wrap my brain around a solution.

jonofisher
08-03-2007, 01:39 AM
thank trippy - very much appreciate the time. i will continue racking my brain and trying different stuff...

jonofisher
08-05-2007, 09:30 AM
Hi Tom

I have been puzzling over this the last couple of days and have almost got it right - the macro works as required except for one thing - the for each... next loop appears to operate in a different order to the slide order (first part of the code - when the slides get sent to the back

Would very much appreciate if you could have a quick look at the file attached and see if you can spot anything. My thoughts:
1. does for each... next operate on slideID not slide index? don't think this is the problem though because i have changed the slides around and slide 2 always comes up first
2.is there a way to force it to go from one slide number to next slide number?

Would be extremely grateful for any thoughts.

Cheers

JFish

Norie
08-05-2007, 12:08 PM
Jfish

I'm no expert on PPT VBA, think I've done like one code.

But using For Each in most other flavours of VBA, eg Excel, can be flaky when you need to do things in a particular order.

Why not loop using For To using the Slides collection Count property?

John Wilson
08-05-2007, 12:52 PM
This is kinda hard to explain but because you are changing the order of slides within the loop you are moving some "target" slides twice. eg the original slide 2 becomes slide 7 when slide 4 moves and is moved again. You will need to start at the end of the presentation and work backwards
using something like


Sub sort()
Dim i As Integer
Dim z As Integer
'if slide has a shape at 156 and 516.75 then move the slide to the back of the presentation. These slides are referred
'to in comments as "facing pages"
For i = ActivePresentation.Slides.Count To 1 Step -1
For Each sh In ActivePresentation.Slides(i).Shapes
If sh.Left = 156 And sh.Top = 516.75 Then
'move slide to end (or end -z)
ActivePresentation.Slides(i).MoveTo ActivePresentation.Slides.Count - z
z = z + 1
End If
Next
Next
This would of course give you 3, 2 ,1 but should be a starting point (note code has been edited so the order is now 1,2,3)

jonofisher
08-05-2007, 01:37 PM
Team - thanks heaps for the assistanceJohn - I now totally understand what you are talking about - moving slides twice. I have put in a do...until loop so that the looping stops before cycling through again. Thanks so much for pointing this outBrilliant result - literally this will save me hours of work a week - gotta love VBA

John Wilson
08-06-2007, 12:03 AM
Just so your not confused - I adapted my code so that the order WASN'T 3,2,1 and then forgot to take out the line that said it would be. Maybe a do > until would work but I think the secret is looping through the slides backwards so that slides moved to the end are not in the loop. I had a similiar problem with a vba program to delete slides with a given criteria. eg if slide 2 of 8 was deleted, slide 3 becomes slide 2 and the vba didn't check it but jumped straight to the "new" slide 3" (ie the old slide 4!) And then because there was now no slide 8 it crashed!

It was late in the UK

jonofisher
08-06-2007, 01:18 AM
Thanks John - i have attached my latest code, (which as you will know from my previous file, is messily written but seems to work ok). I have added in a title slide which acts to stop the loop.

There is now just one strange problem i have discovered - everything works fine, except the last "facing page" has the wrong number assigned to it - by 1. All other pages are ok. This only occurs when (if you see attached file) the last facing page is 3.1. If i remove that facing page, the rest are ok. (ie in the attached file, it will say p6 when it should be p.5. All others are ok)

Given your knowledge of loops i was wondering whether you might know what was going on here?

Thanks once again

John Wilson
08-06-2007, 01:50 AM
Maybe you need to email me

john AT technologytrish.co.uk

TrippyTom
08-08-2007, 08:24 AM
Hi peeps,

I'm curious to find out what the solution was for this (if there is one). I'm sure this knowledge will be useful for me in the future.

jonofisher
08-08-2007, 10:08 AM
Tom - file attached with my code which works - although is awfully messy.

Some times it seems to run an error so if you ever refine it please let me know.

The one thing i want to do is make it less sensitive - ie now the code requires the boxes to be in exactly the right position, whereas i would like to make it work a few pixels in either direction, as sometimes the boxes mysteriously move a little...

anyways, see attached.

cheers