Skip to content

Commit

Permalink
New flag to include square brackets in hyperlinks
Browse files Browse the repository at this point in the history
There is a new configuration flag that allows to include the square brackets in the hyperlinks. It is not possible to remove the hyperlinks with square brackets and the argument `"RemoveHyperlinks"` or the wrapper `GAUG_removeHyperlinks` because Microsoft Word DOES NOT handle the square brackets in the hyperlinks very well.

Some typos removed.
  • Loading branch information
mabentwickeltsich authored Aug 14, 2017
1 parent 238d82f commit 0898226
Showing 1 changed file with 84 additions and 19 deletions.
103 changes: 84 additions & 19 deletions macros.vba
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'*****************************************************************************************
'*****************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2017-01-12 **
'** Last modified: 2017-08-14 **
'** **
'** Sub GAUG_createHyperlinksForCitationsAPA() **
'** **
Expand All @@ -21,14 +21,18 @@ Sub GAUG_createHyperlinksForCitationsAPA()
Dim colMatchesBiblio, colMatchesCitation, colMatchesCitationData, colMatchesBiblioEntry, colMatchesFindEntry As MatchCollection
Dim objMatchBiblio, objMatchCitation, objMatchCitationData, objMatchFindEntry As Match
Dim strTempMatch, strLastAuthors, strLastYear As String
Dim strTypeOfExecution As String


'*****************************
'* Cleaning old hyperlinks *
'*****************************
'possible values are "RemoveHyperlinks", "CleanEnvironment" and "CleanFullEnvironment"
'SEE DOCUMENTATION
Call GAUG_removeHyperlinksForCitations("RemoveHyperlinks")
strTypeOfExecution = "RemoveHyperlinks"

'removes all hyperlinks
Call GAUG_removeHyperlinksForCitations(strTypeOfExecution)



Expand Down Expand Up @@ -365,7 +369,7 @@ Sub GAUG_createHyperlinksForCitationsAPA()

'Ends: Needs re-work

'at this point current citation entry is linked to corresponding reference in bilio
'at this point current citation entry is linked to corresponding reference in biblio

Next 'treats all matches (all entries in citation) to generate hyperlinks

Expand All @@ -374,7 +378,7 @@ Sub GAUG_createHyperlinksForCitationsAPA()
End If 'if it is a citation
Next 'sectionField

'at this point all citations are linked to their corresponding reference in bilio
'at this point all citations are linked to their corresponding reference in biblio

Next 'documentSection

Expand All @@ -388,7 +392,7 @@ End Sub
'*****************************************************************************************
'*****************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2017-01-11 **
'** Last modified: 2017-08-14 **
'** **
'** Sub GAUG_createHyperlinksForCitationsIEEE() **
'** **
Expand All @@ -407,14 +411,46 @@ Sub GAUG_createHyperlinksForCitationsIEEE()
Dim objRegExpCitation As RegExp
Dim colMatchesCitation As MatchCollection
Dim objMatchCitation As Match
Dim blnIncludeSquareBracketsInHyperlinks As Boolean
Dim strTypeOfExecution As String


'*****************************
'* Custom configuration *
'*****************************
'possible values are True and False
'SEE DOCUMENTATION
'if set to True, then argument "RemoveHyperlinks" cannot be used when cleaning old hyperlinks
blnIncludeSquareBracketsInHyperlinks = False






'*****************************
'* Cleaning old hyperlinks *
'*****************************
'possible values are "RemoveHyperlinks", "CleanEnvironment" and "CleanFullEnvironment"
'SEE DOCUMENTATION
Call GAUG_removeHyperlinksForCitations("RemoveHyperlinks")
strTypeOfExecution = "RemoveHyperlinks"

'checks for conflicts (square brackets and removal of hyperlinks)
If blnIncludeSquareBracketsInHyperlinks And strTypeOfExecution = "RemoveHyperlinks" Then
MsgBox "The hyperlinks will include the square brackets and" & vbCrLf & _
"Microsoft Word does not like them this way." & vbCrLf & vbCrLf & _
"You can still continue, but please use the macro" & vbCrLf & _
"GAUG_removeHyperlinksForCitations() with argument " & vbCrLf & _
Chr(34) & "CleanEnvironment" & Chr(34) & " or " & _
Chr(34) & "CleanFullEnvironment" & Chr(34) & "." & vbCrLf, _
vbOKOnly, "Cannot continue creating hyperlinks"

'stops the execution
End
End If

'removes all hyperlinks
Call GAUG_removeHyperlinksForCitations(strTypeOfExecution)



Expand Down Expand Up @@ -461,14 +497,17 @@ Sub GAUG_createHyperlinksForCitationsIEEE()

'if a number of a reference was found, creates the bookmark with the selected text
If blnReferenceNumberFound Then
'restricts the selection to only the number
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Text = CStr(intRefereceNumber)
.Execute
blnReferenceNumberFound = .Found
End With
'if the square brackets are not part of the hyperlinks
If Not blnIncludeSquareBracketsInHyperlinks Then
'restricts the selection to only the number
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Text = CStr(intRefereceNumber)
.Execute
blnReferenceNumberFound = .Found
End With
End If

'creates the bookmark
Selection.Bookmarks.Add _
Expand Down Expand Up @@ -547,9 +586,12 @@ Sub GAUG_createHyperlinksForCitationsIEEE()
blnReferenceNumberFound = .Found
End With

'restricts the selection to only the number
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
'if the square brackets are not part of the hyperlinks
If Not blnIncludeSquareBracketsInHyperlinks Then
'restricts the selection to only the number
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
End If

'a cross-reference is not a good idea, it changes the text in citation (or may delete citation):
'Selection.Fields.Add Range:=Selection.Range, _
Expand Down Expand Up @@ -580,7 +622,7 @@ End Sub
'*****************************************************************************************
'*****************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2017-01-11 **
'** Last modified: 2017-08-14 **
'** **
'** Sub GAUG_removeHyperlinksForCitations(strTypeOfExecution As String) **
'** **
Expand Down Expand Up @@ -637,8 +679,14 @@ Sub GAUG_removeHyperlinksForCitations(Optional ByVal strTypeOfExecution As Strin
'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
'reenables the screen updating
Application.ScreenUpdating = True

MsgBox "Unknown execution type " & Chr(34) & strTypeOfExecution & Chr(34) & " for GAUG_removeHyperlinksForCitations()." & vbCrLf & vbCrLf & _
"Please, correct the error and try again.", vbOKOnly, "Invalid argument"

'the execution option is not correct
Exit Sub
End
End Select

'*****************************
Expand All @@ -656,8 +704,25 @@ Sub GAUG_removeHyperlinksForCitations(Optional ByVal strTypeOfExecution As Strin
Case "RemoveHyperlinks"
'gets all hyperlinks from selection
Set selectionHyperlinks = Selection.Hyperlinks

'deletes all hyperlinks
For i = selectionHyperlinks.Count To 1 Step -1
'this method produces errors if the hyperlinks include the square brackets in IEEE
If Left(selectionHyperlinks(1).Range.Text, 1) = "[" Then
MsgBox "There was an error removing the hyperlinks because they include the square brackets." & vbCrLf & _
"Microsoft Word does not like them this way." & vbCrLf & vbCrLf & _
"Please, use the macro GAUG_removeHyperlinksForCitations() with argument " & vbCrLf & _
Chr(34) & "CleanEnvironment" & Chr(34) & " or " & _
Chr(34) & "CleanFullEnvironment" & Chr(34) & "." & vbCrLf & _
"You can also call the respective wrapper function.", vbOKOnly, "Cannot remove hyperlinks"

'reenables the screen updating
Application.ScreenUpdating = True

'stops the execution
End
End If

'deletes the current hyperlink
selectionHyperlinks(1).Delete
Next
Expand Down

0 comments on commit 0898226

Please sign in to comment.