• Chemical Macro (97)

    Author
    Topic
    #368573

    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

    Viewing 2 reply threads
    Author
    Replies
    • #577959

      Hi Patrick,

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

      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
      

      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

      • #577993

        Hello Gary,

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

        Thanks again

    • #577991

      Gary, you just made a lot of chemists very happy; however, they will be quick to point out that Patrick

      • #577995

        Sam,

        The world is very lucky that I’m not a chemist.. cauldron

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

        Greetings,

        PAtrick

      • #578039

        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…

        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
        

        StuartR

        edited to fix a broken /PRE tag

        • #578048

          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

          • #578391

            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?) since there are so many valid permutations – more than can be captured by a simple rule or two.

            Gary

          • #578589

            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

          • #579233

            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.

            • #579244

              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 original algorithm 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.

            • #579248

              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?

            • #579278

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

              # Symbol IUPAC Proposed name
              104 Unq Unnilquadium Dubnium Db
              105 Unp Unnilpentium Joliotium Jl
              106 Unh Unnilhexium Rutherfordium Rf
              107 Uns Unnilseptium Bohrium Bh
              108 Uno Unniloctium Hahnium Hn
              109 Une Unnilennium Meitnerium Mt

              So, if you thought my ranting about SI vs Si was bad, just wait for Unh vs UNH

            • #579296

              Hi Sam,

              Sorry for the confusion, on my name Silfaar where two different replys, most from my, but one from my brother (the chemist) Olaf. He will not use my name anymore.

              He told me, that for his problem the macro will work by setting caps on the third letter, because he is not using the unnamed elements. But you are right about those elements.

              Well, lets try an other sollution, Put a space between every element so the macro knows where a element name starts and ends and let the macro remove the space.

            • #579532


              > Sorry for the confusion, on my name

              No confusion, you both identify yourself in the message. It is fine for Olaf to use your screen name.


              > the macro will work by setting caps on the third letter

              Not true, pick something hard like Nickel Carbonate, xNiCO3.yNi(OH)2.zH2O It’s easy to know when to subscript, but impossible to know when to capitalize.


              > Put a space between every element

              Isn’t this more confusing than just requiring the chemist to do the capitalization correctly?

            • #579854

              Hello Sam,

              I see your point here. You are right.

        • #578067

          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.

    • #579264

      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

    Viewing 2 reply threads
    Reply To: Chemical Macro (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: