PDA

View Full Version : Dynamically creating activex controls query



n12nja
09-09-2015, 04:25 AM
Hi guys - I'm hoping someone far more knowledgeable about VB will be kind enough to help me.

"TLDR Version - How do I replace this label with an activex version of a label added to the slide master



Dim mySh As Shape
ActivePresentation.Designs(1).SlideMaster.Shapes.AddLabel(msoTextOrientatio nHorizontal, 10, 10, 400, 50)

"

In context:

I am creating a powerpoint in which the user can enter text into a text box on the powerpoint while the show is running and it will then populate every slide a text label with what was typed.
I have successfully managed to get to the stage where it will find the slide with the text box on it - and upon the slide being changed to the next one it will then create a text label in the SlideMaster which displays on every slide.
The problem I now have is if the presentation was to be run again it will then recreate the text label - stacking them on top of each other. It sounds simple - all I should need to do is check if the label already exists - if it does amend it, if not create it.
When I try to check if the text label already exists - it seems the label is not successfully named and thus cannot be referred to. I think it is because it is creating a standard text label and not an activex control thus preventing it being referenced later by name (or if it is I'm not sure how)
Please find below my code



Dim waltInfo As String 'This string is used to store the value input during the show from textbox 'WaltMainBox which exists on one of the normal slides within the slideshow

Function ShapeNamed(sShapeName As String, oSl As Slide) As Shape 'this function finds if the 'shape is present and is used to check when the slide after the slide with the textbox on it has 'been shown
Dim oSh As Shape
For Each oSh In oSl.Shapes
If oSh.Name = sShapeName Then
Set ShapeNamed = oSh
Exit Function
End If
Next
End Function


Sub OnSlideShowPageChange()
Dim sldHeight As Single
sldHeight = ActivePresentation.PageSetup.SlideHeight
Dim sldWidth As Single
sldWidth = ActivePresentation.PageSetup.SlideWidth
Dim WaltCopy As Shape


Dim oSh As Shape


Set oSh = ShapeNamed("WaltMainBox", ActivePresentation.Slides(ActivePresentation.SlideShowWindow.View.Slide.Sli deIndex - 1))
If Not oSh Is Nothing Then
'the slide with the inputbox on it is found so transfer information from it to waltInfo
waltInfo = oSh.OLEFormat.Object.Text


Dim x As Long
Dim mySh As Shape


With ActivePresentation


' On each slidemaster in the presentation add the label
For x = 1 To .Designs.Count


Set mySh = _
.Designs(x).SlideMaster.Shapes.AddLabel(msoTextOrientationHorizontal, 10, 10, sldWidth - 20, 50)



mySh.Name = "WaltLabel" 'I hoped this would allow me to reference this label later but 'doesn't seem to help


With mySh.TextFrame.TextRange
.Font.Name = "Arial"
.Font.Size = 18
.Text = waltInfo


End With


Next
End With




End If
End Sub



Many thanks for any assistance you can give! =)

Darren

n12nja
09-09-2015, 09:42 AM
Alternative solution that I also don't know how to implement:
If I create an activex control on the master in advance in the normal manner (non code) - if I could make it so that if someone changes the slide design the activex label on the slidemaster remains even though the rest of the design changes as normal.
This would then allow me to code it so that the activex control is amended but never needs creating.

Nm - it seems changing design doesn't change the master so I guess this works..will amend and see!

n12nja
09-09-2015, 01:35 PM
I've tried to place an ActiveX label on the Master Slide but when I try to reference it to change it's value I get an error that it isn't an OLE Object =(.

Any ideas please?


ActivePresentation.Designs(1).SlideMaster.Shapes("WaltLabel").OLEFormat.Object.Caption = waltInfo

John Wilson
09-10-2015, 01:27 AM
That code should work. Are you sure the name is correct?

n12nja
09-13-2015, 09:29 AM
Thank you for your reply John, gave me the confidence to give it another go.

I've still no idea why, but upon coming back to it today and hey presto it's working.

Final code here in case it's useful to anyone in the future:


Dim waltInfo As String

Function ShapeNamed(sShapeName As String, oSl As Slide) As Shape
Dim oSh As Shape
For Each oSh In oSl.Shapes
If oSh.Name = sShapeName Then
Set ShapeNamed = oSh
Exit Function
End If
Next
End Function






Sub OnSlideShowPageChange()
If ActivePresentation.SlideShowWindow.View.Slide.SlideIndex = 1 Then
ActivePresentation.Designs(1).SlideMaster.Shapes("WaltLabel").OLEFormat.Object.Caption = ""
End If




Dim oSh As Shape


Set oSh = ShapeNamed("WaltMainBox", ActivePresentation.Slides(ActivePresentation.SlideShowWindow.View.Slide.Sli deIndex - 1))
If Not oSh Is Nothing Then

waltInfo = oSh.OLEFormat.Object.Text


ActivePresentation.Designs(1).SlideMaster.Shapes("WaltLabel").OLEFormat.Object.Caption = waltInfo








Else
' no shape by that name;
End If
End Sub