I am trying to select a slide by name. I have added a title via the outline. below is the code that is not working. "item Idaho not found in the slide collection"
ActivePresentation.Slides("Idaho").Select
The slide's name and the text in the title placeholder nave nothing to do with one another.
Unless you've renamed it, the first slide in the presentation will be named "Slide1", the second "Slide2" and so on.
If you specifically need a way to locate the slide whose title text = "Idaho", you'd need to write a function to search all the slides in the presentation and return the first one it finds that meets your criteria. For example:
Sub TestMe()
Dim oSl As Slide
Set oSl = FindSlideByTitle("idaho")
If Not oSl Is Nothing Then
MsgBox "Found your title on slide " & CStr(oSl.SlideIndex)
End If
End Sub
Function FindSlideByTitle(sTextToFind As String) As Slide
Dim oSl As Slide
For Each oSl In ActivePresentation.Slides
With oSl.Shapes.Title.TextFrame
If .HasText Then
If UCase(.TextRange.Text) = UCase(sTextToFind) Then
Set FindSlideByTitle = oSl
End If
End If
End With
Next
End Function
Reviving an old question, but I wanted to throw this in.
While it's possible that ActivePresentation.Slides("MySlideName").Select doesn't work, this does work for me in PPT 2010:
Dim PPTObj As PowerPoint.Application
Set PPTObj = New PowerPoint.Application
Dim PPTClinic As PowerPoint.Presentation
Set PPTClinic = PPTObj.Presentations.Open(FileName:="Your File Name Here")
PPTClinic.Slides("MySlideName").Select
This, of course, assumes that there is a slide named "MySlideName". Your code will have to deal with gracefully handling the Item MySlideName not found in the Slides collection. error (err.number = -2147188160).
Related
I have been trying to modify a useful piece of code created by David Foster some time ago,
However the one really useful addition to this code is proving hard to find in the references.
I need to get the macro to make sure that the "Slide Image" checkbox is ticked when it applies the notes master to all slides, as some slides have been "frankensteined" into the project.
I am struggling to find any reference to this checkbox in powerpoint references, any ideas?
Sub DReplaceNotesMaster()
' Modified version of code originally posted to
' msnews.microsoft.com public newsgroups by
' David Foster in May of 1999
Dim ctl As CommandBarControl
Dim oSl As Slide
' 700 is the control ID for Layout
Set ctl = CommandBars.FindControl(Id:=700)
ActiveWindow.ViewType = ppViewNotesPage
If (ctl Is Nothing) Then
MsgBox "command not available"
Exit Sub
End If
For Each oSl In ActivePresentation.Slides
' go to the current slide
ActiveWindow.View.GotoSlide (oSl.SlideIndex)
DoEvents
' Bring up the dialog
ctl.Execute
DoEvents
' send it the needed keystrokes
SendKeys "%r{enter}"
DoEvents
Next
End Sub
Oddly, the slide image on a notes page is referred to as a title placeholder. This sub checks every notes page for a placeholder with "Slide Image" in the name and adds it if one is not found. This assumes that someone hasn't used the Selection pane to rename the slide image placeholder. If they have, you'll have to trap the resulting error that displays a message "Invalid request: Slide already contains maximum placeholders of this type".
Sub ShowNotesSlideImage()
Dim oSlide As Slide
Dim oShape As Shape
Dim bTitleFound As Boolean
For Each oSlide In ActivePresentation.Slides
bTitleFound = False
For Each oShape In oSlide.NotesPage.Shapes
If oShape.Type = msoPlaceholder Then
If InStr(oShape.Name, "Slide Image") > 0 Then
bTitleFound = True
End If
End If
Next oShape
If bTitleFound = False Then
oSlide.NotesPage.Shapes.AddPlaceholder Type:=ppPlaceholderTitle
End If
Next oSlide
End Sub
A handy resource for finding control names and ID numbers is Microsoft's Office 2016 Help Files: Office Fluent User Interface Control Identifiers, a free download.
First of all I'm selecting a slide by it's ID. Once that is done I'm trying to select a TextRange so it appears like the image below, however, I keep getting an index out of bounds exception and I've no idea why.
I've also tried selecting the TextRange like this foundText.Select()
My code is as follows
_PowerPointApp.ActivePresentation.Slides.FindBySlideID(slideID).Select()
For Each slide As PowerPoint.Slide In _PowerPointApp.ActivePresentation.Slides
If slideID = slide.SlideID Then
For Each shape As PowerPoint.Shape In slide.Shapes
If shape.Name = slideShape Then
Dim foundText = shape.TextFrame.TextRange.Find(linkText)
If Not foundText Is Nothing Then
shape.TextFrame.TextRange.Characters(foundText.Start, foundText.Start + foundText.Length).Select()
End If
End If
Next
End If
Next
I want to highlight the shape corresponding to a particular group. The following code is only highlighting shapes that are grouped with active page or master but not with the group present in the active page.
Sub CA_Trace_Conflict1()
PCC_CA = InputBox("Enter PCC Band")
'SCC1_CA = InputBox("Enter SCC1 Band")
Dim shp As Visio.Shape
Dim subshp As Visio.Shape
Dim connectorshape As Visio.Shape
Dim BandLinewidth As String
Dim lngShapeIDs() As Long
Dim count As Integer
Dim PCC_Flag As Integer
Dim SCC1_Flag As Integer
PCC_Flag = 0
SCC1_Flag = 0
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
Dim UndoScopeID1 As Long
PCC_CA_space = PCC_CA & " "
For Each shp In Visio.ActivePage.shapes
If shp.Type = 2 Then 'Check if shp is a group
For Each subshp In shp.shapes
If InStr(shp.Text, PCC_CA_space) > 0 Then
'If PCC_CA Like shp.Text Then
Set connectorshape = shp
Debug.Print shp.Parent
Application.ActiveWindow.Page.shapes.ItemFromID(shp.ID).CellsSRC(visSectionObject,visRowLine, visLineWeight).FormulaU = "5.5 pt"
' Debug.Print shp.ID
End If
Next
End If
Next
End Sub
I think you want to select a subshape within a group programmatically. Doing this in Visio is not obvious, so let me help. I'll put links to two articles on my website, plus one on Microsoft's at the end of the post. These discuss selection-related topics in further detail.
So let's tackle your problem...
Setup
Open a blank drawing in Visio
Draw two rectangles, then group them
You now have three shapes on this page.
Sheet.1 is a subshape
Sheet.2 is a subshape
Sheet.3 is the group
You can programmatically select the group like this, as you've discovered:
Public Sub SelectGroup()
'// Get the active window:
Dim win As Visio.Window
Set win = Visio.ActiveWindow
'// Deselect everything:
Call win.DeselectAll
'// Get a shape object:
Dim shp As Visio.Shape
Set shp = Visio.ActivePage.Shapes.ItemFromID(3) '<<----- Sheet.3 is the group!
'// Cause that shape to be selected in the window:
Call win.Select(shp, Visio.VisSelectArgs.visSelect)
'// Cleanup:
Set shp = Nothing
Set win = Nothing
End Sub
By the way, the Sub above is much more nitpicky and long than it has to be. But it will help to have things simple and clean, when you start adding features and behaviors. You can actually one-line the whole procedure like this--you can even paste this into the Immediate window:
Call Visio.ActiveWindow.Select(Visio.ActivePage.Shapes.ItemFromID(3), Visio.VisSelectArgs.visDeselectAll + Visio.VisSelectArgs.visSelect)
Now to subselect Sheet.1 or Sheet.2. One would think we could simply change the shp object to be one of the subshapes, ala:
'// Sheet.1 is a subshape, you'll get an error
Set shp = Visio.ActivePage.Shapes.ItemFromID(1) '<<----- ID = 1
but this won't work. In fact you'll get an "Inappropriate target object for this action" error.
To fix this, we have to pass a different argument to the Select method:
Public Sub SelectSubshape()
'// We've drawn two rectangles on a blank page, then
'// grouped them. Sheet.1 and Sheet.2 are subshapes,
'// Sheet.3 is the group.
'// Get the active window:
Dim win As Visio.Window
Set win = Visio.ActiveWindow
'// Deselect everything:
Call win.DeselectAll
'// Get a subshape object:
Dim shp As Visio.Shape
Set shp = Visio.ActivePage.Shapes.ItemFromID(2)
'// Cause that shape to be SUBSELECTED in the window.
'// Note the different argument: visSubSelect
Call win.Select(shp, Visio.VisSelectArgs.visSubSelect) ' <<------ visSubSelect!
'// Cleanup:
Set shp = Nothing
Set win = Nothing
End Sub
Voila! Subshape selected in the active window!
If you want to detect which shapes are already selected, then you'll have to fiddle with the IterationMode property of a Selection object. This is pretty confusing, plus I don't think you're asking for that right now. But knowing the term will help you search for help in the future, should you need it.
Articles
Getting a Handle on Selecting and Subselecting Visio Shapes
Detect Sub-selected Shapes Programmatically
Selection.Select method (Visio)
I'm writing a script in VBA to change the Title's of the slide to the section title. I have multiple sections and I want to iterate though the ppt to change all the titles of the slides to have the same section title as its section.
I've tried finding how to grab the section title and set it to the slide's title.
Sub test()
ActivePresentation.Slides.Name = ActivePresentation.SectionProperties(sectionName)
End Sub
I need to add iteration, I need i believe syntax is messed.
Hopefully something like the following is what you're looking for. The code
Loops through each slide in the ActivePresentation, adding a title if there isn't already one.
Changes the title text by getting the sectionIndex property of the slide, and then using that index in the SectionProperties.Name method to retrieve the corresponding text.
Sub ChangeMyTitles()
Dim sld As Slide
Dim titleShape As Shape
If ActivePresentation.SectionProperties.Count = 0 Then Exit Sub
For Each sld In ActivePresentation.Slides
With sld
If Not .Shapes.HasTitle Then
Set titleShape = .Shapes.AddTitle
Else
Set titleShape = .Shapes.Title
End If
titleShape.TextFrame2.TextRange.Text = ActivePresentation.SectionProperties.Name(.sectionIndex)
End With
Next sld
End Sub
EDIT:
If you want to modify a different placeholder than the title, you could do something like this. Based on your screenshot, I assume that the placeholder you want to modify is the 3rd one (title is 1st, body is 2nd, and chapter is 3rd), but you might have to change the 3 below.
Sub ChangeMyChapters()
Dim sld As Slide
Dim chapterShape As Shape
If ActivePresentation.SectionProperties.Count = 0 Then Exit Sub
For Each sld In ActivePresentation.Slides
With sld
Set chapterShape = .Shapes.Placeholders(3)
chapterShape.TextFrame2.TextRange.Text = ActivePresentation.SectionProperties.Name(.sectionIndex)
End With
Next sld
End Sub
I've found after I've created several PowerPoint templates that I forgot to add the "Title" placeholder that you can find in Master View. Instead, I've added textbox placeholders instead, which works fine. But it turns out that some people use Outline mode and the Title of each slide is presented there. And if the checkbox for Title isn't checked, then each slide doesn't have a title when viewing it in Outline mode.
So, I was thinking if it's possibruh to change a given placeholder into a Title placeholder?
Maybe using VBA. Paste in Visual Basic. Select the targeted placeholder/textbox (any text).
Then, run it.
Sub convertToTitle()
Dim osld As Slide
Dim SlideIndex As Long
Dim oshp As Shape
Dim oTxR As TextRange
SlideIndex = ActiveWindow.View.Slide.SlideIndex
Set osld = ActivePresentation.Slides(SlideIndex)
Set oshp = ActiveWindow.Selection.ShapeRange(1)
Set osld = oshp.Parent
Set oTxR = oshp.TextFrame.TextRange
With ActivePresentation
ActivePresentation.Slides(SlideIndex).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(2)
'use layout = 2 because it has both Title & Content
'but you can use any layout as long as it has Title in it
osld.Shapes.Placeholders.Item(1).TextFrame.TextRange = oTxR.Characters
oshp.Delete
End With
End Sub
Voila, it changes to Title Placeholder. But you have to run it for every slide.