My PowerPoint slides has a text boxes that have default text in it, such as "Profile Description" and when you click on it, the words "Profile Description" disappear, so you can type in the profile.
I want to find that specific text box, the one that contains the default text "Profile Description", by looking at the contents of the box (I can't use title, as the title might be different depending which slide its on).
I can find many other properties of this text, by using Slide.TextFrame.TextRange, but I can't figure out how to get that default text.
Once I click in the box and type some text, then I can access the new value using Slide.TextFrame.TextRange.Text, but I need the default text. I've looked at a ton of documentation, but I guess I'm missing it somewhere. Please point me in the right direction. Thanks.
Is that text box created from a placeholder? If so, you'll need to look at the parent placeholder on the custom layout associated with the slide, which is not trivial due the differences in collections indexing between a slide and a layout and the fact that the Name property changes each time the slide is generated from the master. Example:
oSld.CustomLayout.Shapes.Placeholders(index).TextFrame2.TextRange.Text
Note that this will work for custom text placeholders but not for built-in placeholders where text on the custom layout is dynamically replaced by PowerPoint on the slide.
Example:
Slide Master / Layout / Title Placeholder : "Click to edit Master title style"
Slide Placeholder : "Click to add title"
You could get a reference to the shape on the layout by using tags. Tags are bits of invisible meta data that you can add to presentations, slides or shapes.
This is how you add a tag to a shape on a slide:
ActivePresentation.Slides(1).Shapes(1).Tags.Add myName, myValue
You then write a function to return a shape by its tag like this:
' *************************************************************************************
' Purpose : Returns a presentation, slide or shape by its tag from a collection of
' presentations, slides or shapes
' Author : Jamie Garroch of YOUpresent.co.uk
' Inputs : TagObject - collection type to be searched. Presentations, Slides or Shapes
' TagName - The tag name to search for (always upper case)
' TagValue - The tag value to search for
' Outputs : Returns a Presentation, Slide or Shape object if a match is found
' *************************************************************************************
Public Function GetByTag(TagObject, TagName As String, TagValue As String) As Object
On Error GoTo errhandler
Select Case True
Case TypeOf TagObject Is Presentations
Dim oPres As Presentation
For Each oPres In TagObject
If TagExists(oPres, TagName, TagValue, True) Then Set GetByTag = oPres: Exit Function
Next
Case TypeOf TagObject Is Slides
Dim oSld As Slide
For Each oSld In TagObject
If TagExists(oSld, TagName, TagValue, True) Then Set GetByTag = oSld: Exit Function
Next
Case TypeOf TagObject Is Shapes
Dim oShp As Shape
For Each oShp In TagObject
If TagExists(oShp, TagName, TagValue, True) Then Set GetByTag = oShp: Exit Function
Next
End Select
Exit Function
errhandler:
DebugMsg "GetByTag Error : " & Err & " " & Err.Description
End Function
I've been trying to find a way to make this work with Tags, but couldn't.
So instead, my solution has been to pre-check the CustomLayout for the Placeholders and note the top x,y coordinates and what values they should have by default.
After, when the page has been generated, I review the shapes by x,y coordinate and if there's a match, substitute in the default value.
Related
I am trying to create a new slide and name it at the same time using VBA. I have a main menu page that contains shapes with text labels in them. I've set the action for each shape to run the following macro: It is intended to create a new slide and name it the same as the category name that is shown in text on the button the user clicked.
Sub ChangeSlideName(objCurrentShape As Shape) ' Place the clicked on shape in a variable.
Dim objTextInBox As String
Dim objCurrentSlideNum As Integer
' Place current slide number into a variable.
objCurrentSlideNum = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
' Set value of string variable to the text in the shape clicked on.
objTextInBox = ActivePresentation.Slides(objCurrentSlideNum).Shapes(objCurrentShape.Name).TextFrame.TextRange.Text
' Create a new slide at the end of the presentation.
Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
' Make the new slide the active slide.
ActivePresentation.SlideShowWindow.View.Last
' Set the name of the new slide to the text contained on the button clicked.
ActiveWindow.View.Slide.Name = objTextInBox
End Sub
My code runs and takes me to the newly created slide; however, I don't know how to tell if it actually named the new slide. Because I'm using a variable to name the slide, I suspect it isn't working. When I run another macro from a different slide, it will not jump me to the slide I renamed. I also use a variable to identify the name of the slide I am trying to jump to. I've tried all 4 of the following commands one at a time.
' Take user to the page of the category clicked on.
SlideShowWindows(1).View.GotoSlide GetSlideIndex(objTextInBox)
ActivePresentation.SlideShowWindow.View.GotoSlide (objTextInBox)
SlideShowWindows(1).View.GotoSlide GetSlideIndex(objTextInBox), 1
ActivePresentation.Slides(objTextInBox).Select
Am I missing something in my code like parenthesis or quotation marks or something? Can I not use a variable in place of a literal string like "New Slide Name"? Also, can someone tell me how to verify the name of a slide?
These work for Powerpoint-2010. I haven't tried it in other version.
When a user clicks on a shape (in my case it's a rectangle with text inside that shows a category name) this code will create a new slide and name it whatever category name was in the shape clicked on.
Sub ChangeSlideName(objClickedShape As Shape) ' Place the clicked on shape in a variable.
Dim objText As String
Dim objSlideNum As Integer
Dim pptNewSlide
' Place current slide number into a variable.
objSlideNum = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
' Set value of string variable to the text in the shape clicked on.
objText = ActivePresentation.Slides(objSlideNum).Shapes(objClickedShape.Name).TextFrame.TextRange.Text
' Create a new slide at the end of the presentation.
Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank)
' Make the new slide the active slide.
ActivePresentation.SlideShowWindow.View.Last
' Change slide # variable to the number of the newly created slide.
objSlideNum = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
' Set the name of the new slide to the text contained on the button clicked.
ActivePresentation.Slides(objSlideNum).Name = objText
End Sub
This is the command that will jump to the newly created slide using the variable:
Dim osld As Slide
Set osld = ActivePresentation.Slides(objTextInBox)
SlideShowWindows(1).View.GotoSlide (osld.SlideIndex)
This allows me to use one macro to jump between slides by using the slide names as the text written in the shapes in the presentation.
I need a way to get an image's Alt Description (specified in HTML) into the Word DOCX image's Caption. This needs to be done with many images in a document, so a macro would be best.
Assume an HTML doc with an img tag similar to this
<img src="http://www.example.com/path/to/image/picture01.jpg"
title="picture 01 title"
alt="this is alt text"
caption="this is a caption field">
This HTML doc is imported into Word 2010 (via File, Open). The image will show in the doc.
Now, how to get the 'title' attribute (which shows up in the Format Picture's Alt-Text dialog as the Description - see screenshot below) into the image's Caption?
Note that the caption parameter in the image tag is not converted to a Word Caption for that image.
Sample Alt-Text dialog for an image which shows the image's alt value as the Description
Microsoft Word has a Range.InsertCaption method, which will insert field codes to automatically number images. As you were asking to insert Alternative Text, my feeling is you were using the term caption simply as a directive to get the text beneath each image on its own carriage return. So that's what this code does:
Sub GenerateAltTextCaptions()
Dim AltTextCaption As String
Dim ImageRange As Range
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst
With ActiveDocument
For i = 1 To .InlineShapes.Count
AltTextCaption = .InlineShapes(i).AlternativeText
Set ImageRange = .InlineShapes(i).Range
ImageRange.Collapse Direction:=wdCollapseEnd
ImageRange.InsertAfter vbCr
ImageRange.InsertAfter AltTextCaption
Next
End With
End Sub
Yes, I think you've hit on a good way to make the conversion. Glad you were able to find it!
The code below will loop all the InlineShapes in the document and, if the Alternative Text is not empty, create a caption using that text.
InsertCaption is only available in VBA for InlineShapes. The user can insert captions for Shapes (graphics with text wrap formatting) because Word evaluates the selected graphic and creates a Textbox of the same width and positions it immediately below the graphic. The Shape and textbox are not, however, linked together in any way. So this functionality is not offered in VBA and would require some "creative" programming.
Sub InsertCaptionFromAltText()
Dim doc as Word.Document
Dim ils As word.InlineShape
Dim captionText As String
Set doc = ActiveDocument
Set ils = doc.InlineShapes(1)
For each ils in doc.InlineShapes
captionText = ils.AlternativeText
If Len(captionText) > 0 Then
ils.Range.InsertCaption Label:=wdCaptionFigure, _
Title:=captionText, _
Position:=wdCaptionPositionBelow
End If
Next
End Sub
I have a VBA Code to resize objects in PowerPoint including Font size, margins and everything else. But I haven’t found a solution to update/change an existing TapStop. There is a the Ruler Object with different levels und a default value. I double checked also the TextRange Object with Characters.
Are there any ideas to update the TabStop size?
Here is an example of a TextBox, i would like to resize:
TextBox Example
Shape.textframe.ruler.tabstops.count is always 0, if I "take" just the shape by For-Each-Loop. If I select it manual, it's also 0 at the sub menu TabStops of Paragraph menu.
If I click inside the shape (blinking cursor) and open the TabStops menu again, I see one TabStopPosition.
How can I access this information by VBA?
I tried it already by Line.Selection and nothing works.
Thanks!
Moe
PowerPoint used to allow only one set of paragraph settings per textframe (ie, per shape). That changed in PPT2007; now each paragraph can have its own tab and other settings. Have a go with this:
Sub ShowMeTabs()
Dim X As Long
Dim lTabCount As Long
With ActiveWindow.Selection.ShapeRange(1).TextFrame2.TextRange
For X = 1 To .Paragraphs.Count
Debug.Print X
With .Paragraphs(X).ParagraphFormat
For lTabCount = 1 To .TabStops.Count
Debug.Print .TabStops(lTabCount).Position
Next ' Tab
Debug.Print "Level:" & .IndentLevel & " Position:" & .LeftIndent 'etc
End With
Next ' paragraph x
End With
End Sub
I need to make two presentations with the same slides, backgrounds, and everything except for the text: one in German and one in English. Therefore I have two separate presentations which I must always simultaneously update, otherwise one language version will be outdated and I often forget what I changed.
I have made a custom show with all of the slides copied into one PowerPoint presentation and that works fairly well, but I still must change two of the same slides each time that I make an edit to one language.
Therefore, I'm trying to write a macro that will recognize all textboxes within the presentation with German text in them and hide them during the show, and vice versa. Then, each of the macros would be linked to a hyperlinked box on the title slide called 'English' or 'German' which, when clicked, would hide the textboxes in the other languages while also leaving all pictures and formatting the same. Ideally, the macro would hide all boxes in one language and make all the boxes in the other language visible again within the same step, so that the presentation is always usable and I don't have a user who opens a PPT with 'no text boxes' because they would all be hidden...
In order to achieve this I have two text boxes containing the text in both languages on the same slide, that's why I'd like to hide the textboxes.
I am able to get all text boxes hidden but not text boxes in a specific language (aka, all boxes regardless of their editing language will get hidden but not any specific ones).
PS - text boxes do not NEED to be referenced here... it could just refer to a shape. I was trying to avoid, that Pictures would be hidden and thought text boxes would be the best way to reference what I wanted.
Is there a better way?
I don't really know how to reference a language in VBA, I found this website by accident and thought someone might have a quick trick to help me out with this issue. The code I have which will blend out textboxes but not blend out specific language-boxes is as follows:
Sub GermanTextBoxFinder()
Dim SlideToCheck As Slide
Dim ShapeIndex As Integer
' Visit each slide
For Each SlideToCheck In ActivePresentation.Slides
' On each slide, count down through the shapes
For ShapeIndex = SlideToCheck.Shapes.Count To 1 Step -1
' If the shape IS a text box and DOES have German text
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
MsoLanguageID.msoLanguageIDGerman _
Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
End If
Next
Next
End Sub
Why don't you name the shapes with German text something that identifies them? E.g. use the prefix "txtGER" for the texts in German and "txtENG" for the ones in English. Then you could use something like the following:
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
Left(SlideToCheck.Shapes(ShapeIndex).Name, 6) = "txtGER" Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
Else
SlideToCheck.Shapes(ShapeIndex).Visible = msoTrue
End If
(Please see this q+a for information on how to rename the shapes).
This will hide text boxes/shapes with text that contain either English or French. You can modify HideFrench to hide German instead ... Intellisense will provide the correct constants.
Sub HideEnglish()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDEnglishUS Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub
Sub HideFrench()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDFrench Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub
I am trying to find out how you write VBA to enter a text box into a slide, and enter text. I am also trying to find vba for entering text into the notes section of a PowerPoint slide.
Any help would be greatly appreciated. I have tried to find a site specifically for this, but have not been able to do so
Entering text into a PPT slide is about the same as entering into the notes section.
You have to start out with a Slide object reference, which represents the slide you're adding to; and you add a text box shape to the slides' shapes collection.
Example:
Sub AddTextBoxToSlide()
Dim oDestSlide As PowerPoint.Slide
Set oDestSlide = ActivePresentation.Slides(1)
Dim slideWidth As Single
Dim slideHeight As Single
slideWidth = oDestSlide.Parent.PageSetup.SlideWidth
slideHeight = oDestSlide.Parent.PageSetup.SlideHeight
Dim oTextBox As PowerPoint.Shape
Set oTextBox = oDestSlide.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, _
Top:=0, _
Width:=slideWidth, _
Height:=slideHeight / 12)
oTextBox.TextFrame.TextRange.Text = "Shape text here"
End Sub
All this does is adds a text box shape to the first slide in the active presentation at the top of the slide. It is as wide as the slide and 1/12th the height of the slide. The parameters for Shapes.AddTextbox() are pretty self-explanatory...
To add to the notes section, I just use the NotesPage object on the slide your notes page is in...so the above code would be about the same, except:
Set oTextBox = DestSlide.NotesPage.Shapes.AddTextbox(msoTextOrientat...
This is an old question, but since you can't record macros in PowerPoint, people will be searching for questions like this until you can.
I didn't need this for adding text to slides, but I tried it for adding text to Notes. However, in Outline View, nothing appeared in my Notes section. It wasn't until I went to View-->Notes Page, and I saw the message I'd added -- at the top of the screen.
You see, when you change Set oTextBox = oDestSlide.Shapes to Set oTextBox = oDestSlide.NotesPage.Shapes, you're not adding text to the Notes. You're adding a textbox to the notes. And that textbox appears only in Notes Page view (at the top of the screen, until you move it).
What we really want to do is add our text to Placeholder 2 (the notes area) on the notes page, like this:
oDestSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.InsertAfter "Notes text here"