PDA

View Full Version : [SOLVED:] Selection refusing to 'unselect' the superscript button (ctrl shift plus-sign)



donedown
02-14-2020, 01:52 PM
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

gmayor
02-14-2020, 10:10 PM
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.

macropod
02-14-2020, 10:20 PM
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

donedown
02-15-2020, 11:51 AM
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.

macropod
02-15-2020, 01:09 PM
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.


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.


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

donedown
02-16-2020, 09:38 AM
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.