How to create ActiveX TextBox dynamically on PowerPoint - vba

I am a free-lance Java programmer who, for the first time, is asked to do some VBA programming on PowerPoint and I am struggling a bit.
I have created a button, which triggers the creation of a slide (this works).
On this new slide, I want to create an ActiveX TextBox (one of those that work when in Presentation mode), but I am only able to create "stardard" PowerPoint textboxes.
I am sure this is a quite easy command, but I cannot find it anywhere..
This is the code that I am using. Can somebody help me?
Thank you!
Public Sub addContentToSatelliteSlide()
currentSlide = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex
'CURRENT SLIDE ON SLIDESHOW
With ActivePresentation.Slides(currentSlide + 1).Shapes
With .AddTextbox(msoTextOrientationHorizontal, 160, 80, 400, 400).TextFrame
.TextRange.Text = "add informatiom here"
.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignLeft
.TextRange.Font.Color = RGB(255, 255, 255)
.TextRange.Font.Size = 11
.TextRange.Font.Name = "Arial"
.TextRange.Font.Bold = False
.TextRange.Font.Color = RGB(0, 0, 0) 'BLACK
End With
End With
End Sub

An ActiveX text box is an OLE control, so use this syntax instead:
.AddOLEObject Left:=160, Top:=80, Width:=400, Height:=400, ClassName:="Forms.TextBox.1"
Here's the Microsoft help page for this: Shapes.AddOLEObject method (PowerPoint)

Related

Create add-in to add sticky notes in powerpoint (VBA)

I would like to create a simple macro for powerpoint that would allow me to click on one button to automatically insert a yellow sticky note onto my slide so I can insert a comment. This is something I need to do over and over in my current job and right now I am wasting a lot of time, each time creating a rectangle -> coloring it yellow -> creating a black outline -> setting font color to red and size to 12..
Appreciate any help here, I know it should not be very hard!
Thanks!
example of standard stickynote on a slide (at scale)
I wrote this for you and hope it helps.
Sub insert_sticky_note()
Dim mySlide As PowerPoint.Slide
Dim myTextbox As PowerPoint.Shape
Set mySlide = ActivePresentation.Slides(ActiveWindow.View.Slide.SlideNumber)
Set myTextbox = mySlide.Shapes.AddTextbox(msoTextOrientationHorizontal, _
Left:=0, Top:=10, Width:=200, Height:=50)
myTextbox.Fill.BackColor.RGB = RGB(250, 246, 0) 'yellow
myTextbox.Fill.Transparency = 0.2 'translucent
myTextbox.Height = 150
myTextbox.Width = 300
myTextbox.TextFrame2.AutoSize = msoAutoSizeTextToFitShape 'https://www.pcreview.co.uk/threads/how-to-vba-code-shrink-text-on-overflow.3537036/#post-12183384
With myTextbox.TextFrame.TextRange
.Text = "Note"
'With .Font
' .Size = 12
' .Name = "Arial"
'End With
End With
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.

MS Word change background color with macro

Can I change the background color of the page in Microsoft Word 2003 (2007, 2010, 2013) through a macro?
I am attempting to create a Word macro (Microsoft Word 2013) to change the page background color. I began by recording the keystrokes - this is the recorded macro:
Sub WritingLayout()
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 204)
ActiveDocument.Background.Fill.Transparency = 0#
ActiveDocument.Background.Fill.PresetTextured msoTextureParchment
End Sub
This macro does not work on new documents.
I found how to do it. Just add this line before the vba code:
ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True
Here is the entire macro:
Sub WritingLayout()
'
' WritingLayout Macro
'
ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 204)
ActiveDocument.Background.Fill.Transparency = 0#
ActiveDocument.Background.Fill.PresetTextured msoTextureParchment
End Sub

disappearing text on mouse click in text box in powerpoint using vba

I am facing an issue with the custom textbox in a PowerPoint 2010 slide. I want to make the text that is initially there inside the textbox (say "Click to enter text") to disappear as soon as mouse is clicked on the textbox to enter some text. If this is possible through events in VBA, I would like to know which event(associated with the textbox) to capture and how to do it using VBA?
Also changing the height parameter is not having any effect in PowerPoint 2010.
here is my code that defines the textbox :
Dim sld As Slide
Set pShape = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=50, Top:=50, width:=500, Height:=300)
pShape.TextFrame.TextRange = "Click to enter text"
pShape.TextFrame.TextRange.Font.Size = 14
pShape.Line.Visible = True
pShape.Line.ForeColor.RGB = RGB(0, 0, 0)
pShape.Line.DashStyle = msoLineDash
Plz help me out with this..
Thanks in advance!!!
The height is not taking because the default is to resize to fit availabl text.
Either use a shape not a textbox or reset the defaut
Set pShape = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=50, Top:=50, Width:=500, Height:=300)
pShape.TextFrame.AutoSize = ppAutoSizeNone
pShape.Height = 300
Making the text disappear on click is possible but difficult. You can use the WindowSelectionChange event but this is very difficult to set up if you are not writing an addin. You would need to write a WithEvents Class module and initialise an instance. Fairly advanced!

VB.NET MS PowerPoint Slide Background

I'm currently working with PowerPoint in VB.NET and having a bit of trouble getting a slide to have an individual background. Using the SlideMaster method changes all slide in the presentation and I want only one to be affected can anyone offer any advice? I'm not in a position to post code at the moment, but I will comment with some when I can, if no one can help.
PS Using the Background.Fill.UserPicture method doesn't seem to be working, but I'm not sure why...
Sometimes the macro recorder in older versions is a blessing, in its own odd way. Here's what PPT 2003 gives you when you record the act of setting a background to a picture for a given slide (after I've commented out the bits that don't have much/any effect on things):
With ActiveWindow.Selection.SlideRange
.FollowMasterBackground = msoFalse
'.DisplayMasterShapes = msoTrue
With .Background
.Fill.Visible = msoTrue
'.Fill.ForeColor.RGB = RGB(255, 255, 255)
'.Fill.BackColor.SchemeColor = ppAccent1
'.Fill.Transparency = 0#
.Fill.UserPicture "C:\Documents and Settings\Me\My Documents\My Pictures\photo.jpg"
End With
End With
UserPicture is the way to set the picture fill, as you see here; but you have to set .FollowMasterBackground to False, else it ignores your fill settings.