From 2c536737a472cd0ef4c081882b3128d8be6879ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Gonz=C3=A1lez=20Garc=C3=ADa?= Date: Wed, 11 Jan 2017 17:52:10 +0100 Subject: [PATCH] Improved version of GAUG_removeHyperlinksForCitations GAUG_removeHyperlinksForCitations can now be executed in three different ways depending on a parameter. The parameter strTypeOfExecution can have one of three different values: "RemoveHyperlinks": UNEXPECTED RESULTS IF MANUAL MODIFICATIONS EXIST, BUT THE FASTEST. Removes the bookmarks and hyperlinks. Manual modifications to citations and bibliography will remain intact. "CleanEnvironment": EXPERIMENTAL, BUT VERY FAST. Removes the bookmarks and hyperlinks. Manual modifications to citations and bibliography will also be removed to have a clean environment "CleanFullEnvironment": SAFE, BUT VERY SLOW IN LONG DOCUMENTS. Removes the bookmarks and hyperlinks. Manual modifications to citations and bibliography will also be removed to have a clean environment. --- macros.vba | 161 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 148 insertions(+), 13 deletions(-) diff --git a/macros.vba b/macros.vba index f5f9ef7..114c3ce 100644 --- a/macros.vba +++ b/macros.vba @@ -1,7 +1,7 @@ '***************************************************************************************** '***************************************************************************************** '** Author: José Luis González García ** -'** Last modified: 2016-12-28 ** +'** Last modified: 2017-01-11 ** '** ** '** Sub GAUG_createHyperlinksForCitationsAPA() ** '** ** @@ -26,7 +26,9 @@ Sub GAUG_createHyperlinksForCitationsAPA() '***************************** '* Cleaning old hyperlinks * '***************************** - GAUG_removeHyperlinksForCitations + 'possible values are "RemoveHyperlinks", "CleanEnvironment" and "CleanFullEnvironment" + 'SEE DOCUMENTATION + Call GAUG_removeHyperlinksForCitations("RemoveHyperlinks") @@ -307,7 +309,7 @@ End Sub '***************************************************************************************** '***************************************************************************************** '** Author: José Luis González García ** -'** Last modified: 2016-12-28 ** +'** Last modified: 2017-01-11 ** '** ** '** Sub GAUG_createHyperlinksForCitationsIEEE() ** '** ** @@ -328,7 +330,9 @@ Sub GAUG_createHyperlinksForCitationsIEEE() '***************************** '* Cleaning old hyperlinks * '***************************** - GAUG_removeHyperlinksForCitations + 'possible values are "RemoveHyperlinks", "CleanEnvironment" and "CleanFullEnvironment" + 'SEE DOCUMENTATION + Call GAUG_removeHyperlinksForCitations("RemoveHyperlinks") @@ -467,25 +471,67 @@ End Sub '***************************************************************************************** '***************************************************************************************** '** Author: José Luis González García ** -'** Last modified: 2016-12-28 ** +'** Last modified: 2017-01-11 ** '** ** -'** Sub GAUG_removeHyperlinksForCitations() ** +'** Sub GAUG_removeHyperlinksForCitations(strTypeOfExecution As String) ** '** ** -'** Removes the bookmarks generated by GAUG_createHyperlinksForCitations ** +'** This is an improved version which runs much faster, ** +'** but still considered as experimental. ** +'** Make sure you have a backup of your document before you execute it! ** +'** ** +'** Removes the bookmarks generated by GAUG_createHyperlinksForCitations* ** '** in the bibliography inserted by Mendeley's plugin. ** -'** Removes the hyperlinks generated by GAUG_createHyperlinksForCitations ** +'** Removes the hyperlinks generated by GAUG_createHyperlinksForCitations* ** '** of the citations inserted by Mendeley's plugin. ** +'** Removes all manual modifications to the citations and bibliography if specified ** +'** ** +'** Parameter strTypeOfExecution can have three different values: ** +'** "RemoveHyperlinks": ** +'** UNEXPECTED RESULTS IF MANUAL MODIFICATIONS EXIST, BUT THE FASTEST ** +'** Removes the bookmarks and hyperlinks ** +'** Manual modifications to citations and bibliography will remain intact ** +'** "CleanEnvironment": ** +'** EXPERIMENTAL, BUT VERY FAST ** +'** Removes the bookmarks and hyperlinks ** +'** Manual modifications to citations and bibliography will also be removed ** +'** to have a clean environment ** +'** "CleanFullEnvironment": ** +'** SAFE, BUT VERY SLOW IN LONG DOCUMENTS ** +'** Removes the bookmarks and hyperlinks ** +'** Manual modifications to citations and bibliography will also be removed ** +'** to have a clean environment ** '***************************************************************************************** '***************************************************************************************** -Sub GAUG_removeHyperlinksForCitations() +Sub GAUG_removeHyperlinksForCitations(Optional ByVal strTypeOfExecution As String = "RemoveHyperlinks") Dim documentSection As Section Dim sectionField As Field + Dim fieldBookmark As Bookmark + Dim selectionHyperlinks As Hyperlinks + Dim i As Integer Dim blnFound As Boolean + Dim sectionFieldName, sectionFieldNewName As String + Dim objMendeleyApiClient As Object + Dim cbbUndoEditButton As CommandBarButton 'disables the screen updating while removing the hyperlinks to increase speed Application.ScreenUpdating = False + 'selects the type of execution + Select Case strTypeOfExecution + Case "RemoveHyperlinks" + 'nothing to do here + Case "CleanEnvironment" + 'get the API Client from Mendeley + Set objMendeleyApiClient = Application.Run("Mendeley.mendeleyApiClient") 'MabEntwickeltSich: This is the way to call the macro directly from Mendeley + Case "CleanFullEnvironment" + 'gets the Undo Edit Button + Set cbbUndoEditButton = Application.Run("MendeleyLib.getUndoEditButton") 'MabEntwickeltSich: This is the way to call the macro directly from Mendeley + Case Else + 'the execution option is not correct + Exit Sub + End Select + '***************************** '* Cleaning old hyperlinks * '***************************** @@ -494,9 +540,31 @@ Sub GAUG_removeHyperlinksForCitations() For Each sectionField In documentSection.Range.Fields 'if it is a citation If sectionField.Type = wdFieldAddin And Left(sectionField.Code, 18) = "ADDIN CSL_CITATION" Then - 'tries to restore it to its original value, first with the whole field sectionField.Select - GAUG_getUndoEditButton().Execute + + 'selects the type of execution to remove hyperlinks + Select Case strTypeOfExecution + Case "RemoveHyperlinks" + 'gets all hyperlinks from selection + Set selectionHyperlinks = Selection.Hyperlinks + 'deletes all hyperlinks + For i = selectionHyperlinks.Count To 1 Step -1 + 'deletes the current hyperlink + selectionHyperlinks(1).Delete + Next + Case "CleanEnvironment" + 'copied from Mendeley.undoEdit(), but removing the code that updates the toolbar in Microsoft Word (making the original function very slow) + 'restores the citations to the original state (deletes hyperlinks) + sectionFieldName = Application.Run("ZoteroLib.getMarkName", sectionField) + sectionFieldNewName = objMendeleyApiClient.undoManualFormat(sectionFieldName) + Call Application.Run("ZoteroLib.fnRenameMark", sectionField, sectionFieldNewName) 'MabEntwickeltSich: This is another way to call the macro directly from Mendeley + Call Application.Run("ZoteroLib.subSetMarkText", sectionField, INSERT_CITATION_TEXT) 'MabEntwickeltSich: This is another way to call the macro directly from Mendeley + Case "CleanFullEnvironment" + 'restores the citations to the original state (deletes hyperlinks) + 'slow version + cbbUndoEditButton.Execute + End Select + End If Next @@ -516,8 +584,8 @@ Sub GAUG_removeHyperlinksForCitations() For Each sectionField In documentSection.Range.Fields 'if it is the bibliography If sectionField.Type = wdFieldAddin And Trim(sectionField.Code) = "ADDIN Mendeley Bibliography CSL_BIBLIOGRAPHY" Then - 'deletes old bookmarks sectionField.Select + 'deletes all bookmarks For Each fieldBookmark In Selection.Bookmarks 'deletes current bookmark fieldBookmark.Delete @@ -528,6 +596,17 @@ Sub GAUG_removeHyperlinksForCitations() Next + 'selects the type of execution + Select Case strTypeOfExecution + Case "RemoveHyperlinks" + 'nothing to do here + Case "CleanEnvironment" + 'copied from Mendeley.undoEdit(), but removing the code that updates the toolbar in Microsoft Word (making the original function very slow) + Call Application.Run("MendeleyLib.refreshDocument") 'MabEntwickeltSich: This is another way to call the macro directly from Mendeley + Case "CleanFullEnvironment" + 'nothing to do here + End Select + 'reenables the screen updating Application.ScreenUpdating = True @@ -535,13 +614,69 @@ End Sub +'***************************************************************************************** +'***************************************************************************************** +'** Author: José Luis González García ** +'** Last modified: 2017-01-11 ** +'** ** +'** Sub GAUG_removeHyperlinks() ** +'** ** +'** Calls Sub GAUG_removeHyperlinksForCitations(strTypeOfExecution As String) ** +'** with parameter strTypeOfExecution = "RemoveHyperlinks" ** +'***************************************************************************************** +'***************************************************************************************** +Sub GAUG_removeHyperlinks() + 'removes all bookmarks and hyperlinks from the citations and bibliography + Call GAUG_removeHyperlinksForCitations("RemoveHyperlinks") +End Sub + + + +'***************************************************************************************** +'***************************************************************************************** +'** Author: José Luis González García ** +'** Last modified: 2017-01-11 ** +'** ** +'** Sub GAUG_cleanEnvironment() ** +'** ** +'** Calls Sub GAUG_removeHyperlinksForCitations(strTypeOfExecution As String) ** +'** with parameter strTypeOfExecution = "CleanFullEnvironment" ** +'***************************************************************************************** +'***************************************************************************************** +Sub GAUG_cleanEnvironment() + 'removes all bookmarks, hyperlinks and manual modifications to the citations and bibliography + Call GAUG_removeHyperlinksForCitations("CleanEnvironment") +End Sub + + + +'***************************************************************************************** +'***************************************************************************************** +'** Author: José Luis González García ** +'** Last modified: 2017-01-11 ** +'** ** +'** Sub GAUG_cleanFullEnvironment() ** +'** ** +'** Calls Sub GAUG_removeHyperlinksForCitations(strTypeOfExecution As String) ** +'** with parameter strTypeOfExecution = "CleanFullEnvironment" ** +'***************************************************************************************** +'***************************************************************************************** +Sub GAUG_cleanFullEnvironment() + 'removes all bookmarks, hyperlinks and manual modifications to the citations and bibliography + Call GAUG_removeHyperlinksForCitations("CleanFullEnvironment") +End Sub + + + '***************************************************************************************** '***************************************************************************************** '** Author: Mendeley ** -'** Last modified: 2016-04-26 ** +'** Last modified: 2017-01-11 ** '** ** '** Function GAUG_getUndoEditButton() As CommandBarButton ** '** ** +'** This functions is not used anymore in any fo the GAUG_* functions ** +'** ** '** Gets the CommandBarButton "Undo Edit" installed by Mendeley's plugin. ** '** The CommandBarButton is used to restore the original citation fields ** '** inserted by Mendeley. **