View Full Version : Inserting text in superscript mode (and getting back to normal after)
RandomGerman
08-27-2017, 08:07 AM
Hi all,
adding a footnote number in superscript means typing the number, marking it, use superscript - and while going on typing, the superscript mode remains ...
My idea: I would like to have a macro allowing the user to insert some text to an Input box and this text gets inserted as superscripted text (or number), exactly where the cursor is located. But after inserting, the user can go on typing in normal mode without the need to switch back anything to normal.
But unfortunatley I'm not getting this superscript-and-back hing right.
Can anyone help me fix it to the way I described?
Thank you,
RG
Option Explicit
Sub AutoSuperscript()
Dim Message As String
Dim Title As String
Dim Default As String
Dim myValue As String
'The Input box works well
Message = "Insert your superscripted text"
Title = "Superscript Input"
Default = "1"
myValue = InputBox(Message, Title, Default)
'Handles if User cancels
On Error GoTo UserCancels
'Adding the text at the position of the cursor - usually the end of a word - works, but it is not set to superscript mode:
With ActiveWindow.Selection
.TextRange2.Font.Superscript = msoTrue
.TextRange2.Text = myValue
.TextRange2.Font.Superscript = msoFalse
End With
UserCancels:
Exit Sub
End Sub
John Wilson
08-27-2017, 12:17 PM
Try this.
Note that cancel does not usually throw an error but does set myValue to NULL so it would look like nothing happened. Checking the strPtr value will show that it was really cancelled.
Sub AutoSuperscript()
Dim Message As String
Dim Title As String
Dim Default As String
Dim txtRange As TextRange2
Dim txtrangeSS As TextRange2
Dim myValue As String
'The Input box works well
Message = "Insert your superscripted text"
Title = "Superscript Input"
Default = "1"
myValue = InputBox(Message, Title, Default)
'Handles if User cancels
'On Error GoTo UserCancels
'I don't thing your original method really works
If StrPtr(myValue) = False Then GoTo UserCancels
'Adding the text at the position of the cursor - usually the end of a word - works, but it is not set to superscript mode:
Set txtRange = ActiveWindow.Selection.TextRange2
Set txtrangeSS = txtRange.InsertAfter(myValue)
txtrangeSS.Font.Superscript = True
'turn OFF SS
CommandBars.ExecuteMso ("Superscript")
Exit Sub
UserCancels:
Msgbox "Cancelled"
End Sub
RandomGerman
08-28-2017, 01:58 AM
Understand. So I better add a second error handling for cases like, e.g., the user clicks the macro without having placed the cursor into the text, because that's a "real" error, not a cancellation.
Thank you, John, ExecuteMso ("Superscript") is a great solution. Is it correct that these ExcetuteMso commands only work from PPT2010 onwards? I think I remember something like that.
RandomGerman
10-07-2018, 08:48 AM
Sometimes it takes a year to find out, not everything is working properly:
In case the cursor is placed between two letters (without a blankspace inbetween) while activating the macro, the myValue text is added, but in normal mode, not in superscript mode. This seems to have to do with ExecuteMso, as it is not happening, when I deactivate the code line including ExecuteMso. But that's no option finally, as it was the idea of the macro to return to normal mode after inserting the myValue text. And it seems to have to do with blankspaces. When there is none, all superscripted text gets executed in that passage.
This may not happen often with superscript, but I created the same macro for subscript and things like H2O with a subscripted 2 between H and O and without blankspaces don't work this way. Any ideas how to fix this are highly appreciated. Thank you in advance.
John Wilson
10-07-2018, 12:58 PM
I don't think it's possible if the insert point is in the middle of a word. It can't even be done manually AFAIK.
RandomGerman
10-09-2018, 08:00 AM
Huh, surprising. But if John doesn't know a solution, I guess there is none. ;-) Thank you!
John Wilson
10-12-2018, 08:44 AM
If you insert text immediately after sub / superscripted text then it will always (AFAIK) also be sub / superscript.
John Wilson
10-12-2018, 08:44 AM
If you insert text immediately after sub / superscripted text in a word then it will always (AFAIK) also be sub / superscript.
RandomGerman
03-24-2020, 09:13 AM
One workaround and one new problem:
Sub AutoSuper()
Dim Message As String
Dim Title As String
Dim Default As String
Dim txtRange As TextRange2
Dim txtrangeSS As TextRange2
Dim myValue As String
'Input box
Message = "Insert your superscripted text"
Title = "Superscript Input"
Default = "1"
myValue = InputBox(Message, Title, Default) + " "
'Handles if User cancels
If StrPtr(myValue) = False Then GoTo err
On Error GoTo err
Set txtRange = ActiveWindow.Selection.TextRange2
Set txtrangeSS = txtRange.InsertAfter(myValue)
txtrangeSS.Font.Superscript = True
'turn OFF SS
CommandBars.ExecuteMso ("Superscript")
Exit Sub
err:
Exit Sub
End Sub
The workaround is for #4: For the case the user places the cursor between two letters, the addition of a superscripted blank space (in the code line with myValue) is a good workaround. The input is superscripted then, as I always wanted. Deleting the one blank after, if necessary, is not too much additional work.
The new problem:
It seems newer versions of PPT handle something different than older versions (2010/13), but only in one special case. When the cursor is placed after (!) a blank, the superscripted text is added as it should, but when the user continues typing from that position, his new text still is superscripted, which is not the case, when the cursor is placed after a letter. Any ideas how to avoid this? It is not happening in 2010 and 2013, everything works well there.
Thank you in advance.
RandomGerman
03-25-2020, 05:56 AM
Additional information to the new problem in #9: It only happens, when one clicks the macro with the cursor in the ultimate position of the text field, shape or placeholder. Not somewhere in the middle of the text.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.