Consulting

Results 1 to 8 of 8

Thread: Inserting text in superscript mode (and getting back to normal after)

  1. #1
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    144
    Location

    Inserting text in superscript mode (and getting back to normal after)

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,710
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    144
    Location
    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.

  4. #4
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    144
    Location
    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.

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,710
    Location
    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.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    144
    Location
    Huh, surprising. But if John doesn't know a solution, I guess there is none. ;-) Thank you!

  7. #7
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,710
    Location
    If you insert text immediately after sub / superscripted text then it will always (AFAIK) also be sub / superscript.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    1,710
    Location
    If you insert text immediately after sub / superscripted text in a word then it will always (AFAIK) also be sub / superscript.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •