Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2014, 06:56 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 09-21-2014, 07:11 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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]
Reply With Quote
  #3  
Old 09-21-2014, 07:18 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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.
Reply With Quote
  #4  
Old 09-21-2014, 07:40 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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]
Reply With Quote
  #5  
Old 09-21-2014, 08:50 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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.
Reply With Quote
  #6  
Old 09-21-2014, 09:25 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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]
Reply With Quote
  #7  
Old 09-21-2014, 10:23 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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
Reply With Quote
  #8  
Old 09-21-2014, 10:38 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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]
Reply With Quote
  #9  
Old 09-21-2014, 11:36 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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.
Reply With Quote
  #10  
Old 09-22-2014, 06:47 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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.
Reply With Quote
  #11  
Old 09-22-2014, 08:18 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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]
Reply With Quote
  #12  
Old 09-22-2014, 10:03 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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
Reply With Quote
  #13  
Old 09-22-2014, 10:21 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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]
Reply With Quote
  #14  
Old 09-22-2014, 10:49 PM
Cray_Z Cray_Z is offline Acronym Finder Windows 7 32bit Acronym Finder Office 2007
Novice
Acronym Finder
 
Join Date: Sep 2014
Posts: 16
Cray_Z is on a distinguished road
Default 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.
Reply With Quote
  #15  
Old 09-22-2014, 11:42 PM
macropod's Avatar
macropod macropod is offline Acronym Finder Windows 7 64bit Acronym Finder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default 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
is functionally equivalent to:
Code:
With DocSrc
.Close SaveChanges:=wdDoNotSaveChanges
End With
With DocAcro
.Close SaveChanges:=wdDoNotSaveChanges
End With
which is functionally equivalent to:
Code:
DocSrc.Close SaveChanges:=wdDoNotSaveChanges
DocAcro.Close SaveChanges:=wdDoNotSaveChanges
which is functionally equivalent to:
Code:
DocAcro.Close False: DocSrc.Close False
which is what I suggested in post #13...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 10:12 PM.


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