Solved

toggle between different alignments of pictures by means of VBA

Posted on 2012-12-20
5
395 Views
Last Modified: 2012-12-21
Dear Experts:

below macros allow me to align pictures on the slides of the active presentation.
Thank you again to JRS Wilsion for that. Now I would like to tweak that code even more.

The user should be able to press a toggle button to toggle between different alignments.

The first code lets the user align all the pictures on all the slides of the active presentation vertically and horizontally centered

The second code lets the user align the pictures on all the slides of the active presentation horizontally centered but the vertical alignment is '.Top = 180'

The third code lets the user align the pictures on all the slides of the active presentation horizontally centered but the vertical alignment is '.Top = 250'

As a matter of fact, I got no idea how to combine these codes so that repeated pressing of the macro button toogles between these different states of alignment. I hope this is not asking too much.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub ResizeAndPosition_Pictures_ToggleButton_1()
' pictures horizontally and vertically centered

Dim i As Integer
Dim oshp As Shape
Dim oshpR As ShapeRange
For i = 1 To ActivePresentation.Slides.Count
For Each oshp In ActivePresentation.Slides(i).Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.ContainedType = msoPicture Then
oshp.ScaleHeight 0.7, True
Set oshpR = ActivePresentation.Slides(i).Shapes.Range(oshp.ZOrderPosition)
oshpR.Align msoAlignCenters, True
oshpR.Align msoAlignMiddles, True
End If
Else
If oshp.Type = msoPicture Then
oshp.ScaleHeight 0.7, True
Set oshpR = ActivePresentation.Slides(i).Shapes.Range(oshp.ZOrderPosition)
oshpR.Align msoAlignCenters, True
oshpR.Align msoAlignMiddles, True
End If
End If
Next oshp
Next i
End Sub

Open in new window



                                           
Sub ResizeAndPosition_Pictures_ToggleButton_2()
'pictures horizontally centered, .Top Property = 180

Dim i As Integer
Dim oshp As Shape
Dim oshpR As ShapeRange
For i = 1 To ActivePresentation.Slides.Count
For Each oshp In ActivePresentation.Slides(i).Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.ContainedType = msoPicture Then
oshp.ScaleHeight 0.7, True
Set oshpR = ActivePresentation.Slides(i).Shapes.Range(oshp.ZOrderPosition)
oshpR.Align msoAlignCenters, True
oshp.Top = 180
End If
Else
If oshp.Type = msoPicture Then
oshp.ScaleHeight 0.7, True
Set oshpR = ActivePresentation.Slides(i).Shapes.Range(oshp.ZOrderPosition)
oshpR.Align msoAlignCenters, True
oshp.Top = 180
End If
End If
Next oshp
Next i

Open in new window



Sub ResizeAndPosition_Pictures_ToggleButton_3()
'horizontally centered, .Top Property = 250
Dim i As Integer
Dim oshp As Shape
Dim oshpR As ShapeRange
For i = 1 To ActivePresentation.Slides.Count
For Each oshp In ActivePresentation.Slides(i).Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.ContainedType = msoPicture Then
oshp.ScaleHeight 0.7, True
Set oshpR = ActivePresentation.Slides(i).Shapes.Range(oshp.ZOrderPosition)
oshpR.Align msoAlignCenters, True
oshp.Top = 250
End If
Else
If oshp.Type = msoPicture Then
oshp.ScaleHeight 0.7, True
Set oshpR = ActivePresentation.Slides(i).Shapes.Range(oshp.ZOrderPosition)
oshpR.Align msoAlignCenters, True
oshp.Top = 250
End If
End If
Next oshp
Next i

Open in new window

0
Comment
Question by:AndreasHermle
  • 2
  • 2
5 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 100 total points
ID: 38710238
Hi Andreas,
You can use the caption of a Toggle button to tell what comes next to the user and to the code.
Sub TriToggle_Click()
    Select Case TriToggle.Caption
        Case "Centre"
            ResizeAndPosition_Pictures_ToggleButton_1
            TriToggle.Caption = "180"
        Case "180"
            ResizeAndPosition_Pictures_ToggleButton_2
            TriToggle.Caption = "240"
        Case "240"
            ResizeAndPosition_Pictures_ToggleButton_3
            TriToggle.Caption = "Centre"
    End Select
End Sub

Open in new window

0
 
LVL 23

Assisted Solution

by:JSRWilson
JSRWilson earned 400 total points
ID: 38710482
I think I'd use a combo box
Demo here
0
 

Author Comment

by:AndreasHermle
ID: 38712170
Dear JSRWilson, dear Graham,

thank you very much for your swift and professional support. Both your solutions are workable and will come in handy in other circumstances.

As a matter of fact I am looking for a solution where all three of the above codes get integrated just in one using the SELECT CASE Statement. I have worked with SELECT CASE statements in Word und Excel but for some reason I cannot get this one working with the codes above.

Regards, Andreas
0
 
LVL 23

Accepted Solution

by:
JSRWilson earned 400 total points
ID: 38712202
Insert a normal shape with text  "Centre" and this code run from it

Sub trigger(oshp As Shape)
Static choice As Long
choice = choice + 1
If choice = 4 Then choice = 1
Select Case choice
Case Is = 1
oshp.TextFrame.TextRange = "180"
Call ResizeAndPosition_Pictures_ToggleButton_1
Case Is = 2
oshp.TextFrame.TextRange = "270"
Call ResizeAndPosition_Pictures_ToggleButton_2
Case Is = 3
oshp.TextFrame.TextRange = "Centre"
Call ResizeAndPosition_Pictures_ToggleButton_3
End Select
End Sub

(and your other code of course)
0
 

Author Closing Comment

by:AndreasHermle
ID: 38712299
Great support from professional experts.

Thank you very much. I really appreciate it.

Regards, Andreas
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now