Super/SubScript problems

M

MikeM

Being a scientist, I use a lot of symbols repetitively. Over time, I've
either created Macros with a dialog box or run by an individual keystroke.
They all work fine except for the ones that involve super/subscripts. For
example, I want to put in the text'Mg2+' where the 2+ is superscripted. The
macro is as follows:
Public Sub MAIN()
WordBasic.Insert "Mg"
WordBasic.Superscript
WordBasic.Insert "2+"
WordBasic.Superscript
WordBasic.Insert " "
End Sub
The last 2 lines supposedly would remove the superscript and then insert a
space. If the insertion point is at the end of a line, with only spaces
following, OR if it's between 2 spaces, this works fine. However, if there
is no space following, everything ends of superscripted, not only the entire
Mg2+, but the following word.
That is, insertion point is next to the 't' of transporter. If I insert the
Mg2+, instead of Mg/super/2+/endsuper/space'transporter', I get 'Mg2+
transporter' with every single character superscripted. Happens in other
similar macros whether super or subscripted. Does the same thing whether the
macro inserts a space at the end or not. I'm sure it's my ignorance of
Visual Basic, since all my macros are just edits of some generic ones, but
what am I doing wrong. Thanks
 
J

Jean-Guy Marcil

MikeM said:
Being a scientist, I use a lot of symbols repetitively. Over time, I've
either created Macros with a dialog box or run by an individual keystroke.
They all work fine except for the ones that involve super/subscripts. For
example, I want to put in the text'Mg2+' where the 2+ is superscripted. The
macro is as follows:
Public Sub MAIN()
WordBasic.Insert "Mg"
WordBasic.Superscript
WordBasic.Insert "2+"
WordBasic.Superscript
WordBasic.Insert " "
End Sub
The last 2 lines supposedly would remove the superscript and then insert a
space. If the insertion point is at the end of a line, with only spaces
following, OR if it's between 2 spaces, this works fine. However, if there
is no space following, everything ends of superscripted, not only the entire
Mg2+, but the following word.
That is, insertion point is next to the 't' of transporter. If I insert the
Mg2+, instead of Mg/super/2+/endsuper/space'transporter', I get 'Mg2+
transporter' with every single character superscripted. Happens in other
similar macros whether super or subscripted. Does the same thing whether the
macro inserts a space at the end or not. I'm sure it's my ignorance of
Visual Basic, since all my macros are just edits of some generic ones, but
what am I doing wrong. Thanks

WordBasic is quite old... use a Range object instead:

Dim rngInsert As Range

'Collpase in case selection is not an insertion point
Selection.Collapse wdCollapseStart

Set rngInsert = Selection.Range

With rngInsert
.InsertAfter "Mg"
.Collapse wdCollapseEnd
.InsertAfter "2+"
.Font.Superscript = True
.Collapse wdCollapseEnd
.InsertAfter " "
.Font.Superscript = False
.Collapse wdCollapseEnd
.Select
End With

If you have lots of these (Normal characters followed by some
super(sub)script ones), instead of rewrting the whole thing over and over,
call a sub like this:


'_______________________________
Option Explicit

'_______________________________
Sub MG2PlusSuper()

InsertSuper "MG", "2+"

End Sub
'_______________________________

'_______________________________
Sub MG2PlusSub()

InsertSub "MG", "2+"

End Sub
'_______________________________

'_______________________________
Sub TextSuper()

InsertSuper "Some Text", "SUPERSCRIPT"

End Sub
'_______________________________

'_______________________________
Sub TextSub()

InsertSub "Some Text", "SUBSCRIPT"

End Sub
'_______________________________

'_______________________________
Private Sub InsertSuper(strNormal As String, strSuper As String)

Dim rngInsert As Range

Selection.Collapse wdCollapseStart

Set rngInsert = Selection.Range

With rngInsert
.InsertAfter strNormal
.Collapse wdCollapseEnd
.InsertAfter strSuper
.Font.Superscript = True
.Collapse wdCollapseEnd
.InsertAfter " "
.Font.Superscript = False
.Collapse wdCollapseEnd
.Select
End With

End Sub
'_______________________________

'_______________________________
Private Sub InsertSub(strNormal As String, strSub As String)

Dim rngInsert As Range

Selection.Collapse wdCollapseStart

Set rngInsert = Selection.Range

With rngInsert
.InsertAfter strNormal
.Collapse wdCollapseEnd
.InsertAfter strSub
.Font.Subscript = True
.Collapse wdCollapseEnd
.InsertAfter " "
.Font.Subscript = False
.Collapse wdCollapseEnd
.Select
End With

End Sub
'_______________________________
 
M

MikeM

I think I got the gist, but you're talking to a guy whose first and pretty
much only programming language was Fortran (a LONG time ago) and who didn't
do well in French or German either. I'll work on it with a couple of
examples and see what happens. These are the only ones that don't work,
although some of the others are (comparatively) more complicated. Likely
I'll miss something and have more questions later.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top