Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 07-17-2019, 09:13 PM
gmayor's Avatar
gmayor gmayor is offline Macro in Word to find specific words? Will be used for good cause (prostate cancer research) Windows 10 Macro in Word to find specific words? Will be used for good cause (prostate cancer research) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

It seems that fellow contributors have been busy while I slept. The original macro didn't work because the search criteria are not as you said. The problem with manuscripts of free text is that they don't always conform to standards so they might not always give the expected results, but based upon your sample I would suggest the following, again bearing in mind that I have not tested it on the Mac version of Word.

It works on the premise that you paste the text into a document and run the macro. The macro adds a table at the top of the page and if the terms are found, it adds them to the first column and highlights them in the text. It then attempts to find the required values associated with them and puts them in the adjacent columns.

As for my own treatment I was diagnosed and treated in an award winning oncology centre (one of the best in Europe) five years ago. All currently appears well though it took a long time to shake off the after effects of the hormone treatment that followed. I finished treatment two years ago. I see my oncologist every six months and I am still alive and well

Code:
Sub Macro1()
Dim vFindText As Variant
Dim oRng As Range
Dim oTable As Table
Dim oCell As Range
Dim oDoc As Document
Dim i As Long, j As Integer
    vFindText = Array("AUA", "SHIM", "PSA", "TURBT", "Frequency")
    Set oDoc = ActiveDocument
    oDoc.Range.InsertParagraphBefore
    Set oRng = oDoc.Range
    oRng.Collapse 1
    Set oTable = oDoc.Tables.Add(oRng, 5, 2)
    For i = 0 To 4
        Set oRng = oDoc.Range
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(FindText:=vFindText(i))
                Select Case i
                    Case 0, 1, 2
                        oRng.MoveEndUntil "0123456789"
                        oRng.Collapse 0
                        oRng.MoveEndWhile "0123456789/."
                        Set oCell = oTable.Cell(i + 1, 1).Range
                        oCell.End = oCell.End - 1
                        oCell.Text = vFindText(i)
                        Set oCell = oTable.Cell(i + 1, 2).Range
                        oCell.End = oCell.End - 1
                        oCell.Text = oRng.Text
                    Case Else
                        Set oCell = oTable.Cell(i + 1, 1).Range
                        oCell.End = oCell.End - 1
                        oCell.Text = vFindText(i)
                        Set oCell = oTable.Cell(i + 1, 2).Range
                        oCell.End = oCell.End - 1
                        oCell.Text = True
                End Select
            Loop
        End With
    Next i
lbl_Exit:
    Set oDoc = Nothing
    Set oRng = Nothing
    Set oTable = Nothing
    Set oCell = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Tags
keyword, macro, search



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Macro in Word to find specific words? Will be used for good cause (prostate cancer research) Macro to search for specific words in a document mike0215 Word VBA 2 11-28-2017 07:25 AM
VBA Word - Find & Apply Styles to Specific Words - Using Case Statement jc491 Word VBA 17 12-26-2015 12:25 PM
Macro in Word to find specific words? Will be used for good cause (prostate cancer research) Need VBA For Macro On How To Remove Specific Words netchie Word VBA 6 08-28-2012 03:37 PM
Macro in Word to find specific words? Will be used for good cause (prostate cancer research) Macro for highlighting specific number of words icsjohn Word VBA 2 12-07-2011 06:44 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:32 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft