#1
|
|||
|
|||
Acronym Finder
I have made use of the code listed in this thread: https://www.msofficeforums.com/word-...soft-word.html
and it works quite well. My interest is to not use a table that is part of the current document but instead I want to make use of an existing, stand-alone, Glossary that gets editted. The existing glossary is a word document (.docx) of our master list of acronyms with definitions (delimited with tab stops). Is it possible to perform the same type of checking for new and deletions of non-existent acronyms using a word document versus the table? Your assistance is greatly appreciated as I am a complete novice to macros. |
#2
|
||||
|
||||
Acronym Finder
While it's easy enough to use one document for the acronyms and to match them against one or more other documents, I don't really see the point of having the code delete entries from the acronym document just because terms found in one test document aren't found in another. If you process multiple documents, all that might lead to is a lot of churning (the same content repeatedly being deleted/added).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Acronym Finder
Perhaps if I let you know what I did with the code it may make it more clear as to what my intent is. I created a table of our ~850 acronyms and then inserted it at the beginning of the document I wanted to check. I ran the macro and it prompted me to add any acronyms that were not currently in the table. It then went thru and deleted all the acronyms that were listed in the table but not present in the document. This left me with a table of acronyms (that applied to the specific document) and included the newly added acronyms (that were not part of the master list of acronyms).
The table then needs to be imported to a new word document that is formatted as an Appendix to our base documents. I was hoping that I can use the master glossary Appendix and have it do the macro perform the same comparisons/edits on it as it does on the table. Thanks again. |
#4
|
||||
|
||||
Acronym Finder
In that case, it seems to me the real issue is that you want to avoid having to copy the master list into your main document, run the macro, then cut & paste the result into the Appendix document. Instead, it seems you want to run the macro from another document, say, the master list document (which would have to be a .doc or .docm file, not a .docx file), but with the result being saved to either a new document (which becomes the Appendix document) or to an existing Appendix document.
Presumably, you wouldn't want to macro to change your master list? Obviously, the process for creating a new Appendix document would be different from the process for adding the table to an existing Appendix document, so you'd need to decide which of those approaches you want to take.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Acronym Finder
Yes. I would like to avoid copying and pasting anything into the main document or making any modifications to it at all.
I want to run the macro from another document, say, the master list document (which would have to be a .doc or .docm file, not a .docx file), with the result being saved a new document. |
#6
|
||||
|
||||
Acronym Finder
Try:
Code:
Sub CreateAcronymAppendixDocument() Application.ScreenUpdating = False Dim DocAcro As Document, DocSrc As Document, DocTgt As Document Dim oTbl As Table, i As Long, j As Long Dim strAcr As String, strFnd As String, strDef As String strAcr = "," Set DocAcro = ThisDocument With Dialogs(wdDialogFileOpen) If .Show = False Then Exit Sub End With Set DocSrc = ActiveDocument Set DocTgt = Documents.Add(DocSrc.AttachedTemplate.FullName) DocTgt.Range.FormattedText = DocAcro.Range.FormattedText With DocTgt Set oTbl = .Tables(1) For i = 2 To oTbl.Rows.Count With oTbl.Cell(i, 2).Range strFnd = Left(.Text, Len(.Text) - 2) strAcr = strAcr & strFnd & "," End With Next End With With DocSrc.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Text = "\([A-Z][A-Za-z0-9]@\)" .MatchWildcards = True .Execute End With Do While .Find.Found .Start = .Start + 1 .End = .End - 1 strFnd = .Text If InStr(strAcr, "," & strFnd & ",") = 0 Then strAcr = strAcr & strFnd & "," strDef = Trim(InputBox("New Term Found: " & strFnd & vbCr & _ "Add to definitions?" & vbCr & _ "If yes, type the definition.")) If strDef <> vbNullString Then With oTbl.Rows .Add .Last.Cells(1).Range.Text = strDef .Last.Cells(2).Range.Text = strFnd End With End If End If .Collapse wdCollapseEnd .Find.Execute Loop End With For i = oTbl.Rows.Count To 2 Step -1 With oTbl.Cell(i, 2).Range strFnd = Left(.Text, Len(.Text) - 2) End With With DocSrc.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Text = strFnd .MatchWholeWord = True .MatchWildcards = False .MatchCase = True .Execute End With j = 0 Do While .Find.Found j = j + 1 .Collapse wdCollapseEnd .Find.Execute Loop End With If j = 0 Then oTbl.Rows(i).Delete Else oTbl.Cell(i, 3).Range.Text = j End If Next Set oTbl = Nothing: Set DocAcro = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Acronym Finder
I tried the code as listed above and found it to perform as follows:
It prompts me to chose the main document. I select the location and file and the macro begins processing the data It finds all acronyms that are not listed in the Acronym document. It appears to check all the acronyms that are not used within the main document BUT fails to delete them from the table (it actually lists them as occurring 1 time in the document) It also does not close out the main document when it is finished. It also does not permit you to close it out manually. I have to use the task manager to get it to close out. <-- Disregard. Last edited by Cray_Z; 09-21-2014 at 10:49 PM. Reason: Updated status |
#8
|
||||
|
||||
Acronym Finder
The code doesn't change your original acronym document - it creates a new one with a copy of the acronym table, then proceeds to update that.
The macro leaves all documents open, because: (a) I didn't assume you'd want to close the document containing the macro (e.g. you might want to run it on another document); (b) I assumed you'd want to review/edit the output document; and (c) the source document may already have been open, in which case you would probably want it to remain that way. Extra code could have been added to close the source document had it already been closed but leaving it open allows you to review the content in light of the macro's processing. You shouldn't need to use the Task Manager to access any of these documents once processing has concluded.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Acronym Finder
Understood. Your help has been truly invaluable. I have spent the last 8 hours of my day working on this task and I spent a good part of my weekend trying to get it working. Again, thank you for putting up with me.
|
#10
|
|||
|
|||
Acronym Finder
I know I have used more than my allotted questions but I was wondering if you could tell me how to run this script automatically when I open the document. I have tried using
Private Sub Document_Open() and also tried creating a macro named AutoOpen but have had no luck in getting it to run properly. I get an error associated with line #15 that reads: Set oTbl = .Tables(1) Any ideas? Run-time error "5941" The requested member of the collection does not exist. |
#11
|
||||
|
||||
Acronym Finder
You should be able to do that by changing:
Sub CreateAcronymAppendixDocument() to: Sub Document_Open() and putting the macro in the Acronym document's own 'ThisDocument' code module. Your error messages suggest you might have been adding the macro to Word's Normal template.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Acronym Finder
I would like to close out the DocSrc and the DocAcro when the macro has completed and just leave the newly created acronym list document open. I tried using the following:
DocAcro.Close Savechanges:=wdDoNotSaveChanges DocSrc.Close Savechanges:=wdDoNotSaveChanges But am not sure this is correct syntax. Can you shed some light on this for me? I have revised my code to include: With DocSrc currDoc = .FullName .Close SaveChanges:=wdDoNotSaveChanges End With With DocAcro currDoc = .FullName .Close SaveChanges:=wdDoNotSaveChanges End With And it appears to be working. Can you tell me if this is the proper (efficient) way to accomplish this? Last edited by Cray_Z; 09-22-2014 at 10:30 PM. Reason: Revised code |
#13
|
||||
|
||||
Acronym Finder
Yes, that syntax is OK, but you need to have those lines before:
Set oTbl = Nothing: Set DocAcro = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing as in, for example: DocAcro.Close Savechanges:=wdDoNotSaveChanges DocSrc.Close Savechanges:=wdDoNotSaveChanges Set oTbl = Nothing: Set DocAcro = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing You could also reduce these to: DocAcro.Close False: DocSrc.Close False Set oTbl = Nothing: Set DocAcro = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Acronym Finder
I had those lines so it looked like: DocAcro.Close Savechanges:=wdDoNotSaveChanges
DocSrc.Close Savechanges:=wdDoNotSaveChanges Set oTbl = Nothing: Set DocAcro = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing And I also tried: DocAcro.Close False: DocSrc.Close False (I tried True as well) Set oTbl = Nothing: Set DocAcro = Nothing: Set DocSrc = Nothing: Set DocTgt = Nothing But for some reason it leaves one or both of the documents open and I am unable to close them out with the X in the upper right hand corner. I end up going to the task bar and right clicking the Word icon and then clicking "close all windows." Only then do the windows close properly. I am using the code: With DocSrc currDoc = .FullName .Close SaveChanges:=wdDoNotSaveChanges End With With DocAcro currDoc = .FullName .Close SaveChanges:=wdDoNotSaveChanges End With And it closes out both documents for me. Just wondering if you have any idea why the two sample codes above do not work and yet the last one works? I prefer to have the code as efficient as possible but don't understand why one works and one doesn't. |
#15
|
||||
|
||||
Acronym Finder
Where does currDoc come from? In any event, it has nothing to do with how you're closing the documents, since you're not actually using it for anything. Thus:
Code:
With DocSrc currDoc = .FullName .Close SaveChanges:=wdDoNotSaveChanges End With With DocAcro currDoc = .FullName .Close SaveChanges:=wdDoNotSaveChanges End With Code:
With DocSrc .Close SaveChanges:=wdDoNotSaveChanges End With With DocAcro .Close SaveChanges:=wdDoNotSaveChanges End With Code:
DocSrc.Close SaveChanges:=wdDoNotSaveChanges DocAcro.Close SaveChanges:=wdDoNotSaveChanges Code:
DocAcro.Close False: DocSrc.Close False
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|