Results 1 to 15 of 15
  1. #1
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    macro comparing two glossaries (2002)

    I've been given an old textbook glossary that we want to update with terms from our new dictionary (which contains combined, updated terms from all of our textbooks). I tried merging the two documents, but the results were quite messy. I recorded a macro to do the job, but it doesn't work. There are three problems (that I can see) with the macro.

    First, when I recorded the macro, I pasted the contents of the clipboard into the Find What box. But when I opened the macro, it had the term of the first word I searched for. I changed that to Selection.Paste, which seemed like it would have worked. It didn't. I thought of using ^c and selecting Use Wildcards, but the old glossary terms are all caps, and the dictionary are only capped where appropriate. (I just tried it anyway, and it gave a runtime error.)

    Second, I'm not sure what to do about terms that aren't in the master dictionary. What I would like to happen is for the term that's not in newglossary.doc to be pasted into it. Instead, it goes to the next paragraph and deletes the next character.

    Third, I don't know how to make it loop so that it runs the macro on the entire file. This function would save me minutes, as opposed to the rest of the puzzle, which will save me hours. Still, it's something that I could probably use every time we update a textbook in the future.

    I'd attach the documents, but the master dictionary is quite large. If it would help, I could create a new file with just the first several pages. The macro I recorded is below.

    ' NewSMA3Glossary Macro
    ' Macro recorded 9/23/2004 by Michael Coleman
    '
    Selection.MoveDown Unit:=wdParagraph, Count:=1
    Selection.Extend
    Selection.Extend Character:="."
    Selection.Copy
    Selection.EscapeKey
    Selection.HomeKey Unit:=wdLine
    Windows("newglossary").Activate
    Selection.Extend
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "Selection.Paste"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    Application.Run MacroName:="TemplateProject.DeleteCharRight.MAIN"
    Selection.MoveDown Unit:=wdParagraph, Count:=1
    Windows("oldglossary").Activate
    End Sub

    I don't know if I'm asking one macro to do too much. I'm pretty certain it can work, though. If someone has the inclination to take up this puzzle, I would be, as always, very grateful.

    Thank you,
    Michael Coleman
    BOMI International

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    Hi Michael,

    Rather than try to debug your current code, could you describe exactly what the macro needs to do? A small sample file would help, too.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    Hi Michael,

    I'm posting my response to the board so others can benefit.

    Michael sent me two files, which matched the descriptions provided below (Michael, you should post those files if possible, though I didn't want to make that decision for you.)

    <hr>
    Here's what's happening now. The dictionary I've been working on is going into production next week, and already it's becoming a useful tool. We're now using it to check glossaries of textbooks being updated. What I'd like to do is:

    * Go through the dictionary file (saved as newglossary.doc).
    * Delete everything but the terms listed in the oldglossary.doc that are defined in the dictionary.
    * Add any terms in oldglossary.doc that are not currently in newglossary.doc (with the definition would be idea, but not crucial).
    * See what terms have been added to newglossary.doc as a result. (I was thinking running the macros with Track Changes turned on would do it.)

    The immediate use for it will be to update a textbook glossary based on the terms and definitions in our dictionary. I think we'd also be able to use it to create a textbook glossary from scratch by using a list of terms rather than the old glossary file. An editor could mark words to be included, then create a list of all the word (unless it's me, in which case I'll create a list as I edit electronically). Then we'll run the macro against that list. It'll show us what words are already defined, and which ones we need to work on (and add to the next edition of the dictionary).

    The attached files are snipets--terms beginning with "a" to be specific. (The actual files are roughly 26 times larger.) Here are some characteristics of each document.

    IN OLDGLOSSARY.DOC:

    * Terms are not formatted consistently. Sometimes terms are in all caps, sometimes not.
    * Each term is followed by a period and then the definition, and each term/definition is contained in a single paragraph.
    * We may add more terms without definitions, or we may want to run the macro only on a list of terms pulled from the revised book. (These uses would be for future purposes, not the current project.)

    IN NEWGLOSSARY.DOC, Everything is consistent:

    * Terms are lowercase unless there's a reason to capitalize.
    * Terms are bold, followed by a period.
    * Terms are formatted with the paragraph style "definition".
    * Some terms have one or more additional paragraphs under them formatted either as "subdefinition" or "formula".
    <hr>

    The code is attached. It adds the new terms at the end of the document, as you'd need to supply the definitions and any formatting. At that point, you could always do a Sort to get things in their proper place.

    The files you sent were small, so it's hard to say how long this would take on large files, but it should be fairly fast.

    Hope this gets you moving in the right direction!
    Attached Files Attached Files

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    I just noticed your parenthetical that you wanted to copy the definition with the term from the old glossary. Here's a modified version:
    <pre>Sub AddTermsToGlossary2()
    Dim docOld As Document
    Dim docNew As Document
    Dim para As Paragraph
    Dim rng As Range
    Dim k As Integer
    Dim sngTermCount As Single
    Dim sTerms() As String
    Dim sTerm As String

    Set docOld = Documents("oldglossary.doc")
    Set docNew = Documents("newglossary.doc")

    ' See how big of an array we'll need.
    ' This is just a rough estimate, since
    ' there may be blank paragraphs
    sngTermCount = docOld.Paragraphs.Count

    ' Set up the array to hold the terms
    ReDim sTerms(1 To sngTermCount) As String
    k = 0

    For Each para In docOld.Paragraphs
    ' Ignore any blank paragraphs
    If para.Range.Characters.Count > 1 Then
    k = k + 1
    ' Get text of paragraph, minus
    ' trailing paragraph mark
    Set rng = para.Range
    rng.MoveEnd unit:=wdCharacter, Count:=-1
    sTerms(k) = Trim(rng.Text)
    End If
    Next para

    ' Since we may have ignored some blank
    ' paragraphs, readjust term count to match
    ' number of items actually added to array
    sngTermCount = k
    ReDim Preserve sTerms(1 To sngTermCount)

    ' Now go through the list, and find
    ' any paragraphs in the new glossary
    ' that start with the term, ignoring
    ' case and format
    For k = 1 To sngTermCount
    With docNew.Content.Find
    .ClearFormatting
    .Text = "^p" & sTerms(k)
    .MatchWholeWord = False
    .MatchCase = False
    .MatchWildcards = False
    .Forward = True
    .Format = False
    .Execute

    ' If the term isn't in the new glossary,
    ' then add it to the end.
    If .Found = False Then
    docNew.Range.InsertAfter vbCr & sTerms(k)
    End If
    End With
    Next k
    End Sub
    </pre>


  5. #5
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    I ran the second macro. It added the term from oldglossary to newglossary that wasn't in there to begin with, but it also added most of the terms that were already in the new glossary.

    The first macro worked in reverse. It deleted terms from the old file, not the new one. What I was hoping for was to take a copy of our master dictionary, save it as newglossary.doc, and then delete all but the relevant terms from that file. If I was unclear before, I'm very, very sorry.

    I've attached oldglossary.doc. I'll send the other file right away. (I don't see how to post two files in the same message.) Andrew, I also added a term to the old glossary to make sure there was at least one term that is not in the new glossary.

    Thank you,
    Attached Files Attached Files
    Michael Coleman
    BOMI International

  6. #6
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    Here's the other file. I deleted several pages to get it under the 100k max.
    Attached Files Attached Files
    Michael Coleman
    BOMI International

  7. #7
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    I'm still unclear about what you want. You want the words that appear in the oldglossary file but aren't yet in the new one to be added?

    Forget about how or why you want this done, just focus on what. If there's a term in the oldglossary that's not in the new one, what should happen? If there's a term in the newglossary that's not in the old one, what should happen?

  8. #8
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    I'm really sorry about the lack of clarity, and I really appreciate you sticking with me.

    I want to take our master dictionary file (which I've saved as newglossary.doc) and filter out everything except what is in the glossary for a specific book (oldglossary.doc) so that we have a file with just those terms but with the more up-to-date definitions.

    I also want to identify any terms that are in the old glossary that have not yet been defined in the new one.
    Michael Coleman
    BOMI International

  9. #9
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    Ah, yes, that's much clearer now.

    I don't have time to make this any "prettier", but it works and it's fast:

    <pre>Sub AddTermsToGlossary()
    Dim iTERM As Integer
    Dim iTERM_AND_DEF As Integer

    Dim docOld As Document
    Dim docNew As Document
    Dim para As Paragraph
    Dim rng As Range
    Dim k As Integer
    Dim sngTermCount As Single
    Dim sTerms() As String
    Dim sTerm As String
    Dim rngToSearch As Range
    Dim rngResult As Range
    iTERM = 1
    iTERM_AND_DEF = 2

    Set docOld = Documents("oldglossary.doc")
    Set docNew = Documents("newglossary.doc")

    ' See how big of an array we'll need.
    ' This is just a rough estimate, since
    ' there may be blank paragraphs
    sngTermCount = docNew.Paragraphs.Count

    ' Set up the array to hold the terms
    ReDim sTerms(iTERM To iTERM_AND_DEF, _
    1 To sngTermCount) As String
    k = 0
    For Each para In docNew.Paragraphs
    ' Ignore any blank paragraphs
    If para.Range.Characters.Count > 1 Then
    ' If there's a period in the paragraph,
    ' get the term (what's before the first period)
    ' and then get everything
    If InStr(para.Range.Text, ".") <> 0 Then
    k = k + 1
    Set rng = para.Range
    rng.Collapse wdCollapseStart
    rng.MoveEndUntil cset:="."
    sTerms(iTERM, k) = Trim(rng.Text)
    Set rng = para.Range
    rng.MoveEnd unit:=wdCharacter, Count:=-1
    sTerms(iTERM_AND_DEF, k) = Trim(rng.Text)
    End If
    End If
    Next para

    ' Re-adjust to eliminate any empty elements
    sngTermCount = k
    ReDim Preserve sTerms(iTERM To iTERM_AND_DEF, _
    1 To sngTermCount)

    ' Now cycle through list to find matches
    For k = 1 To sngTermCount
    Set rngToSearch = docOld.Range
    docOld.TrackRevisions = True

    Set rngResult = rngToSearch.Duplicate

    Do
    ' Find the term at the beginning of a paragraph
    ' followed by a period
    With rngResult.Find
    .ClearFormatting
    .Text = "^p" & sTerms(iTERM, k) & "."
    .Forward = True
    .MatchWildcards = False
    .MatchCase = False
    .Wrap = wdFindStop
    .Execute
    End With

    If rngResult.Find.Found = False Then
    Exit Do
    End If
    With rngResult
    ' If found, replace entire paragraph
    ' with more current definition
    Set rng = .Paragraphs(2).Range
    rng.MoveEnd unit:=wdCharacter, Count:=-1
    rng.Text = sTerms(iTERM_AND_DEF, k)
    .MoveStart wdWord
    .End = rngToSearch.End
    End With
    Loop Until rngResult.Find.Found = False

    Next k
    End Sub
    </pre>


    If you wanted to preservere the formatting from the newglossary file, that would require a different approach.

    HTH

  10. #10
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    I got an error message on the following line:

    If InStr(para.Range.Text, ".") <> 0 Then
    Michael Coleman
    BOMI International

  11. #11
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    What did the error message say? What's the value of para.Range.Text when the error happens? (Hover your mouse over a variable while in break mode to get its value)

  12. #12
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    Is it a compilation error (does it trigger when you do Debug->Compile)? Or does it only happen when you run the macro?

    It ran fine on my machine on the two documents you posted.

  13. #13
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    It says Compile error: Syntax error. When I hovered the mouse over it, it didn't give any value.
    Michael Coleman
    BOMI International

  14. #14
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    I suspect it's an errant linebreak or some other cruft from cutting and pasting from the web. Try replacing with the attached instead. If you still get the error, please post a screenshot (Ctrl-PrntScreen, then paste into Paint).
    Attached Files Attached Files

  15. #15
    Star Lounger
    Join Date
    Feb 2003
    Location
    Annapolis, Maryland, USA
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: macro comparing two glossaries (2002)

    The open angle bracket, <, had converted to lt; or something like that. I guess that's the ASCII code. When I posted the line, it converted back. Anyway, I fixed the line and the whole thing works beautifully. Once again, I can't thank you enough!
    Michael Coleman
    BOMI International

Posting Permissions

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