VBA formatting strings - vba

As of right now everything is red. I need everything after the colon to be black and not be Italic.

You just need to isolate the characters after the colon and set the colour as required:
EDIT: Forgot about the italic bit......
Sub test()
sCallOut = "ACTION: [Insert Callout Here]"
Set oShp = pptSld.Shapes.AddTextbox(msoTextOrientationHorizontal, 110, 100, 557.28, 94.32)
oShp.Line.Visible = msoFalse
oShp.TextFrame.TextRange.Text = sCallOut
oShp.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
With oShp.TextFrame.TextRange.Font
.Name = "Corbel"
.Italic = msoTrue
.Size = 20
.Color.RGB = RGB(216, 3, 33)
.Bold = msoTrue
End With
With oShp.TextFrame.TextRange
With .Characters(InStr(.Characters, ":") + 1, .Length).Font
.Color.RGB = RGB(0, 0, 0)
.Italic = msoFalse
End With
End With
Set oShp = Nothing
End Sub

You can change this line to make the text black:
.Color.RGB = RGB(216, 3, 33)
And this line to remove italics:
.Italic = msoFasle
EDIT:
If you have the cell location, you can do this to the string later on:
With Cells(1, 1).Characters(9, 21).Font
.Color = vbBlack
.Italic = False
.Name = "Corbel"
.Size = 20
This shows how to use Characters
EDIT:
To change part of the text for a text box, you can declare two variables containing strings formatted differently and append them to the textbox.
It will follow the form:
MyTextBox.Value = MyStringVariable
or in your case:
MyTextBox.Value = MyStringVariable1 & MyStringVariable2

The macro recorder found the following:
Sub test()
ActiveSheet.Shapes.Range(Array("txtbox1")).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
"Choisir: wefhweufhwef 344tr saefaefa" 'Entering some text'
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 5).Font 'Selecting some font and changing some settings
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 255)
.Fill.Transparency = 0
.Fill.Solid
.Size = 11
.Name = "+mn-lt"
End With
End Sub
I haven't managed to do it without selecting the textbox first (?? maybe someone can help, usually it isn't hard). You can enter your text with a string in which you figure out the position of the colon using
dim MyString as string
dim intColon as integer
Mystring = "cwsvws:wifvwhivw"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = MyString
intColon = instr(MyString, ":")
And use this as the character count to plug in the macro recorder's code. Then you can format different blocks of text how you want.
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(intColon, 5).Font
.Fill.ForeColor.RGB = RGB(0, 0, 255)
.Fill.Transparency = 0
.Fill.Solid
.Size = 11
.Name = "+mn-lt"
End With
Hope this helps.

Related

Add Textbox at the cursor position in a table

I want to add, via VBA in MS Word, a Textbox at the current cursor position.
This works but if the cursor is located within a table (whatever cell), a Textbox gets added in the wrong location.
Sub AddTextBox()
Dim oShape As Shape
Dim x As Long
Dim y As Long
'get Cursorposition
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
Set oShape = ActiveDocument.Shapes.AddTextBox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=x, Top:=y, Width:=200, Height:=12)
With oShape
With .TextFrame
With .TextRange
.Text = "TEXT"
.Font.Name = "Segoe Script"
.Font.Size = 10
.Font.ColorIndex = wdRed
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
.MarginBottom = Application.CentimetersToPoints(0.15)
.MarginTop = Application.CentimetersToPoints(0.15)
.MarginLeft = Application.CentimetersToPoints(0.1)
.MarginRight = Application.CentimetersToPoints(0.1)
.WordWrap = False
.AutoSize = True
End With
.LockAnchor = False
.WrapFormat.Type = wdWrapNone
.Fill.Visible = msoFalse
.Line.Visible = msoFalse
.Height = Application.CentimetersToPoints(0.8)
End With
End Sub
How do I place the TextBox at the cursor position, when the cursor is placed in a table?
When you add a shape the position defaults to being relative to the margin, so you need to set the required relative position after you have added the shape.
Sub AddTextBox()
Dim oShape As Shape
Dim x As Long
Dim y As Long
'get Cursorposition
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
Set oShape = ActiveDocument.Shapes.AddTextBox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=x, Top:=y, Width:=200, Height:=12, Anchor:=Selection.Range)
With oShape
.LeftRelative = wdRelativeHorizontalPositionPage
.TopRelative = wdRelativeVerticalPositionPage
.LockAnchor = False
.WrapFormat.Type = wdWrapNone
With .TextFrame
With .TextRange
.Text = "TEXT"
.Font.Name = "Segoe Script"
.Font.Size = 10
.Font.ColorIndex = wdRed
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
.MarginBottom = Application.CentimetersToPoints(0.15)
.MarginTop = Application.CentimetersToPoints(0.15)
.MarginLeft = Application.CentimetersToPoints(0.1)
.MarginRight = Application.CentimetersToPoints(0.1)
.WordWrap = False
.AutoSize = True
End With
.Fill.Visible = msoFalse
.Line.Visible = msoFalse
.Height = Application.CentimetersToPoints(0.8)
End With
End Sub

Powerpoint VBA - write function to create shape

I have the following function, which I intended to create a shape:
Public Function LinkToAddInfo(ShapeName As String, BoxName As String, DisplayNumber As Long, AddName As String, TrueNumber As Long) As Shape
Dim ShapeName As Shape
Set ShapeName = .Shapes.AddShape(msoShapeRoundedRectangle, 640, 470, 71, 27)
With ShapeName
.Fill.ForeColor.RGB = RGB(191, 191, 191)
.Fill.Transparency = 0
.Name = BoxName
With .Line
.Weight = 0
.ForeColor.RGB = RGB(191, 191, 191)
.Transparency = 0
End With ' Outline
With .TextFrame.TextRange
.Text = "Add. Info " & DisplayNumber & vbNewLine & AddName
With .Font
.Name = "Arial"
.Size = 8
.Bold = msoTrue
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.RGB = RGB(255, 255, 255)
End With ' Font
End With ' TextFrame
End With ' Square itself
End Function
I tried to call it from within a module using:
LinkToAddInfo("tiny1", "Yedinfo1", DisplayNumber, AddName, AddNumber)
But it throws an error (the code is shown in red within the editor).
When I have all the code within the module itself, it works fine. I'm just struggling to transcribe it into an external function (which I want to do so that I don't have to repeat this code again and again).
How can I achieve this?
You have several problems with this.
You don't need a Function for this, since you aren't returning data to the program. Instead, use a Sub.
The first argument is a string, but then you try to use the same variable name in a declaration as a shape. But you don't use the string argument for anything, so it can be deleted. You also don't use TrueNumber, so that can be taken out.
To access a slide master, you need to use Designs with a numeric argument, not SlideMaster.
The following should do what you want:
Public Sub LinkToAddInfo(BoxName As String, DisplayNumber As Long, AddName As String)
Dim oShape As Shape
Set oShape = ActivePresentation.Designs(1).SlideMaster.Shapes.AddShape(msoShapeRoundedRectangle, 640, 470, 71, 27)
With oShape
.Fill.ForeColor.RGB = RGB(191, 191, 191)
.Fill.Transparency = 0
.Name = BoxName
With .Line
.Weight = 0
.ForeColor.RGB = RGB(191, 191, 191)
.Transparency = 0
End With ' Outline
With .TextFrame.TextRange
.Text = "Add. Info " & DisplayNumber & vbNewLine & AddName
With .Font
.Name = "Arial"
.Size = 8
.Bold = msoTrue
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.RGB = RGB(255, 255, 255)
End With ' Font
End With ' TextFrame
End With ' Square itself
End Sub
```

Shadow is applying on second click

Was trying to script to apply fill colour and shadows together with the help of below scripts
Sub Blue1() 'blue 1
Dim sldFirst As Slide
Set sldFirst = ActivePresentation.Slides(1)
For Each Shape In ActiveWindow.Selection.ShapeRange
Shape.Fill.ForeColor.RGB = RGB(69, 159, 237)
Shape.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignCenter
Shape.Line.Visible = False
With Shape.Shadow
.Size = 100
.ForeColor.RGB = RGB(0, 112, 192)
.Transparency = 0.8
.Blur = 15
.OffsetX = 0
.OffsetY = 3
End With
Next
End Sub
But when i run the code on first click only it applies the fill colour for shadow i have to click run button second time. can you please help me to sort this out.
Thanks in advance
I think you found a bug. The color should get applied the first time around. However, running the command a second time does the job:
Sub Blue1() 'blue 1
Dim sldFirst As Slide
Set sldFirst = ActivePresentation.Slides(1)
For Each Shape In ActiveWindow.Selection.ShapeRange
With Shape
.Fill.ForeColor.RGB = RGB(69, 159, 237)
.TextFrame.TextRange.ParagraphFormat.Alignment = msoAlignCenter
.Line.Visible = False
With .Shadow
.Size = 100
.Transparency = 0.8
.Blur = 15
.OffsetX = 0
.OffsetY = 3
.ForeColor.RGB = RGB(0, 112, 192)
.ForeColor.RGB = RGB(0, 112, 192)
End With
End With
Next
End Sub

How do I format individual lines in a textbox via Microsoft PowerPoint macros?

I want a textbox where the first line and subsequent lines of text have different formatting, but they must be in the same textbox. This is what I currently have, which applies the same formatting to all text.
Sub geberateSlide()
...
With currSlide.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=headerWidth, Height:=headerHeight)
.TextFrame.TextRange.Text = "Test Box" & vbCrLf & "Description"
.TextFrame.AutoSize = ppAutoSizeNone
.Height = headerHeight
.Line.ForeColor.RGB = RGB(0,0,0)
.Line.Visible = True
End With
...
End Sub
The text should be Arial 8. Line 1 should be black and bold, while subsequent text should be blue.
.TextFrame.TextRange.Lines(0, 1) will target the first line.
%300 Zoom
With currSlide.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=headerWidth, Height:=headerHeight)
.Height = headerHeight
.TextFrame.AutoSize = ppAutoSizeNone
With .TextFrame.TextRange
.Text = "Test Box" & vbCrLf & "Description"
With .Font
.Color = vbBlue
.Size = 8
.Name = "Arial"
End With
With .Lines(1).Font
.Color = vbBlack
.Bold = msoTrue
End With
End With
End With

Add a shape to a slide and format that

I am trying to make my vba script below to add annotation notes to powerpoint slides. The idea is that the script can be used to add "to-be-checked notes" to slides. Hence, I've got it set up in a little add-in that displays a menu so adding the TBC, TBU, TBD notes are added.
The sub is showing errors from time to time and does not always fully do its job (i guess because of the part where I wrote in my code:
ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
Can anyone assist me how do make the script bulletproof. A short explanation of the approach would be great. That way I can learn how do things right in the future.
Best,
eltiburon
This is what my entire script so far looks like:
Sub InsertShape_TBC()
ActiveWindow.Selection.SlideRange.Shapes.AddShape(msoShapeRectangle, 575.5, 9.12, 124.75, 34.12).Select
With ActiveWindow.Selection.ShapeRange
.Fill.Visible = msoTrue
.Fill.Solid
.Fill.ForeColor.RGB = RGB(162, 30, 36)
.Fill.Transparency = 0#
.Line.Visible = msoFalse
End With
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=1, Length:=0).Select
With ActiveWindow.Selection.TextRange
.Text = "[TBC]"
With .Font
.Name = "Arial"
.Size = 18
.Bold = msoFalse
.Italic = msoFalse
.Underline = msoFalse
.Shadow = msoFalse
.Emboss = msoFalse
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.SchemeColor = ppForeground
End With
End With
ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
ActiveWindow.Selection.SlideRange.Shapes("Rectangle 4").Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Select
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=1, Length:=6).Select
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
ActiveWindow.Selection.TextRange.Font.Color.RGB = RGB(Red:=255, Green:=255, Blue:=255)
ActivePresentation.ExtraColors.Add RGB(Red:=255, Green:=255, Blue:=255)
ActiveWindow.Selection.Unselect
End Sub
This looks like the kind of code produced by the macro recorder in earlier versions of PPT.
First off, never select anything in code unless it's absolutely necessary to do so (and it seldom is). Use shape references instead (as you've seen in a couple of the other examples I posted in response to your other questions).
Because the recorded macro assumes that you're working with a shape called Rectangle 4, it will only work if you run it on a slide that has three rectangles already. So instead:
Dim oSh as Shape
Set oSh = ActiveWindow.Selection.SlideRange.Shapes.AddShape(msoShapeRectangle, 575.5, 9.12, 124.75, 34.12)
' Notice that I removed the .Select from the end of your code.
' We don't want to select anything if we don't have to.
' Then
With oSh
With .TextFrame.TextRange
.Text = "[TBC]"
With .Font
.Name = "Arial"
.Size = 18
.Bold = msoFalse
.Italic = msoFalse
.Underline = msoFalse
.Shadow = msoFalse
.Emboss = msoFalse
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.SchemeColor = ppForeground
End With ' Font
End with ' TextRange
End With ' oSh, the shape itself