• Zotero在word中插入带超链接的参考文献/交叉引用/跳转参考文献


    Zotero以其丰富的插件而闻名,使用起来十分的带劲,最重要的是它是免费的、不卡顿,不像某专业软件。

    然而Zotero在word插入参考文献时,无法为参考文献添加超链接,这是一个不得不提的遗憾。

    不过,有大佬已经写出了基于Word VBA语言的代码,可以为Zotero在Word中的参考文献一键增加超链接!源代码如下所示:

    Word: Possibility to link references and bibliography in a document? - Zotero Forums

    不过,这个代码在运行的时候,我遇到了一个严重的问题。当一个位置同时引用的参考文献>2,就只能为前两个文献添加超链接,且第3个以及之后的参考文献。引用了同一个作者的两篇及以上的参考文献,只能给第一个文献添加超链接,第二个就无法添加。

    作为对VBA语言一窍不通的我,硬着头皮啃了一下源代码,终于发现了问题所在:在同一位置的参考文献添加链接时,原代码无法正常更新下一处要添加超链接的位置,最关键的是其中pos变量。

    为此,我增加了对同一位置参考文献的不同引用的定位代码,然后将定位结果复制给pos变量,让代码循环执行时可以正确找到应该添加参考文献的位置:

    1. Public Sub ZoteroLinkCitation()
    2. Dim nStart&, nEnd&
    3. nStart = Selection.Start
    4. nEnd = Selection.End
    5. Application.ScreenUpdating = False
    6. Dim title As String
    7. Dim titleAnchor As String
    8. Dim style As String
    9. Dim fieldCode As String
    10. Dim numOrYear As String
    11. Dim pos&, n1&, n2&
    12. ActiveWindow.View.ShowFieldCodes = True
    13. Selection.Find.ClearFormatting
    14. With Selection.Find
    15. .Text = "^d ADDIN ZOTERO_BIBL"
    16. .Replacement.Text = ""
    17. .Forward = True
    18. .Wrap = wdFindContinue
    19. .Format = False
    20. .MatchCase = False
    21. .MatchWholeWord = False
    22. .MatchWildcards = False
    23. .MatchSoundsLike = False
    24. .MatchAllWordForms = False
    25. End With
    26. Selection.Find.Execute
    27. With ActiveDocument.Bookmarks
    28. .Add Range:=Selection.Range, name:="Zotero_Bibliography"
    29. .DefaultSorting = wdSortByName
    30. .ShowHidden = True
    31. End With
    32. ActiveWindow.View.ShowFieldCodes = False
    33. For Each aField In ActiveDocument.Fields
    34. ' check if the field is a Zotero in-text reference
    35. If InStr(aField.Code, "ADDIN ZOTERO_ITEM") > 0 Then
    36. fieldCode = aField.Code
    37. pos = 0
    38. Paper_i = 1
    39. Do While InStr(fieldCode, """title"":""") > 0
    40. n1 = InStr(fieldCode, """title"":""") + Len("""title"":""")
    41. n2 = InStr(Mid(fieldCode, n1, Len(fieldCode) - n1), """,""") - 1 + n1
    42. title = Mid(fieldCode, n1, n2 - n1)
    43. titleAnchor = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(title, " ", "_"), "&", "_"), ":", "_"), ",", "_"), "-", "_"), ".", "_"), "(", "_"), ")", "_"), "?", "_"), "!", "_")
    44. titleAnchor = Left(titleAnchor, 40)
    45. Selection.GoTo What:=wdGoToBookmark, name:="Zotero_Bibliography"
    46. Selection.Find.ClearFormatting
    47. With Selection.Find
    48. .Text = Left(title, 255)
    49. .Replacement.Text = ""
    50. .Forward = True
    51. .Wrap = wdFindAsk
    52. .Format = False
    53. .MatchCase = False
    54. .MatchWholeWord = False
    55. .MatchWildcards = False
    56. .MatchSoundsLike = False
    57. .MatchAllWordForms = False
    58. End With
    59. Selection.Find.Execute
    60. Selection.Paragraphs(1).Range.Select
    61. With ActiveDocument.Bookmarks
    62. .Add Range:=Selection.Range, name:=titleAnchor
    63. .DefaultSorting = wdSortByName
    64. .ShowHidden = True
    65. End With
    66. aField.Select
    67. Selection.Find.ClearFormatting
    68. If pos = 0 Then
    69. ' 初始化起始位置和数组
    70. startPosition = 1
    71. ReDim commaPositions(1 To 1)
    72. ' 查找逗号的位置(前提是作者和年份之间采用英文逗号分隔符,否则要改为其他符号)
    73. Do
    74. commaPosition = InStr(startPosition, Selection, ",")
    75. If commaPosition > 0 Then
    76. ' 将逗号的位置添加到数组
    77. commaPositions(UBound(commaPositions)) = commaPosition
    78. ' 更新起始位置,以便下一次查找
    79. startPosition = commaPosition + 1
    80. ReDim Preserve commaPositions(1 To UBound(commaPositions) + 1)
    81. End If
    82. Loop While commaPosition > 0
    83. End If
    84. ' 输出记录的逗号位置
    85. For j = 1 To UBound(commaPositions)
    86. Debug.Print "Comma found at position: " & commaPositions(j)
    87. Next j
    88. With Selection.Find
    89. .Text = "^#"
    90. ' .Text = "^#"
    91. .Replacement.Text = ""
    92. .Forward = True
    93. .Wrap = wdFindContinue
    94. .Format = False
    95. .MatchCase = False
    96. .MatchWholeWord = False
    97. .MatchWildcards = False
    98. .MatchSoundsLike = False
    99. .MatchAllWordForms = False
    100. End With
    101. Selection1 = Selection.Find.Execute
    102. Selection.MoveLeft Unit:=wdCharacter, Count:=1
    103. Selection.MoveRight Unit:=wdCharacter, Count:=pos
    104. Selection.Find.Execute
    105. Selection.MoveLeft Unit:=wdCharacter, Count:=1
    106. 'Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
    107. Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
    108. numOrYear = Selection.Range.Text & ""
    109. ' pos = pos + Len(numOrYear)
    110. pos = commaPositions(Paper_i) - 1
    111. Paper_i = Paper_i + 1
    112. style = Selection.style
    113. ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", SubAddress:=titleAnchor, ScreenTip:="", TextToDisplay:="" & numOrYear
    114. aField.Select
    115. Selection.style = style
    116. 'Selection.style = ActiveDocument.Styles("CitationFormating")
    117. fieldCode = Mid(fieldCode, n2 + 1, Len(fieldCode) - n2 - 1)
    118. Loop
    119. End If
    120. Next aField
    121. ActiveDocument.Range(nStart, nEnd).Select
    122. End Sub

  • 相关阅读:
    【校招VIP】前端计算机网络之HTTP和HTTPS
    C语言上网计费系统模拟系统
    C#调用Dapper
    网络通信基础
    【云原生】Kubernetes----POD控制器
    基于SpringBoot的流浪动物管理系统设计与实现
    Ubuntu安装与配置MySQL简要记录
    《Nature》论文插图的Matlab复刻第2期—单组多色横向柱状图(Part1-305)
    input输入函数
    Python 潮流周刊第 38 期(摘要)+赠书5本
  • 原文地址:https://blog.csdn.net/weixin_42413559/article/details/134494771