View Full Version : Setting active font properties within a textbox
crossharu
12-10-2017, 04:36 AM
Hi,
This is a fairly basic issue which I've been trying to solve on and off for weeks!
I currently have a macro which sets the formatting of selected to a predefined format. This works well but I have been asked to edit it so that you can click the button and then type in the desired format as compared to typing first, selecting that text and then clicking the button.
What am I missing that would allow this?
Many thanks!
Paul_Hossler
12-10-2017, 08:39 AM
Without actually seeing the macro, it's a little hard to see what's missing
crossharu
12-11-2017, 03:30 AM
Hi Paul - please see below. Thanks
Sub RedStrikethrough()
Dim Text As TextRange2
If ActiveWindow.Selection.Type = ppSelectionText Then
Set Text = ActiveWindow.Selection.TextRange2
With Text
.Words.Font.Fill.ForeColor.RGB = vbRed
.Words.Font.Strikethrough = msoTrue
End With
End If
End Suba
John Wilson
12-11-2017, 09:16 AM
Not sure I understand how the user will input the required format but be aware you current code will not work if the user selects characters in the word - Only those selected will change. If you need the WORD to change regardless of the selection you wil need more complex code.
Sub RedStrikethrough()
Dim otr2 As TextRange2
Dim allText As TextRange2
Dim L As Long
If ActiveWindow.Selection.Type = ppSelectionText Then
Set allText = ActiveWindow.Selection.ShapeRange(1).TextFrame2.TextRange
Set otr2 = ActiveWindow.Selection.TextRange2
L = selectedWord(otr2, allText)
allText.Words(L).Font.Fill.ForeColor.RGB = vbRed
End If
End Sub
Function selectedWord(otr2 As TextRange2, allText As TextRange2) As Long
Dim L As Long
For L = 1 To allText.Words.Count
If otr2.Start < allText.Words(L).Start + allText.Words(L).Length Then
selectedWord = L
Exit For
End If
Next
End Function
This might need to be even more complex to NEVER fail. It is not clear if the user can select multiple words.
crossharu
12-11-2017, 09:50 AM
Thanks for the reply John.
The intention is for the desired format to be pre-defined so there is no need for user input in that regard. In addition, when selecting individual characters/words (or the entire textframe for that matter) my code above functions as desired.
It is only when the cursor is placed at the end of the text with no text selected, for example, and the button is clicked that it does not behave as desired. In this scenario it should set the active font which will be used when typing starts to the pre-defined format (just as the font colour or bold buttons would work, for example) but instead it does nothing.
The code definitely executes and
If ActiveWindow.Selection.Type = ppSelectionText returns true so that's not the issue but I can't get any further I'm afraid.
Please let me know if I have missed the point of your post!
Thanks
John Wilson
12-11-2017, 10:46 AM
I think the only way might be to insert a space set to red and select it., When you type the space would be overwritten. Not elegant but should work.
Sub StartTypinginRed()ActiveWindow.Selection.TextRange2.InsertAfter(" ").Select
ActiveWindow.Selection.TextRange2.Font.Fill.ForeColor.RGB = vbRed
End Sub
crossharu
12-24-2017, 10:16 AM
Works well, thanks very much.
Interestingly, when I do the above and then press backspace to delete the inserted space, the formatting persists when I then start typing. However, when I inserted a line after your suggestion (ActiveWindow.Selection.Delete) to do the same, this did not have the desired effect. Presumably this is just an oddity of Powerpoint.
Thanks again.
John Wilson
12-28-2017, 02:01 AM
I don't like using SendKeys but see if this works
Sub StartTypinginRed()
With ActiveWindow.Selection.TextRange2.InsertAfter(" ")
.Font.Fill.ForeColor.RGB = vbRed
End With
SendKeys "({BACKSPACE})"
End Sub
crossharu
12-30-2017, 12:05 PM
It works flawlessly even if it isn't that elegant! Thanks a lot, John.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.