PDA

View Full Version : Changing text in an existing textbox



lion
04-02-2007, 03:25 AM
Is there any way of changing either the captions of existing labels or the text in existing textboxes using vba? (I can't find out how to identify existing labels or textboxes on a slide :banghead: ) Any ideas gratefully received!: pray2:

TrippyTom
04-02-2007, 09:37 AM
Every time you add a shape to a slide, PPT assigns a name to it (like "Rectangle 6"). If you want to modify a specific shape, you will have to know this name so you can reference it in your code. Otherwise, you can create the shape in VBA and name it yourself, like this:


Sub test()
'The following example adds a rectangle to myDocument, gives it the name Red Square, and then sets its foreground color and line style.
Set myDocument = ActivePresentation.Slides(2)
With myDocument.Shapes.AddShape(Type:=msoShapeRectangle, _
Top:=144, Left:=144, Width:=72, Height:=72)
.Name = "Red Square"
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Line.DashStyle = msoLineDashDot
End With
End Sub

Once you know the name, you can refer back to that object in vba to modify it.

TrippyTom
04-02-2007, 09:44 AM
Sorry, more to the point of your question:

Sub test2()
'This example adds a vertical label that contains the text "Test Label" to myDocument.
Set myDocument = ActivePresentation.Slides(2)
myDocument.Shapes.AddLabel(Orientation:=msoTextOrientationVerticalFarEast, _
Left:=100, Top:=100, Width:=60, Height:=150).TextFrame _
.TextRange.Text = "Test Label"
End Sub

This comes straight from the VBA help files. :)

lion
04-02-2007, 01:52 PM
:hi: Thanks for the reply. It isn't quite what I was hoping for. I have made a template for a blockbusters game (to use with my class at school) and I wanted to randomise the letters in the hexagons each time it was used. I have been trying to reference labels or textboxes that are already present on the slide. I think I may be trying to do the impossible!!:(

TrippyTom
04-02-2007, 02:02 PM
Hi again,

Could you post the code and/or the file you're working with? Someone should be able to help you further if they see the specifics of what you're working with - and perhaps lead you in another direction you might not have thought of.

lion
04-02-2007, 04:51 PM
:hi: Thanks again - I'm attaching the file, it still very basic but I've posted a few notes with the code. I think I may have found a way around the problem, not ideal but better than nothing. I can change the text in text boxes using vba before running the slideshow (not during it) so will be able to randomise at this stage. Still can't change label captions (see module2). :confused2

John Wilson
04-03-2007, 04:46 AM
Hi

Firstly using labels is just complicating things and making the vba more difficult. Just use normal autoshapes with text. I have attached a version of your file with a shuffle routine and the start of a select case routine to goto the correct slide. You should be able to see how to complete this (NB only A.B.C & D work at the moment)

lion
04-03-2007, 07:14 AM
:hi: Many thanks. It works a treat!! Only problem now is that I don't know how to mark the thread as 'solved'! :thumb

John Wilson
04-04-2007, 12:48 AM
Glad to help. If you get stuck with making the correct shape change colour email me John AT SIGN technologytrish.co.uk. Can I also suggest that you change the answer inputted to lower case using LCase(strsanswer) this will avoid problems with capital letters.