Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. Function ZoterZeroFieldFix(F) ' Fix a field with ZoterZero
  2. fieldChanged = 0 ' assume nothing changed, unless...
  3. fieldText = F.Code.Text ' get the current code of the field as text
  4. If InStr(fieldText, " ADDIN ZOTERO_ITEM CSL_CITATION") = 1 Then ' make sure this is a Zotero field to modify, starts with right text
  5. myArray = Split(fieldText, " ", 5) ' split that code into parts up to 5
  6. Dim Json As Object ' create a Json object to work with
  7. Set Json = JsonConverter.ParseJson(myArray(4)) ' get the fourth part, which is the Json data
  8. myResult = F.Result.Text ' get the displayed text from the field to work with it below
  9. '' TEXT REPLACEMENT FOR DOUBLED PARENTHESES TO REMOVE PARENTHESES
  10. If Left(myResult, 2) = "((" Then ' if it begins with doubled open parentheses...
  11. myResult = Split(myResult, "((", 2) ' split at double parentheses, only 2 parts
  12. myResult = myResult(1) ' get the second part (without open double parentheses)
  13. fieldChanged = 1 ' report updated below
  14. End If
  15. If Right(myResult, 2) = "))" Then ' if it ends with doubled open parentheses...
  16. myResult = StrReverse(myResult) ' reverse the string so we can work from the end:
  17. myResult = Split(myResult, "))", 2) ' split at double parentheses, only 2 parts
  18. myResult = myResult(1) ' get the second part (without close double parentheses)
  19. myResult = StrReverse(myResult) ' reverse the string back to normal:
  20. fieldChanged = 1 ' report updated below
  21. End If
  22. '' UPDATE AND SAVE FIELD IF CHANGED ABOVE:
  23. If fieldChanged = 1 Then ' the field text has been updated as MyResult, let's save it:
  24. ' the following two lines make Zotero think this was the original output, so no warnings!
  25. Json("properties")("plainCitation") = myResult ' set the Json citation data to new label
  26. Json("properties")("formattedCitation") = myResult ' again, other instance
  27. F.Result.Text = myResult ' replace the displayed text with the new text
  28. F.Result.Font.Underline = wdUnderlineNone ' remove dashed underlining from Zotero's delayed update feature if present
  29. myJson = JsonConverter.ConvertToJson(Json) ' collapse Json back to text
  30. F.Code.Text = " " & myArray(1) & " " & myArray(2) & " " & myArray(3) & " " & myJson & " " ' reconstruct field code
  31. ZoterZeroFieldFix = 1 ' updated, return success
  32. End If
  33. End If
  34. End Function
  35.  
  36. Sub ZoterZero()
  37. '
  38. ' ZoterZero main function
  39. '' if selection or text near cursor contains fields, check and fix them
  40. '' else check and fix all fields in document
  41. changeSuccess = 0 ' no fields fixed yet
  42. Selection.Expand Unit:=wdSentence ' expand the selection to at least sentence-level
  43. If Selection.Fields.Count > 0 Then ' if fields are selected...
  44. checkField = Selection.Fields.Count ' get the total number of fields
  45. While checkField > 0 ' check each field
  46. changeSuccess = ZoterZeroFieldFix(Selection.Fields(checkField)) ' check and fix this field
  47. checkField = checkField - 1 ' check the previous field next
  48. Wend
  49. End If
  50. If changeSuccess = 0 Then ' no fields have been updated yet, let's update all fields in document
  51. ' based on http://www.vbaexpress.com/kb/getarticle.php?kb_id=1100
  52. Dim rngStory As Word.Range ' vars for below
  53. Dim lngValidate As Long ' vars for below
  54. Dim oShp As Shape ' vars for below
  55. lngValidate = ActiveDocument.Sections(1).Headers(1).Range.StoryType ' starting point
  56. For Each rngStory In ActiveDocument.StoryRanges 'Iterate through all linked stories
  57. Do
  58. On Error Resume Next
  59. checkField = rngStory.Fields.Count ' get the total number of fields in this section
  60. While checkField > 0 ' check each field
  61. changeSuccess = ZoterZeroFieldFix(rngStory.Fields(checkField)) ' check and fix this field
  62. checkField = checkField - 1 ' check the previous field next
  63. Wend
  64. Select Case rngStory.StoryType
  65. Case 6, 7, 8, 9, 10, 11
  66. If rngStory.ShapeRange.Count > 0 Then
  67. For Each oShp In rngStory.ShapeRange
  68. If oShp.TextFrame.HasText Then
  69. checkField = Shp.TextFrame.TextRange.Fields.Count ' get the total number of fields in this section
  70. While checkField > 0 ' check each field
  71. changeSuccess = ZoterZeroFieldFix(Shp.TextFrame.TextRange.Fields(checkField)) ' check and fix this field
  72. checkField = checkField - 1 ' check the previous field next
  73. Wend
  74. End If
  75. Next
  76. End If
  77. Case Else 'Do Nothing
  78. End Select
  79. On Error GoTo 0
  80. 'Get next linked story (if any)
  81. Set rngStory = rngStory.NextStoryRange ' get ready for next section
  82. Loop Until rngStory Is Nothing ' keep going through until all sections are done
  83. Next
  84. End If
  85. Selection.Collapse ' reset cursor to beginning of section which isn't quite right but close enough
  86. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement