PDA

View Full Version : Change properties at cursor



Catiuso
06-05-2021, 01:06 AM
Hi everyone,
I am trying to replicate what powerpoint does when you change properties of what you are typing (without selecting text), eg clicking "red" as font color:


When you click red, the text you type from there is red
Using ActiveWindow.Selection.TextRange.Font.Color.RGB = RGB(255, 0, 0) does not work, as nothing is really selected (you just have your cursor at a specific position in the text)

I tried browsing the selection object properties but was not able to find anything.
Would really appreciate your help!

John Wilson
06-05-2021, 09:11 AM
I don't know a sensible way but here is a clunky way ...


Sub apply_Red()
Dim otxr2 As TextRange2
On Error Resume Next
Set otxr2 = ActiveWindow.Selection.TextRange2
If Not otxr2 Is Nothing Then
otxr2.InsertAfter ("A")
otxr2.Font.Fill.ForeColor.RGB = vbRed
SendKeys "{BKSP}"
End If
End Sub

Catiuso
06-12-2021, 02:43 AM
Thank you so much John!! It is clunky but at least it works (what I have not been able to so far)! I have customized it so it changes the typed text to red when it is black and vice versa. Maybe there's sth to do it in a "cleaner" way by sort of selecting the character after where you are typing. Although don't know how if you are at the end of your textbox.
For reference my code:

Sub redfont()

Set otxr2 = ActiveWindow.Selection.TextRange2
Dim lettre_avt As Integer

If (Not otxr2 Is Nothing) Then
If otxr2 = "" Then
lettre_avt = ActiveWindow.Selection.TextRange2.Start - 1
If ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(lettre_avt ).Font.Color.RGB <> 0 Then
'type black
otxr2.InsertAfter ("A")
otxr2.Font.Fill.ForeColor.RGB = vbBlack
SendKeys "{BKSP}"
Else
'type red
otxr2.InsertAfter ("A")
otxr2.Font.Fill.ForeColor.RGB = vbRed
SendKeys "{BKSP}"
End If
End If
End If

End Sub

John Wilson
06-12-2021, 05:05 AM
I hate cluncky too but AFAIK there's no other way. Hopefully someone else will pop up with a better way!