![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
Could you help me with a macro to replace the full name of elements by their respective symbol, in a long MS Word text. For example:
"Actinium" replaced by "Ac" "Aluminum" replaced by "Al" "Americium" replaced by "Am" "Antimony" replaced by "Sb" "Argon" replaced by "Ar" "Arsenic" replaced by "As" "Astatine" replaced by "At" "Barium" replaced by "Ba" Note: There is no " " in the text, before and after. Thank you |
#2
|
||||
|
||||
![]()
Try the following macro (which would be impossible to record):
Code:
Sub Demo() Application.ScreenUpdating = False Dim StrFnd As String, StrRep As String, i As Long StrFnd = StrFnd & "|Hydrogen|Helium|Lithium|Beryllium|Boron|Carbon|Nitrogen|Oxygen" & _ "|Fluorine|Neon|Sodium|Magnesium|Aluminium|Silicon|Phosphorus|Sulphur|Chlorine|Argon" & _ "|Potassium|Calcium|Scandium|Titanium|Vanadium|Chromium|Manganese|Iron|Cobalt|Nickel" & _ "|Copper|Zinc|Gallium|Germanium|Arsenic|Selenium|Bromine|Krypton|Rubidium|Strontium" & _ "|Yttrium|Zirconium|Niobium|Molybdenum|Technetium|Ruthenium|Rhodium|Palladium|Silver" & _ "|Cadmium|Indium|Tin|Antimony|Tellurium|Iodine|Xenon|Caesium|Barium|Lanthanum|Cerium" & _ "|Praseodymium|Neodymium|Promethium|Samarium|Europium|Gadolinium|Terbium|Dysprosium" & _ "|Holmium|Erbium|Thulium|Ytterbium|Lutetium|Hafnium|Tantalum|Tungsten|Rhenium|Osmium" & _ "|Iridium|Platinum|Gold|Mercury|Thallium|Lead|Bismuth|Polonium|Astatine|Radon|Francium" & _ "|Radium|Actinium|Thorium|Protactinium|Uranium|Neptunium|Plutonium|Americium|Curium" & _ "|Berkelium|Californium|Einsteinium|Fermium|Mendelevium|Nobelium|Lawrencium|Rutherfordium" & _ "|Dubnium|Seaborgium|Bohrium|Hassium|Meitnerium|Darmstadtium|Roentgenium|Copernicium" & _ "|Nihonium|Flerovium|Moscovium|Livermorium|Tennesine|Oganesson" StrRep = "|H|He|Li|Be|B|C|N|O|F|Ne|Na|Mg|Al|Si|P|S|Cl|Ar|K|Ca|Sc|Ti|V|Cr|Mn|Fe|Co|Ni|Cu|Zn" & _ "|Ga|Ge|As|Se|Br|Kr|Rb|Sr|Y|Zr|Nb|Mo|Tc|Ru|Rh|Pd|Ag|Cd|In|Sn|Sb|Te|I|Xe|Cs|Ba|La|Ce|Pr|Nd" & _ "|Pm|Sm|Eu|Gd|Tb|Dy|Ho|Er|Tm|Yb|Lu|Hf|Ta|W|Re|Os|Ir|Pt|Au|Hg|Tl|Pb|Bi|Po|At|Rn|Fr|Ra|Ac" & _ "|Th|Pa|U|Np|Pu|Am|Cm|Bk|Cf|Es|Fm|Md|No|Lr|Rf|Db|Sg|Bh|Hs|Mt|Ds|Rg|Cn|Nh|Fl|Mc|Lv|Ts|Og" With ActiveDocument.Range.Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Wrap = wdFindContinue .MatchCase = True .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False For i = 1 To UBound(Split(StrFnd, "|")) .Text = Split(StrFnd, "|")(i) .Replacement.Text = Split(StrRep, "|")(i) .Execute Replace:=wdReplaceAll Next End With Application.ScreenUpdating = True End Sub For Mac macro installation & usage instructions, see: https://wordmvp.com/Mac/InstallMacro.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you, Paul. It works fine. Would it be possible to add this condition to it: "only original words like Hydrogen, Helium, Lithium, etc., are to be changed".
Best, Bob |
#4
|
||||
|
||||
![]()
I don't know what you mean by that.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
In the text of my document, I would like to keep the full name of the elements--Hydrogen, Helium, Lithium, etc., but in the footnotes, I would like to use the abbreviations instead.
Your macro changes indiscriminately all full names for their corresponding abbreviation, whether they appear in the body of my document or in the footnotes. Best, Bob |
#6
|
||||
|
||||
![]()
There is nothing indiscriminate about the macro. Your request said nothing about footnotes, so the macro doesn't process them. If you wanted to process footnotes instead of the document body, you should have said so. To process footnotes, change:
With ActiveDocument.Range.Find to: With ActiveDocument.StoryRanges(wdFootnotesStory).Find
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
You're right. In my initial request, I had not realized that such request could have been included at once. Thank you again, Paul. You saved me hours of manual work, with the inherent mistake potential.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
ArchanRC | Word | 2 | 06-01-2018 03:51 PM |
![]() |
GEORGEJUNGCOCAINE | Word VBA | 2 | 02-27-2018 10:01 PM |
turn footnotes and their respective superscripts into normal unlinkable text | GEORGEJUNGCOCAINE | Word | 1 | 02-27-2018 05:47 PM |
How to adapt a text pasted from PDF to the full width of a microsoft word page | Alessio | Word | 2 | 01-13-2018 03:35 PM |
![]() |
spc94 | Word VBA | 3 | 06-25-2015 04:46 AM |