Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    3 Star Lounger siflaar's Avatar
    Join Date
    Jan 2001
    Location
    Amersfoort, Netherlands
    Posts
    203
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Chemical Macro (97)

    Hello all,

    My brother is chemist and in his reports he often has to use a lot of chemical formulas like: C16H19N3O5S

    Although the numbers and letters of the formulas are different each time, they have a default format:
    1) Letters are caps
    2) Numbers are subscript

    Is there an macro that can set these formats for a selected formula, or can one of you write a macro that will do the trick?
    And is it also possible to activate that macro with the right mousebutton?

    Thanks in advance.Greetings,

    Patrick Schouten
    The Netherlands
    Greetings,

    Patrick Schouten
    (The Netherlands)

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Chemical Macro (97)

    Hi Patrick,

    Another fun challenge!
    The following was done using Word 2000, but I'm sure it would work with Word 97 as well:

    <pre>Public Sub FormatChemFormula()
    Dim SelRng As Range
    Dim Char As Range

    Set SelRng = Selection.Range
    For Each Char In SelRng.Characters
    If IsNumeric(Char.Text) Then
    Char.Font.Subscript = True
    Else
    Char.Case = wdUpperCase
    End If
    Next 'Char
    Set SelRng = Nothing
    End Sub
    </pre>

    If you'll excuse a very shorthand explanation of how to get it onto a right-click menu:

    First you need to determine where you are going to store the macro (Normal.dot, a workgroup template or a global template).
    Adding a macro to a shortcut menu is a customization so you also need to determine where you want to "context" that customization to.
    The simplest way to do both of the above is to put them in Normal.dot, although more experienced customizers prefer the alternatives.

    When you display the Customize dialog, click on the Toolbars tab, and you should see an entry for a toolbar called "Shortcut Menus" - put a checkmark next to this, and you should see a toolbar appear, that has three menus on it: Text, Table, and Draw. Click on the 'Text' menu and you'll see a whole lot of choices under it - each of these represents a context-sensitive shortcut menu that will appear when you're working in Word.
    Click on the menu item called "Text" and you will the popup menu. From this point, you can use the customize dialog to place a menu item for a macro, just in the same way that you would create a menu item for a macro on an ordinary toolbar.

    Pardon the sparse instructions; hopefully that is enough to get you started!

    Gary

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Chemical Macro (97)

    Gary, you just made a lot of chemists very happy; however, they will be quick to point out that Patrick
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  4. #4
    3 Star Lounger siflaar's Avatar
    Join Date
    Jan 2001
    Location
    Amersfoort, Netherlands
    Posts
    203
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Chemical Macro (97)

    Sam,

    The world is very lucky that I'm not a chemist.. <img src=/S/cauldron.gif border=0 alt=cauldron width=20 height=20>

    Thanks for the remark and the program additions
    I will send them to my brother to get it al correct.

    Greetings,

    PAtrick
    Greetings,

    Patrick Schouten
    (The Netherlands)

  5. #5
    3 Star Lounger siflaar's Avatar
    Join Date
    Jan 2001
    Location
    Amersfoort, Netherlands
    Posts
    203
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Chemical Macro (97)

    Hello Gary,

    Thanks for the quick and easy sollution.
    I will pass it thru to my brother

    Thanks again
    Greetings,

    Patrick Schouten
    (The Netherlands)

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Chemical Macro (97)

    There is a possibility that the user might have entered the text in upper case already, to cope with this it might be better to lowercase any text that you don't uppercase...

    <pre>Sub ChemFormula()
    Dim Char As Range
    Dim IsPreviousNumeric As Boolean
    IsPreviousNumeric = True
    Selection.Expand wdWord
    For Each Char In Selection.Range.Characters
    If IsNumeric(Char.Text) Then
    Char.Font.Subscript = True
    IsPreviousNumeric = True
    Else
    If IsPreviousNumeric Then
    Char.Case = wdUpperCase
    else
    Char.Case = wdLowerCase
    End if
    IsPreviousNumeric = False
    End If
    Next Char
    End Sub
    </pre>


    StuartR

    edited to fix a broken /PRE tag

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Chemical Macro (97)

    <font face="Comic Sans MS">No way, both Si and SI are valid. The author must type the correct case for the second and subsequent characters. si can be changed to Si, and I suppose you could modify the code to make sii into SiI. --Sam</font face=comic>
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  8. #8
    Star Lounger
    Join Date
    Mar 2002
    Location
    North Carolina, USA
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Chemical Macro (97)

    I found a similar principle in testing this macro to StuartR- I needed to turn off subscripting on the letters.
    The first time I ran the macro, it worked fine, from then on it wouldn't work, somehow the letters were acquiring the subscript attribute.
    So I added char.font.subscript= False to the Else section.

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Chemical Macro (97)

    Sam,

    Just getting back to this thread - the code you and Stuart posted improve on my first try. It does start to seem though, that any thorough macro like this would need to use some kind of lookup table (periodic table?<g>) since there are so many valid permutations - more than can be captured by a simple rule or two.

    Gary

  10. #10
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Chemical Macro (97)

    Sammy,

    To echo Gary's sentiments about a lookup table. I run a monthly Word Workshop at my Computer Club - the latest session was just last night. I was in the middle of preparing my material this past weekend when I saw your first posting and Stuart's addition.

    The topic for the Workshop was just little tips and tricks. I've been trying to introduce small VBA projects as part of this. Your formula macro was just perfect for this. So I snatched it up and dropped it into my material - with a big thanks to you right there.

    When I presented this, everyone really liked the macro. I think this helped show the benefit of writing VBA. Everyone knew you could subscript/cancel subscript, etc (or write a char style). But we all appreciated how laborious that would be. Perfect timing!

    We then debated how to enhance the character casing. Ended up agreeing on a lookup table. Alas we have no chemists in the room so no one was motivated to enhance this.

    Thanks.

    Fred

  11. #11
    3 Star Lounger siflaar's Avatar
    Join Date
    Jan 2001
    Location
    Amersfoort, Netherlands
    Posts
    203
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Chemical Macro (97)

    question, (btw I'm the chemist who asked my brother (siflaar) to ask you the question about the chemical formula marco. OK enough about that....

    What if the formula is silicium fluorine that is SiF??
    I will type sif and the macro turns it in Sif which is not correct??
    please respond.......
    Olaf.
    Greetings,

    Patrick Schouten
    (The Netherlands)

  12. #12
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Chemical Macro (97)

    Ah, a chemist, now we will get some correct specs. The simplest approach would be to only subscript all of the numbers and assume that the chemist entered the correct case for the letters, ie, don't type sif, because unless this program has a database of valid formulas, it cannot know if your compound starts with sulfur or silicon. Suppose you develop a compound of Unnilhexium, how will the algorithm handle Unh? Well, all of that should make you say "unh."

    So, I think that if you want to do some capitalization, my <!post=original algorithm,127000>original algorithm<!/post> is best:
    1) Capitalize the first letter
    2) Capitalize the first letter after a number
    3) Leave any other letter alone

    However, I can easily do whatever you want, so let me know.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  13. #13
    3 Star Lounger siflaar's Avatar
    Join Date
    Jan 2001
    Location
    Amersfoort, Netherlands
    Posts
    203
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Re: Chemical Macro (97)

    Hello all,

    I was just thinking (may I should do sometine else but...)
    Is it not so that the letters combinations of ONE element could only be 1 or 2 characters, that means that if there is a third character this one should always be caps?
    Greetings,

    Patrick Schouten
    (The Netherlands)

  14. #14
    Star Lounger
    Join Date
    Mar 2001
    Location
    New York, USA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Chemical Macro (97)

    Don't forget that there maybe waters added to the formula. For example AlCl3.6(H2O). Since I have seen waters added using *, - or . I developed the following to be used in Excel. You might modify it for Word. Did this long ago, but I think it will still work. HTH

    Sub subscript()

    Dim isnum(30) As Integer
    Dim txt As String
    Dim Ascnum As Integer

    txt = ActiveWindow.ActiveCell
    For i = 2 To Len(txt) 'first character will not be subscript
    'Is character numeric?
    Ascnum = Asc(Mid(txt, i, 1))
    If Ascnum > 47 And Ascnum < 58 Then 'between 0 and 9
    isnum(i) = 1
    Else
    If Ascnum = 42 Or Ascnum = 45 Or Ascnum = 46 Then 'is a *, -, .
    isnum(i) = 0
    i = i + 1
    isnum(i) = 0
    Else
    isnum(i) = 0
    End If
    End If
    Next i
    'Change numbers to subscript
    For i = 1 To Len(txt)
    If isnum(i) = 1 Then
    ActiveCell.Characters(i, 1).Font.subscript = True
    End If
    Next i

    End Sub

  15. #15
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Chemical Macro (97)

    Unh, you flunk Chemistry. BTW, that was a pun: just to confuse us, unh is Unnilhexium and we also now have:

    <table border=1><td>#</td><td>Symbol</td><td>IUPAC</td><td>Proposed name<td><td> </td><td>104</td><td>Unq</td><td>Unnilquadium</td><td>Dubnium</td><td>Db</td><td>105</td><td>Unp</td><td>Unnilpentium</td><td>Joliotium</td><td>Jl</td><td>106</td><td>Unh</td><td>Unnilhexium</td><td>Rutherfordium</td><td>Rf</td><td>107</td><td>Uns</td><td>Unnilseptium</td><td>Bohrium</td><td>Bh</td><td>108</td><td>Uno</td><td>Unniloctium</td><td>Hahnium</td><td>Hn</td><td>109</td><td>Une</td><td>Unnilennium</td><td>Meitnerium</td><td>Mt</td><tr></table>

    So, if you thought my ranting about SI vs Si was bad, just wait for Unh vs UNH
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Page 1 of 2 12 LastLast

Posting Permissions

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