PowerPoint Add-in Add a textbox to a selected slide - vba

I'm trying to make a button that when clicked, will add two text boxes to the selected slide in a specified place with specified formatting (font, size, color, justified). I've been trying to reverse engineer anything applicable, but just end up breaking things. This set of code will allow me to make a rectangle (not a textbox which is preferred) size it and place it (just 1 not 2) with sample text.. For the life of me I cant figure out how to make it create a textbox on the selected slide or active window.. what am I doing wrong? Heres the code I found..
Sub AddTextBox()
Set myDocument = ActivePresentation.Slides(1)
With myDocument.Shapes _
.AddTextBox(msoShapeRectangle, 180, 175, 350, 140).TextFrame
.TextRange.Text = "Ctrl+A(Select all), Ctrl+V(Paste)"
.MarginTop = 10
End With
End Sub

To refer to the slide that's currently being displayed, you can use the Slide Property of the View object...
Dim mySlide As Slide
Set mySlide = ActiveWindow.View.Slide
For creating a textbox, here's the proper syntax as per the documentation here...
Syntax
expression. AddTextbox( Orientation, Left, Top, Width,
Height )
expression A variable that represents a Shapes object.
So, in your case, it would be something like this...
Dim myTextbox As Shape
Set myTextbox = mySlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 180, 175, 350, 140)
Change the text orientation as desired.

Related

Using variables inside .Slides & .Shapes methods in Powerpoint VBA

I am working on an interactive Powerpoint Presentation where the user will click on a thumbnail of photo and be able to view it nearly full screen. I'm having difficulty with .Shapes and .Slides methods.
I want several smaller images to appear on one slide in the presentation. If the user wants to view it very large they just need to click on the image. I would then like the image to appear on it's own newly generated slide as large as it can fit on that slide. When they click the larger image, they will be taken back to the smaller images slide they were viewing. This is easily enough achieved by making a separate full sized image slide for every small image in the show and simply calling the large slide number when the small image is clicked; however it is time consuming and makes the presentation far larger than it needs to be. If a user never clicks to see the enlarged image, then the page with the large image is taking up space. I've opted to execute vba code when an image is clicked on that is supposed to:
copy the image
create a new slide after the last slide in the presentation
paste the image into the new slide
resize the image as large as it can fit on the screen
view the new slide with larger image
send the user back to the slide
they started on.
Code:
Sub ViewFullSize()
Dim pptNewSlide As Slide
' Dim objCurrentSlideIndex As Integer
' objCurrentSlideIndex = ActiveWindow.Selection.SlideRange.SlideIndex
With ActivePresentation
.Slides(2).Shapes("Picture 7").Copy
.Slides(4).Shapes.Paste
End With
Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutCustom)
ActivePresentation.SlideShowWindow.view.Last
End Sub
This code executes and does what is epected. My issue is, I need the slide numbers and shape numbers to be variables. I don't want to rewrite this snippet of code for 100's of photos that could be clicked on. I've tried to make the current slide a variable like this:
Dim objCurrentSlideIndex As Integer
objCurrentSlideIndex = ActiveWindow.Selection.SlideRange.SlideIndex
With ActivePresentation
.Slides(objCurrentSlideIndex).Shapes("Picture 7").Copy
.Slides(4).Shapes.Paste`
End With
The variable I tried .Slides(objCurrentSlideIndex) causes the whole subroutine not to execute, but doesn't crash the slideshow. I've used Set and a slew of other syntax and can't get it to use a variable instead of a plain number. Is there a way to do this? Can the .Slides() and .Shapes() methods even use variables? I've read several of Microsoft's and PPTools pages, but can find no examples using variables.
Sub ViewFullSize(objCurrentShape As Shape) ' Place shape clicked on into variable.
Dim pptNewSlide As Slide
Dim objCurrentSlideNum As Integer
Dim objLastSlideNum As Integer
' Place current slide number into a variable.
objCurrentSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition
' Send shape to clipboard for later pasting.
ActivePresentation.Slides(objCurrentSlideNum).Shapes(objCurrentShape.Name).Copy
' Place new blank slide at the end of the presentation.
Set pptNewSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutCustom)
' Make the new slide the active slide.
ActivePresentation.SlideShowWindow.view.Last
' Place the new slide number into a variable.
objLastSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition
' Paste the shape image from the clipboard onto the new slide.
ActivePresentation.Slides(objLastSlideNum).Shapes.Paste
End Sub
I stumbled onto a snippet of code that showed when a shape was clicked, it could pass its identifiers directly into the subroutine and be assigned to a variable. In my case (objCurrentShape As Shape). This could then be used with the .Shapes() method that I used to call the shape for copying .Shapes(objCurrentShape.Name).Copy.
The .Slides() method was simpler to assign to a variable (or so I believe) because it was not dependent on which shape was clicked. It's merely the active slide number and was attained with the .View.CurrentShowPosition function.
This code can now be assigned to any number of shapes on a slide and will copy and past that shape to a newly created blank slide at the end of your presentation for further manipulations.
Fully working code!
For anyone interested this is the finished (maybe not gleaned), fully operational code that I have working in Powerpoint 2017.
This was designed to be assigned as a Macro Action to pictures in a slideshow. When there are multiple smaller sized images on a page, they can each be assigned this one macro that will show the image full screen on it's own slide and then send the user right back to the screen they were on that contains the smaller images. It's sort of like a full screen zoom function.
It's documented as well as I can document to allow anyone to follow allong with what's taking place at each step. Edits for proper wording and terminology are welcome if I have stated anything incorrectly.
This is not specific to my machine or paths or anything like that. You can simply copy and paste into a module in powerpoint and start assigning the new macro to any images in your presentation.
Sub ViewFullSize(objCurrentShape As Shape) ' Place shape clicked-on into variable.
' Credit Shyam Pillai # http://www.skphub.com/ppt00040.htm#2 for the method of
' bringing the shape into the macro as a variable allowing easier manipulation.
Dim pptNewSlide As Slide
Dim objCurrentSlideNum As Integer
Dim objLastSlideNum As Integer
Dim objLargeView As Shape
' Place current slide number into a variable.
objCurrentSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition
' Copy shape to clipboard for later pasting.
ActivePresentation.Slides(objCurrentSlideNum).Shapes(objCurrentShape.Name).Copy
' Place new blank 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
' Place the new slide number into a variable.
objLastSlideNum = ActivePresentation.SlideShowWindow.view.CurrentShowPosition
' Paste the shape image from the clipboard onto the new slide.
ActivePresentation.Slides(objLastSlideNum).Shapes.Paste
' Put pasted image into a variable.
Set objLargeView = ActivePresentation.Slides(objLastSlideNum).Shapes(1)
' Full credit for this next section of the code goes to PPTools & David Marcovitz
' # http://www.pptfaq.com/FAQ00352_Batch_Insert_a_folder_full_of_pictures-_one_per_slide.htm
' Thanks for the hard work!
' Manipulate the image using the variable.
With objLargeView
' Set mouse-click action on image to return user back to the slide they came from.
.ActionSettings(ppMouseClick).Action = ppActionLastSlideViewed
' Reposition the image for proper resizing
.Left = 0
.Top = 0
.ScaleHeight 1, msoTrue
.ScaleWidth 1, msoTrue
' Resize the image to full screen while maintaining aspect ratio.
' This is wide screen mode. If you are working with the more
' narrow mode, simply change the 9 to a 3 and the 16 to a 4
' to keep the correct aspect ratio.
If 9 * .Width > 16 * .Height Then
.Width = ActivePresentation.PageSetup.SlideWidth
.Top = 0.5 * (ActivePresentation.PageSetup.SlideHeight - .Height)
Else
.Height = ActivePresentation.PageSetup.SlideHeight
.Left = 0.5 * (ActivePresentation.PageSetup.SlideWidth - .Width)
End If
End With
' From here, the slideshow is now showing the originally clicked-on image
' full screen on its own page waiting for the user to click on it to return
' to the rest of the show. If the slideshow isn't set to kiosk mode, then
' there is the possibility of the user clicking somewhere on the screen out
' of the picture area and it would end the slideshow.
End Sub

VBA, Fill shape with button click

I'm very new to macros and I'm trying to teach myself VBA in excel 2013 and could use a lot of help. I would like to know how to change a shape fill color (not a cell) when I click an ActiveX button. Here is what I'm thinking:
onClick() <-- Do I need a button name?
if shape.color = RGB(231,230,230) <-- this is the starting color
shape.color = RGB(0,0,0) <-- this is what I want to change it to
else
shape.color = RGB(231,230,230) <-- if color is black change to this
end if
By the way, this is for fun and not for anything special or official.
Here is the code you are looking for:
Option Explicit
Sub ButtonClick()
Dim shp As Shape
Set shp = ThisWorkbook.Worksheets(1).Shapes("Rectangle 1")
If shp.Fill.ForeColor.RGB = RGB(231, 230, 230) Then
shp.Fill.ForeColor.RGB = RGB(0, 0, 0)
Else
shp.Fill.ForeColor.RGB = RGB(231, 230, 230)
End If
End Sub
Just make sure that you adjust the above code to the correct shape name (which is "Rectablge 1" in this example, which is located on Sheet(1)).
Afterwards, create a button on that sheet and assign this macro to the button to make it work and toggle the shape color between black and grey.
Next time, I recommend that you (in a first step) record the things you want Excel to do for you with the VBA macro recorder: https://www.youtube.com/watch?v=Q_HQGHPBYoo Afterwards, have a look at the code and try to understand it by highlighting VBA key-words and pressing F1. Also, I'd recommend reading this: http://www.homeandlearn.org/ Afterwards you should be set to write some rather fancy macros.
Put this in the module of the worksheet which contains the button:
EDITED: Sorry, I originally thought you wanted the actual button colour to change. I borrowed from Ralph's answer to account for the shape being the object of the colour change, but below this is for an ActiveX control (as you asked) instead of a form button:
Private Sub CommandButton1_Click() ' Yes, you do need to specify the button
Dim shp As Shape
Set shp = ThisWorkbook.Worksheets(1).Shapes("Rectangle 1")
If shp.Fill.ForeColor.RGB = RGB(231, 230, 230) Then
shp.Fill.ForeColor.RGB = RGB(0, 0, 0)
Else
shp.Fill.ForeColor.RGB = RGB(231, 230, 230)
End If
End Sub
Yes, you need a button name. Drop an ActiveX control on the sheet, then go to the sheet's module in the VBE. In the left dropdown at the top of the code pane, find your control name (you probably want to rename it first - I didn't for this example)
When you pick the control name in the left dropdown and the event name in the right, the VBE stubs the Sub and End Sub for you.
Next you need to reference your shape in your code. Start by using the Me keyword. When you're in a sheet's code module, Me refers to that sheet. Then the dot operator will expose all the properties and methods of that sheet. When you drop an ActiveX control on a sheet, a new property of the sheet is automatically created that provides access to the object. You can use
Me.CommandButton1.BackColor = RGB(231, 230, 230)
You If logic looks fine.

Creating a button in PowerPoint VBA, but it's not clickable

I'm working in Excel VBA, creating a PowerPoint presentation.I am trying to place Next and Previous buttons on each slide. I am using the code below:
Dim ppApp As PowerPoint.Application, ppPres As PowerPoint.Presentation, ppSlide As PowerPoint.Slide
Dim shpNextButton As PowerPoint.Shape
Set ppApp = CreateObject("Powerpoint.Application")
Set ppPres = ppApp.Presentations.Open("C:\Users\test1.pptm")
Set ppSlide = ppPres.Slides.Add(ppPres.slides.count + 1, ppLayoutBlank)
Set shpNextButton = ppSlide.Shapes.AddShape(msoShapeActionButtonForwardorNext, 750, 480, 40, 12.5)
With shpNextButton.TextFrame.TextRange
.Text = "Next"
With .Font
.Size = 10
.name = "Arial"
End With
End With
shpNextButton.ActionSettings(ppMouseClick).Action = ppActionNextSlide
This code creates the button with the correct text on it. However in the PowerPoint slide, the button is clickable. When I click on it, it just acts like a regular shape.
Your code works for me when I use an adapted version of it (removing lines 1,3,4 and setting a reference to the current slide for line 5) within the PowerPoint VBE so the only thing I can think of is that the 'pp' constants are not set if you haven't added the PowerPoint library to your project. What do you get in the Immediate window if you type this?
?ppActionNextSlide
It should return 1 if all is ok. If it returns 0 then that's equivalent to ppActionNone which would explain what's happening.
By the way, your code added the button outside of my 4:3 slide so I assume you're using a 16:9 slide layout. It would be better to reference the right hand side of the side less an offset to avoid this potential issue with:
ActivePresentation.PageSetup.SlideWidth

Select a single check box

I have a piece of code that only works when there are many check boxes. However when I only have one checkbox the code bugs. Basically what I want the macro to do is to select the ckeckbox as an object and align it to a cell it should also work if more than one checkbox. Could you please help?
Many thanks in advance. Please see code attached
Worksheets("Analysis Line Cupboards by Pick").CheckBoxes.Select
Selection.ShapeRange.Align msoAlignCenters, msoFalse
Selection.ShapeRange.IncrementLeft 45
Range("A10000").Select
You can align a checkbox (or any shape) on another shape, or on a cell with the left property.
Example, for an ActiveX checkbox in a Sheet to be aligned on cell B5:
Sheets("Sheet1").Shapes("Checkbox_1").Left = Sheets("Sheet1").Range("B5").Left
In your case (adapt it):
Sheets("Analysis Line Cupboards by Pick").Shapes("Checkbox name").Left = cell.left
Or, if you have multiple checkboxes, give them appropriate names and loop through them. For example, their names could be chbx_A, chbx_Hello, chbx_10 or something.
Sub AlignCHBX()
dim shp as shape
for each shp in Sheets("Analysis Line Cupboards by Pick").Shapes
if shp.name like "chbx*" then shp.left = Sheets("Analysis Line Cupboards by Pick").Range("B1").Left
next
End sub
This verifies that the shape's name starts with chbx to avoid moving other shapes. You can use this to differentiate certain groups of checkboxes, as well!
For a NON-ActiveX checkbox, use the following syntax to refer to it's left, for example:
Sheets("Sheet1").OLEObjects("chbx_A").Left
For it's value:
Sheets("Sheet1").OLEObjects("chbx_A").Object.Value
For .Top it works the same way. The .Left and .Top properties are numeric values measured in pixels. If you want to go to an absolute position, you can write Sheets("Sheet1").OLEObjects("chbx_A").Left = 150 for example. When you are making the shape's Left = to the cell's left, the code actually goes to see what Absolute value the left position of the cell is, and gives it to that shape. If I can elaborate, you could write:
dim nbPosition as Double
nbPosition = ActiveSheet.Range("B5").Left
debug.print nbPosition 'It could say 40, for example, depending on column width
ActiveSheets.Shapes("Shape1").Left = nbPosition + 10 'Will send it 10 pixels further than nbPosition

Entering information into the Notes section of a PowerPoint slide using VBA

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"