Consulting

Results 1 to 10 of 10

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

  1. #1
    VBAX Contributor
    Joined
    Apr 2015
    Location
    Germany
    Posts
    167
    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
    2,093
    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
    167
    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
    167
    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
    2,093
    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
    167
    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
    2,093
    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
    2,093
    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

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

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

Posting Permissions

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