I would like to ask, why sometimes when I use the following code on a selected textbox, I will get the msgbox return value in multiple decimals places (eg. 3-6 decimals place) and sometimes it will return a whole number with the same textbox
Sub getShapeNow()
With ActiveWindow.Selection.ShapeRange(1)
MsgBox .Name
MsgBox .Width
MsgBox .Height
MsgBox .Top
MsgBox .Left
End With
End Sub
Powerpoint obviously does not support any Width, but only some Width, which is based on the screen rezolution. Thus, if you try
Sub GetShapeNow()
With ActiveWindow.Selection.ShapeRange(1)
.Width = 47.00147
Debug.Print .Width
End With
End Sub
I would see 47.0015 printed on the immediate window,as a Width as exact as .00147 is not supported by my rezolution.
The properties are a decimal with values after the comma. If there are no values after the comma, e.g. 47 and not 47.01, the VBA writes the value as 47.
Check this:
Sub GetShapeNow()
With ActiveWindow.Selection.ShapeRange(1)
.Width = 47
.Height = 34
.Top = 40.4
.Left = 147
Debug.Print .Name; .Width; .Height; .Top; .Left
End With
End Sub
At the immediate window you see this:
Rectangle 1 47 34 40,4 147
In general, the value that Shape.Width returns is from type Single:
MSDN Shape.Width Property
Single Data Type
Related
I am in my first week of learning VBA, and I am looking for a VBA code that will help me resize and reposition pictures pasted into PowerPoint 2016. The desired picture format details are below:
Size
- Height = 3.39"
- Width = 6.67"
- Rotation = 0
- Scale Height = 62%
- Scale Width = 62%
- Aspect Ratio = Locked
- Relative to original picture size = true
Position
- Horizontal position = 0
- Top Left Corner
- Vertical position = 2.06
- Top Left Corner
Any help would be greatly appreciated.
Below is the code that worked for me. Thanks for the support.
Sub ResizeAll()
For Each tSlide In ActiveWindow.Presentation.Slides
tSlide.Select
With tSlide.Shapes.Item(1)
'assume a blank slide with one image added only
.Select
.Height = 72 * 3.39
.Width = 72 * 6.67
'algin middle (Horizontal Center)
.Left = 0
.Top = ActivePresentation.PageSetup.SlideHeight / 3.25
End With
Next
End Sub
Okay, so this macro will adjust the details of every picture within your powerpoint.
Sub AdjustImages()
Dim curSlide As Slide
Dim curShape As Shape
For Each curSlide In ActivePresentation.Slides
For Each curShape In curSlide.Shapes
With curShape
'size:
''1 inch = 72 points
.Height = 72 * 3.39
.Width = 72 * 6.67
.ScaleHeight 0.62, msoTrue
.ScaleWidth 0.62, msoTrue
.LockAspectRatio = msoTrue
'position:
.Rotation = 0
.Left = 0
.Top = 2.06
'Relative to original picture size = true
End With
Next curShape
Next curSlide
End Sub
The only part of your question that I don't really understand is when you mention it being "relative to original picture size = true". I can't seem to find an attribute that matches that.
very simple Problem:
Dim myForm As Object
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Set MyComboBox = myForm.Designer.Controls.Add("Forms.ComboBox.1")
With MyComboBox
.Left = 100
.Top = 20 + x * 10
.Height = 16
.Width = 100
For Each col In fields
.AddItem col
Next
End With
I can clearly print out the values in fields (which is a variant), but somehow the Combobox remains blank... Any ideas? I'm wasting way to much time on this...
thanks for your help
I'd like to have the comment box fit the comments just right (no extra space at the bottom).
I know there is the .AutoSize but I want the maximum width to be 300.
Here is the code I have,
For Each mycell In myRng.Cells
If Not (mycell.Comment Is Nothing) Then
With mycell.Comment.Shape
.TextFrame.AutoSize = True
If .width > 300 Then
lArea = .width * .height
.width = 300
.height = (lArea / 300)
End If
End With
End If
Next mycell
mycell and myRng are Range datatypes, lArea is Long.
Now, this works relatively well but leaves extra space at the bottom of a number of comments because the area the AutoSized text takes up is different from the area of the AutoSized comment box.
Is there a way to check for blank space inside a comment and then trim it? Or is what I have the best it is going to be?
try this ... test comment has been placed in cell E4
discovered by putting Range("e4").Comment.Shape.TextFrame in the Watch window
Sub testComment()
With Range("e4").Comment.Shape
.TextFrame.AutoSize = True
lArea = .Width * .Height
.Width = 300
.Height = (lArea / .Width) ' used .width so that it is less work to change final width
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 0 ' margins need to be tweaked
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
End With
End Sub
I've changed the code in the previous comment to only resize the box if width is above 300 because otherwise the final size of small boxes were messed up. Also changed to go through all comment box on activesheet
Sub reset_box_size()
Dim pComment As Comment
For Each pComment In Application.ActiveSheet.Comments
With pComment.Shape
.TextFrame.AutoSize = True
lArea = .Width * .Height
'only resize the autosize if width is above 300
If .Width > 300 Then .Height = (lArea / .Width) ' used .width so that it is less work to change final width
.TextFrame.AutoMargins = False
.TextFrame.MarginBottom = 0 ' margins need to be tweaked
.TextFrame.MarginTop = 0
.TextFrame.MarginLeft = 0
.TextFrame.MarginRight = 0
End With
Next
End Sub
there is an autosizefunction.
here is a small code to show how to use:
Dim Rg_Com as Range , Rg_Value as Range
Set Rg_Com = Cells(1,1)
Set Rg_Value = Cells(1,2)
'Comment in A1 will be same as Text in B1:
With Rg_Com
.ClearComments
.AddComment
With .Comment
.Text Text:=Rg_Value.Value2
.Shape.TextFrame.AutoSize = True '<<< just to make all text visible in one comment, all chars having the basic size
End With
End With
I want to replicate an selected object in powerpoint using VBA code. I have a following code mention below
Sub CopySizeAndPosition()
' Usage: Select two shapes. The size and position of
' the first shape selected will be copied to the second.
Dim w As Double
Dim h As Double
Dim l As Double
Dim t As Double
With ActiveWindow.Selection.ShapeRange(1)
w = .Width
h = .Height
l = .Left
t = .Top
End With
With ActiveWindow.Selection.ShapeRange(2)
.Width = w
.Height = h
.Left = l
.Top = t
End With
End Sub
But I want to specify my value instead of getting object value. So, please help and thanx in advance!
Assuming you have selected a single shape, you can set your values like this:
' Sets the size and position of the first shape in a selection
Sub SetShapeSizeAndPosition()
With ActiveWindow.Selection.ShapeRange(1)
.Width = 100
.Height = 100
.Left = 100
.Top = 100
End With
End Sub
I am working on one project. In which i want to find out " Is my textbox going out of slide or not?" . If yes then show error msg.
so my logic is if i found the dimension of the slide then i will use it in IF...Else condition like :
If textbox_position < slide_dimension then
#Error
end if
If you have any other idea then please tell me.
Thanks
The presentation's .PageSetup.SlideWidth and .SlideHeight properties will give you the dimensions of the slide in points.
Your function would need to do something like (off top of head and out of the air ..):
Function IsOffSlide (oSh as Shape) as Boolean
Dim sngHeight as single
Dim sngWidth as Single
Dim bTemp as Boolean
bTemp = False ' by default
With ActivePresentation.PageSetup
sngHeight = .SlideHeight
sngWidth = .SlideWidth
End With
' this could be done more elegantly and in fewer lines
' of code, but in the interest of making it clearer
' I'm doing it as a series of tests.
' If any of them are true, the function will return true
With oSh
If .Left < 0 Then
bTemp = True
End If
If .Top < 0 Then
bTEmp = True
End If
If .Left + .Width > sngWidth Then
bTemp = True
End If
If .Top + .Height > sngHeight Then
bTemp = True
End If
End With
IsOffSlide = bTemp
End Function
Why you not use a placeholders of PowerPoint to make this? for example:
Sub SetText(IndexOfSlide As Integer, txt As String)
'http://officevb.com
ActivePresentation.Slides(IndexOfSlide).Shapes.Placeholders(1).TextFrame.TextRange.Text = txt
End Sub
You can call this sub and pass parameters
IndexOfSlide with a number of slide and txt with a text to create.