Consulting

Results 1 to 6 of 6

Thread: Selection refusing to 'unselect' the superscript button (ctrl shift plus-sign)

  1. #1
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    3
    Location

    Selection refusing to 'unselect' the superscript button (ctrl shift plus-sign)

    Sub cs_citation_01()
    '   cs_citation_01 Macro
        
        Dim fontSize As Integer
        Dim fontName As String
        
        Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        
        fontSize = Selection.Font.Size
        fontName = Selection.Font.Name
        
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        
    
        SendKeys "{RIGHT}"
    
        With Selection
            .Font.Size = 14
            .Font.Superscript = True
            .TypeText Text:="[" & websiteName & "]"
            selectTextToFormat (websiteName)
            .Hyperlinks.Add Anchor:=Selection.Range, TextToDisplay:=Selection.Range.Text, Address:=hyperLink     'TextToDisplay:=Selection.Text
        End With
    
        Selection.Font.Superscript = False
    
        'Superscript button still selected on MS Wo
        
    '   Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    '    SendKeys "{RIGHT}"
    '    SendKeys " "
        
    '    Selection.Font.Size = fontSize
    '    Selection.Font.Name = fontName
        
        'MsgBox ("Ln53")
    End Sub
    I need the program to deselect, the SuperScript Notation, and to revert back to the font size of the character, preceding the bracket, [], tags.


    Edit: The following is closest to what I'm trying to do, due to Extend:=wdMove instead of Extend:=wdExtend. But when used on the last line, the superscript notation is still 'enabled' in the GUI.

        Selection.Font.Superscript = False
        
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
    Last edited by donedown; 02-14-2020 at 03:38 PM.

  2. #2
    Why not use the replace function?
    Sub Macro1()
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            With .Replacement.Font
                .Superscript = False
                .Subscript = False
            End With
            .Text = "\[*\]"
            .Replacement.Text = "^&"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
    End Sub
    or simply select the whole document, apply superscript then remove the superscript.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,963
    Location
    So what does 'selectTextToFormat' do? It's not in the code you posted. Likewise, there's no indication of what 'websiteName' and 'Hyperlink' are or contain.

    Perhaps all you need is something along the lines of:
    Sub Demo()
    Dim Rng As Range
    Const websiteName As String = "VBA Express"
    Const Hyperlink As String = "http://www.vbaexpress.com/forum/showthread.php?66803-Selection-refusing-to-unselect-the-superscript-button-(ctrl-shift-plus-sign)"
        Set Rng = Selection.Range
        With Rng
            .Collapse wdCollapseEnd
            .Hyperlinks.Add Anchor:=.Duplicate, TextToDisplay:="[" & websiteName & "]", Address:=Hyperlink
            .End = .End + 1
            With .Hyperlinks(1).Range
              .Font.Size = 14
              .Font.Superscript = True
            End With
            .Start = .Hyperlinks(1).Range.End
            .Text = " "
            .Font.Reset
            .Select
        End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    3
    Location
    Thank your for the responses.

    To gmayor, there would be multiple instances of square bracket, through out the document, as much as 100 or more. This macro would insert the bracket, before the present cursor location.

    To macropod, the code is much closer to what I'm trying to achieve, but there are 3 issues.

    It seems the statement:
    Text = " "

    Would insert space, if the macro is executed in the middle of sentence, which is undesirable.

    Also, if set the font of the sentence is set to a non-default (Calibri 11) to a different one (Times New Roman 12), Font.Reset, reverts to Calibri 11, instead of Times New Roman 12.

    Also I would only like the text inside of the square brackets to be hyperlinked.

    I've attached the full code, forum won't let new users post any protocol characters related to web services, including at symbol:
    Cursor may be placed anywhere, preferable middle or end of a sentence, and then macro can be executed.
    Attached Files Attached Files
    Last edited by donedown; Yesterday at 12:20 PM.

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,963
    Location
    Quote Originally Posted by donedown View Post
    It seems the statement:
    Text = " "

    Would insert space, if the macro is executed in the middle of sentence, which is undesirable.
    Well, you wouldn't be concerned about whether the selection remains hyperlinked if you weren't about to do some more typing. As it is, the inserted space is left selected so it can be overtyped with whatever you want to have follow the hyperlink.
    Quote Originally Posted by donedown View Post
    Also, if set the font of the sentence is set to a non-default (Calibri 11) to a different one (Times New Roman 12), Font.Reset, reverts to Calibri 11, instead of Times New Roman 12.
    That's a self-inflicted problem caused by you not using Styles properly. You can revert to your own font capture & restoration code if you prefer.
    Quote Originally Posted by donedown View Post
    Also I would only like the text inside of the square brackets to be hyperlinked.
    In that case, use something along the lines of:
    Sub Demo()
    Dim Rng As Range
    Const websiteName As String = "VBA Express"
    Const Hyperlink As String = "http://www.vbaexpress.com/forum/showthread.php?66803-Selection-refusing-to-unselect-the-superscript-button-(ctrl-shift-plus-sign)"
        Set Rng = Selection.Range
        With Rng
            .Collapse wdCollapseEnd
            .Hyperlinks.Add Anchor:=.Duplicate, TextToDisplay:=websiteName, Address:=Hyperlink
            .End = .End + 1
            .End = .Hyperlinks(1).Range.End
            .InsertAfter "]"
            .InsertBefore "["
            .Font.Size = 14
            .Font.Superscript = True
            .Collapse wdCollapseEnd
            .Text = " "
            .Font.Reset
            .Select
        End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    3
    Location
    Thank you for the helpful response.

    I was able to achieve improved results with the following:

        Dim fontSize As Integer
        Dim fontName As String
        
        fontSize = Selection.Font.Size
        fontName = Selection.Font.Name
    As noted in the next segment of code, .Hyperlink will use the default font setting, requiring the statement, .FontName = userSelectedFont
    I'm afraid this would be an issue if had numerous font settings, such as color and such.

        Dim Rng As Range: Set Rng = Selection.Range
        
        With Rng
            .Collapse wdCollapseEnd
            .Hyperlinks.Add Anchor:=.Duplicate, TextToDisplay:=websiteName, Address:=hyperLink
            .End = .End + 1
            .End = .Hyperlinks(1).Range.End
            .InsertAfter "]"
            .InsertBefore "["
            .Font.Size = 14
            .Font.Name = fontName           'Without this, default instead of selected font is used
            .Font.Superscript = True
            .Collapse wdCollapseEnd
            .Text = " "                     'Required, or superscript right bracket will be affected
            .Font.Superscript = False
            .Font.Size = fontSize
            .Select
        End With
        
        SendKeys "{Backspace}"
    If one leaves the cursor position, i.e. click anywhere else, then place their cursor back to where it was after the macro was executed, cursor will revert back to superscript notation. This is much more acceptable than a space between the 2 characters.

    Thank again for the Selection.Range suggestion, as it considerably reduced the lines of code in the script.

Posting Permissions

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