TextBox object customisation - Compile error: Invalid or unqualified reference - excel

I would like a textbox like this in my Excel spreadsheet:
I used this query: VBA Shapes.AddTextbox Method
I modified the code:
Sub asbuiltstamp()
Set myDocument = Worksheets(1)
myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _
800, 50, 200, 75) _
.TextFrame.Characters.Text = "City Fibre As-Built"
.Font.ColorIndex = 3
.Font.Size = 20
.Font.HorizontalAlignment = xlCenter
.Shapes.Rotation = 45
.Shapes.Fill = False
End Sub
I get:
Compile error: Invalid or unqualified reference.
How can I customise my textbox with VBA Excel?
How can I set its own name (other than "Textbox1")?

You've tried to access several properties without specifying what they are properties of. You need something like this:
Sub asbuiltstamp()
Set myDocument = Worksheets(1)
With myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 800, 50, 200, 75)
With .TextFrame
.HorizontalAlignment = xlCenter
With .Characters
.Text = "City Fibre" & vbLf & "As-Built"
With .Font
.Bold = True
.ColorIndex = 3
.Size = 20
End With
End With
End With
.Rotation = 45
.Fill.Visible = False
End With
End Sub

Related

Excel VBA changing the text within the textbox

I am trying to change the number in my text box.
My code so far looks like this:
Sub Box()
ActiveSheet.Shapes("Asbuilt_Number1").Copy
ActiveSheet.Range("C25").PasteSpecial
Selection.ShapeRange.TextFrame.textRange.Characters.text = "2"
Selection.Name = "Asbuilt_Number"
End Sub
what is based from the Macro
Sub Boxes_Two()
'
' Macro3 Macro
'
'
ActiveSheet.Shapes.Range(Array("Asbuilt_Number_1")).Select
Selection.Copy
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft -0.75
Selection.ShapeRange.IncrementTop 347.25
Selection.ShapeRange(1).TextFrame2.textRange.Characters.text = "2"
With Selection.ShapeRange(1).TextFrame2.textRange.Characters(1, 1). _
ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignCenter
End With
With Selection.ShapeRange(1).TextFrame2.textRange.Characters(1, 1).Font
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 15
.Name = "+mn-lt"
End With
ActiveSheet.Shapes.Range(Array("Asbuilt_Number_1")).Select
Selection.ShapeRange.Name = "Asbuilt_Number"
Selection.Name = "Asbuilt_Number"
End Sub
I am unhappy with macro, since my copied number goes in completely different place, than I targeted.
My non-macro code throws error: Object doesn't support this property or method
at the line
Selection.ShapeRange.TextFrame.textRange.Characters.text = "2"
Even if I remove the Characters, likewise in the template below:
https://learn.microsoft.com/en-us/office/vba/api/project.shaperange.textframe2
How can I change the name of my textboxes swiftly?
This works for me, let's try the following code:
Note: ActiveSheet.Range("C25").Paste will not work
Sub Box()
With ActiveSheet
.Shapes("Asbuilt_Number1").Copy
[C25].Activate
.Paste
.Shapes(.Shapes.Count).Name = "Asbuilt_Number2"
.Shapes("Asbuilt_Number2").TextFrame2.TextRange.Characters.Text = "2"
End With
End Sub

Bold only one portion in VBA

I am currently building a macro that will create a Powerpoint from an excel workbook. I have two cells that I currently combine into one powerpoint textbox. I want to be able to bold one cell value in the textbox. Is this possible?
This is my current code:
Proj = Sheets("Bay du Nord").Range("A23")
Proj2 = Sheets("Bay du Nord").Range("B23")
Set LCProj = Slide2.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=210, Top:=265, Width:=110, Height:=100)
With LCProj
.Name = "LC Proj"
With .TextFrame.TextRange
.Text = Proj & vbNewLine & Proj2 & vbNewLine & "kg CO2/BOE"
.Font.Size = 12
.Font.Bold = False
.ParagraphFormat.Alignment = ppAlignCenter
End With
With .Fill
.TwoColorGradient msoGradientHorizontal, 2
.ForeColor.RGB = RGB(140, 0, 0)
.BackColor.RGB = RGB(180, 5, 0)
End With
.Shadow.Type = msoShadow14
End With
Say I want to bold the value that is in cell A23 (Proj) and leave the value in B23 (Proj2) the same.
Thanks
You can use TextRange.Characters() to change the properties of a subset of the original TextRange like so:
Proj = Sheets("Bay du Nord").Range("A23")
Proj2 = Sheets("Bay du Nord").Range("B23")
Set LCProj = Slide2.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=210, Top:=265, Width:=110, Height:=100)
With LCProj
.Name = "LC Proj"
With .TextFrame.TextRange
.Text = Proj & vbNewLine & Proj2 & vbNewLine & "kg CO2/BOE"
.Font.Size = 12
.Font.Bold = False
.ParagraphFormat.Alignment = ppAlignCenter
With .Characters(1, len(Proj))
.Font.Bold = True
End With
End With
With .Fill
.TwoColorGradient msoGradientHorizontal, 2
.ForeColor.RGB = RGB(140, 0, 0)
.BackColor.RGB = RGB(180, 5, 0)
End With
.Shadow.Type = msoShadow14
End With

Add box around text for Excel to Powerpoint VBA

I am importing data from Excel into Powerpoint slides. How would I be able to add a box/outline around the textframe? Also is there any way in which I can add a static outline that does not move, even if I have no text in the cell I am importing from?
Thank you for any and all help!
Here's snippet of my code:
Set ppSlide2 = ppPres.Slides.Add(i + 1, ppLayoutBlank).Shapes
Set HeaderPPT = ppSlide2.AddTextbox(msoTextOrientationHorizontal, 75, 150, 800, 700).TextFrame.TextRange
If Sheet1.Range("P" & RowExcel).Text = "Title" Then
With HeaderPPT
.Text = vbNewLine & Cells(RowExcel, col + 9)
.ParagraphFormat.Alignment = ppAlignCenter
.Font.Bold = True
.Font.Name = "Calibri Light"
.Font.Size = 55
End With
End If
TextFrame or TextRange doesn't have the border element.
The parent 'Shape' can have borders like this.
Dim sld as Slide, shp as Shape
Set sld = Activepresentation.Slides(1)
Set shp = sld.Shapes.AddShapes(msoShapeRectangle, 100,100,100,100)
With shp
.Line.Visible = True
.Line.Weight = 2
.Line.ForeColor.RGB = RGB(255,0,0)
End with
To put a static border, I think you can add a rectangle or TextBox first.
And then add a TextBox again with Excel cell contents.
(But if you add a Textbox with AddTextBox, the shape's border is static by default even if it has no text in it.)
Any way, I tested the following code:
Set ppSlide2 = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count, ppLayoutBlank).Shapes
'first, add a box(rectangle) or textbox so that the z-order can be lower than the next Textbox
Set HeaderBOX = ppSlide2.AddShape(msoShapeRectangle, 75, 150, 800, 700)
'or Set HeaderBOX = ppSlide2.AddTextbox(msoTextOrientationHorizontal, 75, 150, 800, 700)
With HeaderBOX
.Name = "HeaderBOX"
.Fill.Visible = False 'make it transparent
.Line.Visible = True
.Line.ForeColor.RGB = rgbBlack 'RGB(0,0,0)
.Line.Weight = 2
End With
'then, add a textbox
Set HeaderPPT = ppSlide2.AddTextbox(msoTextOrientationHorizontal, 75, 150, 800, 700).TextFrame.TextRange
With HeaderPPT
.Text = vbNewLine & "Excel Cell Text Here"
.ParagraphFormat.Alignment = ppAlignCenter
.Font.Bold = True
.Font.Name = "Calibri Light"
.Font.Size = 55
End With

VBA code to bold all numbers within a selection in a textbox?

I have code to change selected text's font to Arial and size 10, it's quite simple:
Sub Arial10()
With Selection.ShapeRange.TextFrame2.TextRange.Font
.name = "Arial"
.Size = 10
End With
End Sub
How can I add to this macro to bold any numbers and dashes ("-") within this selection?
Here it goes....
Sub Arial10()
With Selection.ShapeRange.TextFrame2.TextRange.Font
.Name = "Arial"
.Size = 10
End With
'-----BOLD all numbers and dashes-----
With Selection.ShapeRange.TextFrame2.TextRange
Dim i As Long
For i = 1 To Len(.Text)
If Mid(.Text, i, 1) Like "#" Or _
Mid(.Text, i, 1) = "-" Then
.Characters(i, 1).Font.Bold = True
End If
Next
End With
End Sub
Another take:
Sub Arial10andBoldStuff()
Dim shp As ShapeRange
Dim i As Long
Dim Char As Object
Set shp = Selection.ShapeRange
With shp.TextFrame2.TextRange
With .Font
.Name = "Arial"
.Size = 10
End With
For i = 1 To .Characters.Count
Set Char = .Characters(i, 1)
If IsNumeric(Char) Or Char = "-" Then
Char.Font.Bold = True
End If
Next i
End With
End Sub
Setting the shape to a variable is helpful for getting the Intellisense.

how to create a button in the first blank cell of a column

i'm trying to create a button with the following:
Selection.Characters.Text = "Hide"
With Selection.Characters(Start:=1, Length:=4).Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 1
i wish for the button to appear in a blank cell on the end of column B, after cell B4 (cells B1-B3 may not always have a value as well)
thank you
just to share, i ended up finding the code, was a lot simpler than i thought...:
Dim btn As Button
Dim rng As Range
With Worksheets("Sheet1")
Set rng = .Range("B4").End(xlDown).Offset(1, 0)
Set btn = .Buttons.Add(rng.Left, rng.Top, 57.75, 34.5)
With btn
.Caption = "Hide"
.OnAction = "Macro2"
End With
End With
i guess the specification of the font & stuff wasn't that important...

Resources