-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMendeleyMacros.bas
2802 lines (2326 loc) · 170 KB
/
MendeleyMacros.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Attribute VB_Name = "MendeleyMacros"
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-10 **
'** **
'** Function GAUG_isMendeleyCiteOMaticPluginInstalled() As Boolean **
'** **
'** Checks if the MS Word plugin Mendeley Cite-O-Matic is installed in Microsoft Word. **
'** **
'** RETURNS: A boolean that indicates if MS Word plugin Mendeley Cite-O-Matic is installed in Microsoft Word. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_isMendeleyCiteOMaticPluginInstalled() As Boolean
Dim installedAddin As AddIn
Dim blnFound As Boolean
'initializes the flag
blnFound = False
'checks all AddIns
For Each installedAddin In Application.AddIns
'if Mendeley 1.x has been found
If Left(installedAddin.Name, 11) = "Mendeley-1." Then
blnFound = True
'exits loop
Exit For
End If
Next
'returns true if the MS Word plugin Mendeley Cite-O-Matic is installed
GAUG_isMendeleyCiteOMaticPluginInstalled = blnFound
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-02 **
'** **
'** Function GAUG_getAvailableMendeleyVersion(Optional ByVal intUseMendeleyVersion As Integer = 0) As Integer **
'** **
'** Finds the available version of Mendeley. **
'** The version can be overridden by the optional parameter intUseMendeleyVersion, **
'** useful if the macros fail to detect the correct version. **
'** **
'** Parameter intUseMendeleyVersion can have three different values: **
'** 0: (DEFAULT) **
'** Autodetect Mendeley's version **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** **
'** RETURNS: An integer with the major version number of Mendeley that is installed. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getAvailableMendeleyVersion(Optional ByVal intUseMendeleyVersion As Integer = 0) As Integer
Dim intAvailableMendeleyVersion As Integer
Dim blnFound As Boolean
Dim fldField As Field
Dim ccContentControl As ContentControl
'if the optional argument is not within valid versions
If intUseMendeleyVersion < 0 Or intUseMendeleyVersion > 2 Then
MsgBox "The version " & intUseMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & _
"or 0 for the macros to automatically detect it." & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getAvailableMendeleyVersion(intUseMendeleyVersion)"
'stops the execution
End
End If
'if the macros must automatically detect the version of Mendeley's plugin
If intUseMendeleyVersion = 0 Then
blnFound = False
'checks all fields in the document
For Each fldField In ActiveDocument.Fields
'if this is a citation by Mendeley Desktop 1.x
If fldField.Type = wdFieldAddin And Left(fldField.Code, 18) = "ADDIN CSL_CITATION" Then
blnFound = True
'sets available version to 1
intAvailableMendeleyVersion = 1
'exits loop
Exit For
End If
Next fldField
'if version 1.x was not found, tries to detect if 2.x is installed
If Not blnFound Then
'checks all content controls in the document
For Each ccContentControl In ActiveDocument.ContentControls
'if this is a citation by Mendeley Reference Manager 2.x
If ccContentControl.Type = wdContentControlRichText And Left(Trim(ccContentControl.Tag), 21) = "MENDELEY_CITATION_v3_" Then
blnFound = True
'sets available version to 2
intAvailableMendeleyVersion = 2
'exits loop
Exit For
End If
Next ccContentControl
End If
'if the macros could not detect the version of Mendeley's plugin
'(there are no citations added by Mendeley in the entire document)
If Not blnFound Then
MsgBox "The version of Mendeley's plugin could not be detected." & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getAvailableMendeleyVersion(intUseMendeleyVersion)"
'stops the execution
End
End If
'if the user has specified the version of Mendeley's plugin
Else
'overrides the available version
intAvailableMendeleyVersion = intUseMendeleyVersion
End If
'returns the available version of Mendeley
GAUG_getAvailableMendeleyVersion = intAvailableMendeleyVersion
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-16 **
'** **
'** Function GAUG_getMendeleyWebExtensionXMLFileContents() **
'** **
'** Copies the current .docx file into a temporary folder and renames it to .zip. **
'** Extracts the contents of the .zip file and searches for the file 'word\webextensions\webextension<number>.xml' that **
'** corresponds to Mendeley Reference Manager 2.x (with the App Mendeley Cite). **
'** Reads the contents of the file 'word\webextensions\webextension<number>.xml' **
'** **
'** **
'** RETURNS: A string with the contents (if any) of the file 'webextension<number>.xml' that corresponds **
'** to Mendeley Reference Manager 2.x (with the App Mendeley Cite). **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getMendeleyWebExtensionXMLFileContents()
Dim adoStream As ADODB.Stream
Dim strDocumentName As String
Dim strDocumentPath As String
Dim strDocumentFullName As String
Dim strZipDocumentFullName As String
Dim strTemporalFolder As String
Dim strWebExtensionXMLFullName As String
Dim objFileSystem As Object
Dim objFile As Object
Dim objXMLFile As Object
Dim strXMLFileContents As String
Dim objShell As Object
Dim objZipAsFolder As Object
'initializes the file system object
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
'initializes the stream (used to read the UTF-8 XML files)
Set adoStream = New ADODB.Stream
adoStream.Charset = "UTF-8"
'initializes the string that will hold the the contents of the file 'webextension<number>.xml' that corresponds to Mendeley Reference Manager 2.x (with the App Mendeley Cite)
strXMLFileContents = ""
'checks if the document has a file path (if it is saved)
If ActiveDocument.Path <> "" Then
'gets the name, path and full name of the active document
strDocumentName = ActiveDocument.Name
strDocumentPath = ActiveDocument.Path
strDocumentFullName = ActiveDocument.FullName
'builds the path for a temporary folder to unzip the document
strTemporalFolder = Environ$("temp") & "\" & "MendeleyMacros_GAUG_temp"
'cleans up temporary folder if it exists
If objFileSystem.FolderExists(strTemporalFolder) Then
objFileSystem.DeleteFolder strTemporalFolder, True
End If
'creates temporary folder
objFileSystem.CreateFolder strTemporalFolder
'copies the .docx file and renames it to .zip
strZipDocumentFullName = strTemporalFolder & "\" & strDocumentName & ".zip"
objFileSystem.CopyFile strDocumentFullName, strZipDocumentFullName
'initializes the shell object
Set objShell = CreateObject("Shell.Application")
'opens the zip file (which is the .docx file)
Set objZipAsFolder = objShell.namespace(CVar(strZipDocumentFullName))
'if the zip file could be opened
If Not objZipAsFolder Is Nothing Then
'extracts the zip file (copies the files from the zip to the temporary folder without showing progress)
objShell.namespace(CVar(strTemporalFolder)).CopyHere objZipAsFolder.items, 4
'Microsoft Word may have several web extensions installed
'we need to find the file that corresponds to Mendeley Reference Manager 2.x (with the App Mendeley Cite)
'iterates over all files in folder 'word\webextensions\'
For Each objFile In objFileSystem.getfolder(CVar(strTemporalFolder & "\" & "word\webextensions")).Files
'if the file name starts with 'webextension' and finishes with '.xml'
If (Left(objFile.Name, 12) = "webextension") And (Right(objFile.Name, 4) = ".xml") Then
'creates the path to the 'webextension<number>.xml' file inside the unzipped folder
strWebExtensionXMLFullName = strTemporalFolder & "\word\webextensions\" & objFile.Name
'checks if the file 'webextension<number>.xml' exists
If objFileSystem.FileExists(strWebExtensionXMLFullName) Then
'opens the XML file, reads and closes it
adoStream.Open
adoStream.LoadFromFile strWebExtensionXMLFullName
strXMLFileContents = adoStream.ReadText
adoStream.Close
'if the files contains '<we:property name="MENDELEY_CITATIONS"', it is the one we are looking for
If InStr(1, strXMLFileContents, "<we:property name=" & Chr(34) & "MENDELEY_CITATIONS" & Chr(34), vbTextCompare) > 0 Then
'stops the search, we have the file
Exit For
Else
'clears the contents of the string and continues searching for the file
strXMLFileContents = ""
End If
End If 'checks if the file 'webextension<number>.xml' exists
End If 'if the file name starts with 'webextension' and finishes with '.xml'
Next objFile 'iterates over all files in folder 'word\webextensions\'
'cleans up the temporary folder
objFileSystem.DeleteFolder strTemporalFolder, True
End If 'if the zip file could be opened
End If 'checks if the document has a file path (if it is saved)
'returns the contents (if any) of the file 'webextension<number>.xml' that corresponds to Mendeley Reference Manager 2.x (with the App Mendeley Cite)
GAUG_getMendeleyWebExtensionXMLFileContents = strXMLFileContents
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-16 **
'** **
'** Function GAUG_getAllCitationsFullInformation(intMendeleyVersion As Integer) As String **
'** **
'** Finds and returns the full information of all citations, when available. **
'** **
'** Parameter intMendeleyVersion can have two different values: **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** The function returns an empty string due to the fact that **
'** Mendeley Desktop 1.x stores the information of each citation inside the field of the citation **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** The function returns the information of all citations in a single string **
'** **
'** RETURNS: A string that contains all the information of all citations (when Mendeley Reference Manager 2.x is available). **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getAllCitationsFullInformation(ByVal intMendeleyVersion As Integer) As String
Dim objRegExpWordOpenXMLCitations As RegExp
Dim colMatchesWordOpenXMLCitations As MatchCollection
Dim objMatchWordOpenXMLCitations As match
Dim strAllCitationsFullInformation As String
Dim blnFound As Boolean
'if the argument is not within valid versions
If intMendeleyVersion < 1 Or intMendeleyVersion > 2 Then
MsgBox "The version " & intMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getAllCitationsFullInformation(intMendeleyVersion)"
'stops the execution
End
End If
'initialize the flag
blnFound = False
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
blnFound = True
'nothing to do here, Mendeley Desktop 1.x does not provide the information of all citations as a single block of information
strAllCitationsFullInformation = ""
'Mendeley Reference Manager 2.x is installed
Case 2
Set objRegExpWordOpenXMLCitations = New RegExp
'ActiveDocument.WordOpenXML contains everything on the document, including hidden information about the citations added by Mendeley's plugin
'we need that hidden information to be able to match every citation to the corresponding entry in the bibliography
'(the information is stored as one block of data within WordOpenXML)
'sets the pattern to match everything from '<we:property name="MENDELEY_CITATIONS"' to (but not including) '<we:property name="MENDELEY_CITATIONS_STYLE"' or '</we:properties><we:bindings/>'
'for details on the regular expression, see https://stackoverflow.com/questions/7124778/how-can-i-match-anything-up-until-this-sequence-of-characters-in-a-regular-exp
objRegExpWordOpenXMLCitations.Pattern = "<we:property name=\" & Chr(34) & "MENDELEY_CITATIONS\" & Chr(34) & ".+?(?=((<we:property name=\" & Chr(34) & "MENDELEY_CITATIONS_STYLE\" & Chr(34) & ")|(</we:properties><we:bindings/>)))"
'sets case insensitivity
objRegExpWordOpenXMLCitations.IgnoreCase = False
'sets global applicability
objRegExpWordOpenXMLCitations.Global = True
'gets the full XML of the document
'if file 'word\webextensions\webextension<number>.xml' is bigger than 1MB, the instruction 'ActiveDocument.WordOpenXML' fails
'better to directly read the file 'word\webextensions\webextension<number>.xml'
'strWordOpenXML = ActiveDocument.WordOpenXML
strWordOpenXML = GAUG_getMendeleyWebExtensionXMLFileContents()
'checks that the string can be compared
If objRegExpWordOpenXMLCitations.Test(strWordOpenXML) Then
'gets the matches (all information of citations according to the regular expression)
Set colMatchesWordOpenXMLCitations = objRegExpWordOpenXMLCitations.Execute(strWordOpenXML)
'there should be only one match which contains the information of all citations
If colMatchesWordOpenXMLCitations.Count = 1 Then
blnFound = True
'treats all matches (the only one)
For Each objMatchWordOpenXMLCitations In colMatchesWordOpenXMLCitations
'replaces all '"' by '"' to handle the string more easily
strAllCitationsFullInformation = Replace(objMatchWordOpenXMLCitations.value, """, Chr(34))
Next objMatchWordOpenXMLCitations
End If
End If
End Select
'if no information could be found (or many matches where found which is not expected)
If Not blnFound Then
MsgBox "Could not find ONLY one match when looking for the full information of all citations." & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getAllCitationsFullInformation(intMendeleyVersion)"
'stops the execution
End
End If
'returns the string that contains all the information of all citations (when Mendeley Reference Manager 2.x is available)
GAUG_getAllCitationsFullInformation = strAllCitationsFullInformation
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-02 **
'** **
'** Function GAUG_getCitationFullInfo(ByVal intMendeleyVersion As Integer, ByVal strAllCitationsFullInformation As String, **
'** ByVal fldCitation As Field, ByVal ccCitation As ContentControl) As String **
'** **
'** Finds and returns the full information of a particular citation. **
'** **
'** Parameter intMendeleyVersion can have two different values: **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** Parameter strAllCitationsFullInformation is a string that contains all information of all citations **
'** (when Mendeley Reference Manager 2.x is used) **
'** Parameter fldCitation is the citation's field **
'** (when Mendeley Desktop 1.x is used) **
'** Parameter ccCitation is the citation's content control **
'** (when Mendeley Reference Manager 2.x is used) **
'** **
'** RETURNS: A string that contains the full information of the citation. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getCitationFullInfo(ByVal intMendeleyVersion As Integer, ByVal strAllCitationsFullInformation As String, ByVal fldCitation As Field, ByVal ccCitation As ContentControl) As String
Dim objRegExpVisibleCitationItems As RegExp
Dim colMatchesVisibleCitationItems As MatchCollection
Dim objMatchVisibleCitationItem As match
Dim strCitationFullInfo As String
Dim blnFound As Boolean
'if the argument is not within valid versions
If intMendeleyVersion < 1 Or intMendeleyVersion > 2 Then
MsgBox "The version " & intMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getCitationFullInfo(intMendeleyVersion, strAllCitationsFullInformation, fldCitation, ccCitation)"
'stops the execution
End
End If
'initialize the flag
blnFound = False
'initializes the variable
strCitationFullInfo = ""
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'if the citation's field is not empty
If Not (fldCitation Is Nothing) Then
blnFound = True
'the full information of the citation is inside the citation's field
strCitationFullInfo = fldCitation.Code
End If
'Mendeley Reference Manager 2.x is installed
Case 2
'if the citation's content control is not empty
If Not (ccCitation Is Nothing) Then
'creates the object for regular expressions
Set objRegExpVisibleCitationItems = New RegExp
'sets the pattern to match everything from '"citationID":"MENDELEY_CITATION_' to (but not including) '"citationID":"MENDELEY_CITATION_' or to the end of the string
'this gets individual citations, then the correct one can be selected
objRegExpVisibleCitationItems.Pattern = "{\" & Chr(34) & "citationID\" & Chr(34) & ":\" & Chr(34) & "MENDELEY_CITATION_.+?(?=(({\" & Chr(34) & "citationID\" & Chr(34) & ":\" & Chr(34) & "MENDELEY_CITATION_)|($)))"
'sets case insensitivity
objRegExpVisibleCitationItems.IgnoreCase = False
'sets global applicability
objRegExpVisibleCitationItems.Global = True
'checks that the string can be compared
If objRegExpVisibleCitationItems.Test(strAllCitationsFullInformation) Then
'gets the matches (all information of individual citations according to the regular expression)
Set colMatchesVisibleCitationItems = objRegExpVisibleCitationItems.Execute(strAllCitationsFullInformation)
'treats all matches (all individual citations) to find the correct one
For Each objMatchVisibleCitationItem In colMatchesVisibleCitationItems
'if the tag of the searched citation is in the current match
If InStr(1, objMatchVisibleCitationItem.value, ccCitation.Tag, vbTextCompare) > 0 Then
blnFound = True
'the full information of the citation is in this match
strCitationFullInfo = objMatchVisibleCitationItem.value
'exits loop
Exit For
End If
Next objMatchVisibleCitationItem
End If 'checks that the string can be compared
End If 'if the citation's content control is not empty
End Select
'if no information could be found
If Not blnFound Then
MsgBox "Could not find the full information of the citation." & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getCitationFullInfo(intMendeleyVersion, strAllCitationsFullInformation, fldCitation, ccCitation)"
'stops the execution
End
End If
'returns the citation information
GAUG_getCitationFullInfo = strCitationFullInfo
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-04 **
'** **
'** Function GAUG_getCitationItemsFromCitationFullInfo(ByVal intMendeleyVersion As Integer, **
'** ByVal strCitationFullInfo As String) As Variant() **
'** **
'** Returns the full information of all the individual items of a particular citation. **
'** **
'** Parameter intMendeleyVersion can have two different values: **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** Parameter strCitationFullInfo is a string that contains the full information of the citation **
'** **
'** RETURNS: An array that contains the full information of all the individual items of the citation. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getCitationItemsFromCitationFullInfo(ByVal intMendeleyVersion As Integer, ByVal strCitationFullInfo As String) As Variant()
Dim varCitationItemsFromCitationFullInfo() As Variant
Dim intTotalCitationItems As Integer
Dim objRegExpCitationItems As RegExp
Dim colMatchesCitationItems As MatchCollection
Dim objMatchVisibleCitationItemItem As match
'if the argument is not within valid versions
If intMendeleyVersion < 1 Or intMendeleyVersion > 2 Then
MsgBox "The version " & intMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getCitationItemsFromCitationFullInfo(intMendeleyVersion, strCitationFullInfo)"
'stops the execution
End
End If
'if the citation's full info is not empty
If Not (strCitationFullInfo = "") Then
Set objRegExpCitationItems = New RegExp
'sets case insensitivity
objRegExpCitationItems.IgnoreCase = False
'sets global applicability
objRegExpCitationItems.Global = True
'initializes the counter
intTotalCitationItems = 0
'builds the regular expression according to the version of Mendeley
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'sets the pattern to match everything from '{"id":"ITEM' to (but not including) ',{"id":"ITEM' or to the end of the string if no other item is found
'this gets individual citation items
objRegExpCitationItems.Pattern = "{\s*\" & Chr(34) & "id\" & Chr(34) & "\s*:\s*\" & Chr(34) & "ITEM.+?(?=((,\s*{\s*\" & Chr(34) & "id\" & Chr(34) & "\s*:\s*\" & Chr(34) & "ITEM)|($)))"
'Mendeley Reference Manager 2.x is installed
Case 2
'sets the pattern to match everything from '{"id":"' to (but not including) ',{"id":"' or to the end of the string if no other item is found
'this gets individual citation items
objRegExpCitationItems.Pattern = "{\s*\" & Chr(34) & "id\" & Chr(34) & "\s*:\s*\" & Chr(34) & ".+?(?=((,\s*{\s*\" & Chr(34) & "id\" & Chr(34) & "\s*:\s*\" & Chr(34) & ")|($)))"
End Select
'checks that the string can be compared
If objRegExpCitationItems.Test(strCitationFullInfo) Then
'gets the matches (individual citation items according to the regular expression)
Set colMatchesCitationItems = objRegExpCitationItems.Execute(strCitationFullInfo)
'treats all matches (all individual citation items)
For Each objMatchVisibleCitationItemItem In colMatchesCitationItems
'MsgBox objMatchVisibleCitationItemItem.value
'updates the counter to include this citation item
intTotalCitationItems = intTotalCitationItems + 1
'adds the full information of the citation item to the list
ReDim Preserve varCitationItemsFromCitationFullInfo(1 To intTotalCitationItems)
varCitationItemsFromCitationFullInfo(intTotalCitationItems) = objMatchVisibleCitationItemItem.value
Next objMatchVisibleCitationItemItem
End If 'checks that the string can be compared
End If 'if the citation's full info is not empty
'returns the list of all items (individual citations within the field or content control) from the citation full information
GAUG_getCitationItemsFromCitationFullInfo = varCitationItemsFromCitationFullInfo
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-08 **
'** **
'** Function GAUG_getAuthorsEditorsFromCitationItem(ByVal intMendeleyVersion As Integer, ByVal strAuthorEditor As String, **
'** ByVal strCitationItem As String) As Variant() **
'** **
'** Returns the list of authors or editors of the individual citation item. **
'** **
'** Parameter intMendeleyVersion can have two different values: **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** Parameter strAuthorEditor can have two different values: **
'** "author": **
'** Retrieve the authors of the individual citation item **
'** "editor": **
'** Retrieve the editors of the individual citation item **
'** Parameter strCitationItem is a string that contains the full information of the citation item **
'** **
'** RETURNS: An array that contains the list of authors or editors of the citation item. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getAuthorsEditorsFromCitationItem(ByVal intMendeleyVersion As Integer, ByVal strAuthorEditor As String, ByVal strCitationItem As String) As Variant()
Dim varAuthorsFomCitationItem() As Variant
Dim intTotalAuthorsEditorsFromCitationItem As Integer
Dim strFamilyName As String
Dim objRegExpAuthorsFromCitationItem, objRegExpAuthorFamilyNamesFromCitationItem As RegExp
Dim colMatchesAuthorsFromCitationItem, colMatchesAuthorFamilyNamesFromCitationItem As MatchCollection
Dim objMatchAuthorFromCitationItem, objMatchAuthorFamilyNameFromCitationItem As match
'if the argument is not within valid versions
If intMendeleyVersion < 1 Or intMendeleyVersion > 2 Then
MsgBox "The version " & intMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getAuthorsEditorsFromCitationItem(intMendeleyVersion, strAuthorEditor, strCitationItem)"
'stops the execution
End
End If
'if the argument is not 'author' or 'editor'
If Not (strAuthorEditor = "author" Or strAuthorEditor = "editor") Then
MsgBox "The argument for strAuthorEditor is not valid." & vbCrLf & vbCrLf & _
"Use " & Chr(34) & "author" & Chr(34) & " or " & Chr(34) & "editor" & Chr(34) & " instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getAuthorsEditorsFromCitationItem(intMendeleyVersion, strAuthorEditor, strCitationItem)"
'stops the execution
End
End If
'if the citation item's full info is not empty
If Not (strCitationItem = "") Then
Set objRegExpAuthorsFromCitationItem = New RegExp
'sets case insensitivity
objRegExpAuthorsFromCitationItem.IgnoreCase = False
'sets global applicability
objRegExpAuthorsFromCitationItem.Global = True
'builds the regular expression according to the version of Mendeley
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'sets the pattern to match everything from '"author":[' or '"editor":[' to (but not including) ']'
'this gets the full list of authors from the citation item
objRegExpAuthorsFromCitationItem.Pattern = "\" & Chr(34) & strAuthorEditor & "\" & Chr(34) & "\s*:\s*\[.+?(?=\])"
'Mendeley Reference Manager 2.x is installed
Case 2
'sets the pattern to match everything from '"author":[' or '"editor":[' to (but not including) ']'
'this gets the full list of authors from the citation item
objRegExpAuthorsFromCitationItem.Pattern = "\" & Chr(34) & strAuthorEditor & "\" & Chr(34) & "\s*:\s*\[.+?(?=\])"
End Select
'checks that the string can be compared
If objRegExpAuthorsFromCitationItem.Test(strCitationItem) Then
'gets the matches (list of all authors as a single block of data)
Set colMatchesAuthorsFromCitationItem = objRegExpAuthorsFromCitationItem.Execute(strCitationItem)
'treats all matches (there should be at most one match, zero when editors are listed instead of the authors)
For Each objMatchAuthorFromCitationItem In colMatchesAuthorsFromCitationItem
Set objRegExpAuthorFamilyNamesFromCitationItem = New RegExp
'sets case insensitivity
objRegExpAuthorFamilyNamesFromCitationItem.IgnoreCase = False
'sets global applicability
objRegExpAuthorFamilyNamesFromCitationItem.Global = True
'initializes the counter
intTotalAuthorsEditorsFromCitationItem = 0
'builds the regular expression according to the version of Mendeley
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'sets the pattern to match everything from '"family":"' to (but not including) '"'
'this gets the family names of authors from the citation item
objRegExpAuthorFamilyNamesFromCitationItem.Pattern = "\" & Chr(34) & "family\" & Chr(34) & "\s*:\s*\" & Chr(34) & ".+?(?=\" & Chr(34) & ")"
'Mendeley Reference Manager 2.x is installed
Case 2
'sets the pattern to match everything from '{"family":"' to (but not including) '"'
'this gets the family names of authors from the citation item
objRegExpAuthorFamilyNamesFromCitationItem.Pattern = "{\s*\" & Chr(34) & "family\" & Chr(34) & "\s*:\s*\" & Chr(34) & ".+?(?=\" & Chr(34) & ")"
End Select
'checks that the string can be compared
If objRegExpAuthorFamilyNamesFromCitationItem.Test(objMatchAuthorFromCitationItem.value) Then
'gets the matches (the family names of all authors)
Set colMatchesAuthorFamilyNamesFromCitationItem = objRegExpAuthorFamilyNamesFromCitationItem.Execute(objMatchAuthorFromCitationItem.value)
'treats all matches (the family name of the authors, if any)
For Each objMatchAuthorFamilyNameFromCitationItem In colMatchesAuthorFamilyNamesFromCitationItem
'gets only the family name, without the extra characters in the match
'from '{"family":"FamilyName' to just "FamilyName"
strFamilyName = Right(objMatchAuthorFamilyNameFromCitationItem.value, Len(objMatchAuthorFamilyNameFromCitationItem.value) - InStr(1, objMatchAuthorFamilyNameFromCitationItem.value, ":", vbTextCompare) - 1)
'updates the counter to include this family name of the author
intTotalAuthorsEditorsFromCitationItem = intTotalAuthorsEditorsFromCitationItem + 1
'adds the family name of the author to the list
ReDim Preserve varAuthorsFomCitationItem(1 To intTotalAuthorsEditorsFromCitationItem)
varAuthorsFomCitationItem(intTotalAuthorsEditorsFromCitationItem) = strFamilyName
Next objMatchAuthorFamilyNameFromCitationItem
End If 'checks that the string can be compared
Next objMatchAuthorFromCitationItem 'treats all matches (there should be at most one match, zero when editors are listed instead of the authors)
End If 'checks that the string can be compared
End If 'if the citation's full info is not empty
'returns the list of the family names of the authors
GAUG_getAuthorsEditorsFromCitationItem = varAuthorsFomCitationItem
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-04 **
'** **
'** Function GAUG_getYearFromCitationItem(ByVal intMendeleyVersion As Integer, ByVal strCitationItem As String) As String **
'** **
'** Returns the year of issue of the individual citation item. **
'** **
'** Parameter intMendeleyVersion can have two different values: **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** Parameter strCitationItem is a string that contains the full information of the citation item **
'** **
'** RETURNS: A string with the year of issue of the citation item (it does not include the letter that may be present after the year). **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getYearFromCitationItem(ByVal intMendeleyVersion As Integer, ByVal strCitationItem As String) As String
Dim strYearFromCitationItem As String
Dim objRegExpYearFromCitationItem As RegExp
Dim colMatchesYearFromCitationItem As MatchCollection
Dim objMatchYearFromCitationItem As match
'if the argument is not within valid versions
If intMendeleyVersion < 1 Or intMendeleyVersion > 2 Then
MsgBox "The version " & intMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_getYearFromCitationItem(intMendeleyVersion, strCitationItem)"
'stops the execution
End
End If
'if the citation item's full info is not empty
If Not (strCitationItem = "") Then
Set objRegExpYearFromCitationItem = New RegExp
'sets case insensitivity
objRegExpYearFromCitationItem.IgnoreCase = False
'sets global applicability
objRegExpYearFromCitationItem.Global = True
'builds the regular expression according to the version of Mendeley
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'sets the pattern to match everything from '"issued":{"date-parts":[[' to (but not including) ']]' or ','
'this gets only the year from the citation item, skips the month if present
objRegExpYearFromCitationItem.Pattern = "\" & Chr(34) & "issued\" & Chr(34) & "\s*:\s*{\s*\" & Chr(34) & "date\-parts\" & Chr(34) & "\s*:\s*\[\s*\[.+?(?=((\s*\]\s*\])|(,)))"
'Mendeley Reference Manager 2.x is installed
Case 2
'sets the pattern to match everything from '"issued":{"date-parts":[[' to (but not including) ']]' or ','
'this gets only the year from the citation item, skips the month if present
objRegExpYearFromCitationItem.Pattern = "\" & Chr(34) & "issued\" & Chr(34) & "\s*:\s*{\s*\" & Chr(34) & "date\-parts\" & Chr(34) & "\s*:\s*\[\s*\[.+?(?=((\s*\]\s*\])|(,)))"
End Select
'checks that the string can be compared
If objRegExpYearFromCitationItem.Test(strCitationItem) Then
'gets the matches (the year of issue)
Set colMatchesYearFromCitationItem = objRegExpYearFromCitationItem.Execute(strCitationItem)
'treats all matches (there should only one match)
For Each objMatchYearFromCitationItem In colMatchesYearFromCitationItem
'gets only the year, without the extra characters in the match
'from '"issued":{"date-parts":[["Year"' or '"issued":{"date-parts":[[Year' to just 'Year'
If Right(objMatchYearFromCitationItem.value, 1) = Chr(34) Then
'takes only the year and removes existing '"'
strYearFromCitationItem = Replace(Right(objMatchYearFromCitationItem.value, 5), Chr(34), "")
Else
strYearFromCitationItem = Right(objMatchYearFromCitationItem.value, 4)
End If
Next objMatchYearFromCitationItem 'treats all matches (there should be at most one match, zero when editors are listed instead of the authors)
End If 'checks that the string can be compared
End If 'if the citation item's full info is not empty
'returns the year of issue
GAUG_getYearFromCitationItem = strYearFromCitationItem
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-05 **
'** **
'** Function GAUG_getPartsFromVisibleCitationItem(ByVal strVisibleCitationItem As String) As Variant() **
'** **
'** Returns the authors or editors (if present), year of issue and letter (if present) of a particular visible citation item. **
'** **
'** Parameter strVisibleCitationItem is a string that contains the visible text of the citation item **
'** **
'** RETURNS: An array that contains the authors or editors, the year of issue and the letter after the year of the visible citation item. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_getPartsFromVisibleCitationItem(ByVal strVisibleCitationItem As String) As Variant()
Dim varPartsFromVisibleCitationItem(1 To 3) As Variant
Dim intSizeOfString As Integer
'initializes the parts to empty string
'(in some cases the authors will not be present or the year of issue does not have a letter at the end)
'e.g. in a citation such as '(FamilyName, 2024a, 2024b)', the second item in the citation is '2024b' (the first is 'FamilyName, 2024a') which does not include the author but includes a letter
'e.g. in a citation such as '(FamilyName, 2020; OtherFamilyName, 2023)', both items have authors but not letter at the end of the year
varPartsFromVisibleCitationItem(1) = ""
varPartsFromVisibleCitationItem(2) = ""
varPartsFromVisibleCitationItem(3) = ""
'removes leading or trailing blank spaces
strVisibleCitationItem = Trim(strVisibleCitationItem)
'if the citation item is not empty
If Not strVisibleCitationItem = "" Then
'gets the size of the string containing the visible citation item
intSizeOfString = Len(strVisibleCitationItem)
'if the citation item DOES NOT include a letter at the end of the year
If Asc(Right(strVisibleCitationItem, 1)) >= 48 And Asc(Right(strVisibleCitationItem, 1)) <= 57 Then
'if the visible citation item includes authors (or editors) its length is bigger than ', YYYY'
If intSizeOfString > 6 Then
'gets the authors or editors
varPartsFromVisibleCitationItem(1) = Mid(strVisibleCitationItem, 1, intSizeOfString - 6)
End If
'gets the year
varPartsFromVisibleCitationItem(2) = Mid(strVisibleCitationItem, intSizeOfString - 3, 4)
'if the citation item includes a letter at the end of the year
Else
'if the visible citation item includes authors (or editors) its length is bigger than ', YYYYx'
If intSizeOfString > 7 Then
'gets the authors or editors
varPartsFromVisibleCitationItem(1) = Mid(strVisibleCitationItem, 1, intSizeOfString - 7)
End If
'gets the year
varPartsFromVisibleCitationItem(2) = Mid(strVisibleCitationItem, intSizeOfString - 4, 4)
'gets the letter at the end of the year
varPartsFromVisibleCitationItem(3) = Right(strVisibleCitationItem, 1)
End If 'if the citation item DOES NOT include a letter at the end of the year
End If
'returns the three parts of the visible citation item
GAUG_getPartsFromVisibleCitationItem = varPartsFromVisibleCitationItem
End Function
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
'** Author: José Luis González García **
'** Last modified: 2024-10-09 **
'** **
'** Function GAUG_createHyperlinksForURLsInBibliography(ByVal intMendeleyVersion As Integer, ByVal fldBibliography As Field, **
'** ByVal ccBibliography As ContentControl, arrNonDetectedURLs() As Variant) As Integer **
'** **
'** Generates the hyperlinks for the URLs in the bibliography inserted by Mendeley's plugin. **
'** **
'** Parameter intMendeleyVersion can have two different values: **
'** 1: **
'** Use version 1.x of Mendeley Desktop **
'** 2: **
'** Use version 2.x of Mendeley Reference Manager **
'** Parameter fldBibliography is the bibliography's field **
'** (when Mendeley Desktop 1.x is used) **
'** Parameter ccBibliography is the bibliography's content control **
'** (when Mendeley Reference Manager 2.x is used) **
'** Parameter arrNonDetectedURLs specifies the URLs, not detected by the regular expression, **
'** to generate the hyperlinks in the bibliography **
'** **
'** RETURNS: An integer with the number of hyperlinks that could not be created for the URLs. **
'***********************************************************************************************************************************************
'***********************************************************************************************************************************************
Function GAUG_createHyperlinksForURLsInBibliography(ByVal intMendeleyVersion As Integer, ByVal fldBibliography As Field, ByVal ccBibliography As ContentControl, arrNonDetectedURLs() As Variant) As Integer
Dim blnURLFound As Boolean
Dim objRegExpURL As RegExp
Dim colMatchesURL As MatchCollection
Dim objMatchURL As match
Dim strURL, strSubStringOfURL As String
Dim varNonDetectedURL As Variant
Dim objCurrentFieldOrContentControl As Object
Dim intTotalURLsWithoutHyperlink As Integer
'if the argument is not within valid versions
If intMendeleyVersion < 1 Or intMendeleyVersion > 2 Then
MsgBox "The version " & intMendeleyVersion & " of Mendeley's plugin is not valid." & vbCrLf & vbCrLf & _
"Use version 1 or version 2 instead," & vbCrLf & vbCrLf & _
"Cannot continue creating hyperlinks.", _
vbCritical, "GAUG_createHyperlinksForURLsInBibliography(intMendeleyVersion, fldBibliography, ccBibliography, arrNonDetectedURLs)"
'stops the execution
End
End If
'creates the object for regular expressions (to get all URLs in bibliography)
Set objRegExpURL = New RegExp
'sets the pattern to match every URL in the bibliography (http, https or ftp)
objRegExpURL.Pattern = "((https?)|(ftp)):\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z0-9]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=\[\]\(\)<>;]*)"
'sets case insensitivity
objRegExpURL.IgnoreCase = False
'sets global applicability
objRegExpURL.Global = True
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'if the bibliography's field is not empty
If Not (fldBibliography Is Nothing) Then
'gets the field of the bibliography
Set objCurrentFieldOrContentControl = fldBibliography
Else
'nothing to do
GAUG_createHyperlinksForURLsInBibliography = 0
Exit Function
End If
'Mendeley Reference Manager 2.x is installed
Case 2
'if the bibliography's content control is not empty
If Not (ccBibliography Is Nothing) Then
'gets the content control of the bibliography
Set objCurrentFieldOrContentControl = ccBibliography
Else
'nothing to do
GAUG_createHyperlinksForURLsInBibliography = 0
Exit Function
End If
End Select
'if the array of non detected URLs is not empty
If Not Not arrNonDetectedURLs Then
'generates the hyperlinks from the list of non detected URLs
'the non detected URLs shall be done first or some conflicts may arise
For Each varNonDetectedURL In arrNonDetectedURLs
'prevents errors if the match is longer than 256 characters
strSubStringOfURL = Left(CStr(varNonDetectedURL), 256)
'according to the version of Mendeley
Select Case intMendeleyVersion
'Mendeley Desktop 1.x is installed
Case 1
'selects the current field (Mendeley's bibliography field)
objCurrentFieldOrContentControl.Select
'Mendeley Reference Manager 2.x is installed
Case 2
'selects the current content control (Mendeley's bibliography content control)
objCurrentFieldOrContentControl.range.Select
End Select
'finds all instances of current URL
Do
'finds and selects the text of the URL
With Selection.Find
.Forward = True
.Wrap = wdFindStop
.Text = strSubStringOfURL