当前位置: 首页 > article >正文

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变量,让代码循环执行时可以正确找到应该添加参考文献的位置:

Public Sub ZoteroLinkCitation()
Dim nStart&, nEnd&
nStart = Selection.Start
nEnd = Selection.End
Application.ScreenUpdating = False
Dim title As String
Dim titleAnchor As String
Dim style As String
Dim fieldCode As String
Dim numOrYear As String
Dim pos&, n1&, n2&

ActiveWindow.View.ShowFieldCodes = True
Selection.Find.ClearFormatting
With Selection.Find
    .Text = "^d ADDIN ZOTERO_BIBL"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute
With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, name:="Zotero_Bibliography"
    .DefaultSorting = wdSortByName
    .ShowHidden = True
End With
ActiveWindow.View.ShowFieldCodes = False


For Each aField In ActiveDocument.Fields
' check if the field is a Zotero in-text reference
    If InStr(aField.Code, "ADDIN ZOTERO_ITEM") > 0 Then
        fieldCode = aField.Code
        pos = 0
        Paper_i = 1
        Do While InStr(fieldCode, """title"":""") > 0
            n1 = InStr(fieldCode, """title"":""") + Len("""title"":""")
            n2 = InStr(Mid(fieldCode, n1, Len(fieldCode) - n1), """,""") - 1 + n1
        
            title = Mid(fieldCode, n1, n2 - n1)
            
            titleAnchor = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(title, " ", "_"), "&", "_"), ":", "_"), ",", "_"), "-", "_"), ".", "_"), "(", "_"), ")", "_"), "?", "_"), "!", "_")
            titleAnchor = Left(titleAnchor, 40)
            
            Selection.GoTo What:=wdGoToBookmark, name:="Zotero_Bibliography"
            Selection.Find.ClearFormatting
            With Selection.Find
                .Text = Left(title, 255)
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindAsk
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute
            
            Selection.Paragraphs(1).Range.Select
            
            With ActiveDocument.Bookmarks
                .Add Range:=Selection.Range, name:=titleAnchor
                .DefaultSorting = wdSortByName
                .ShowHidden = True
            End With
            
            aField.Select
                        
            Selection.Find.ClearFormatting
                
                If pos = 0 Then
                    ' 初始化起始位置和数组
                    startPosition = 1
                    ReDim commaPositions(1 To 1)
                    
                    ' 查找逗号的位置(前提是作者和年份之间采用英文逗号分隔符,否则要改为其他符号)
                    Do
                        commaPosition = InStr(startPosition, Selection, ",")
                        
                        If commaPosition > 0 Then
                            ' 将逗号的位置添加到数组
                            commaPositions(UBound(commaPositions)) = commaPosition
                            ' 更新起始位置,以便下一次查找
                            startPosition = commaPosition + 1
                            ReDim Preserve commaPositions(1 To UBound(commaPositions) + 1)
                        End If
                    Loop While commaPosition > 0
                End If
                ' 输出记录的逗号位置
    For j = 1 To UBound(commaPositions)
        Debug.Print "Comma found at position: " & commaPositions(j)
    Next j
                
                With Selection.Find
                    .Text = "^#"
                    ' .Text = "^#"
                    .Replacement.Text = ""
                    .Forward = True
                    .Wrap = wdFindContinue
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                End With
            
            Selection1 = Selection.Find.Execute
            
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            Selection.MoveRight Unit:=wdCharacter, Count:=pos
            
            Selection.Find.Execute
            Selection.MoveLeft Unit:=wdCharacter, Count:=1
            'Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
            Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
             
            numOrYear = Selection.Range.Text & ""
            
           ' pos = pos + Len(numOrYear)
           pos = commaPositions(Paper_i) - 1
           Paper_i = Paper_i + 1
            
            style = Selection.style
                        
            ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", SubAddress:=titleAnchor, ScreenTip:="", TextToDisplay:="" & numOrYear
            aField.Select
            
            Selection.style = style
            'Selection.style = ActiveDocument.Styles("CitationFormating")
            
            fieldCode = Mid(fieldCode, n2 + 1, Len(fieldCode) - n2 - 1)
        
        Loop
    End If
Next aField
ActiveDocument.Range(nStart, nEnd).Select
End Sub



http://www.kler.cn/a/134393.html

相关文章:

  • 【学习笔记】数据结构(七)
  • AI写作(二)NLP:开启自然语言处理的奇妙之旅(2/10)
  • vwmare虚拟机繁忙的解决办法
  • 解决:WSL2可视化opencv和pyqt冲突:QObject::moveToThread
  • 后端接口返回二进制文件,前端 window.opent预览展示
  • AI写作(四)预训练语言模型:开启 AI 写作新时代(4/10)
  • 队列和微服务的异步通信
  • Python选择排序和冒泡排序算法
  • linux基础:4:gdb的使用
  • 保姆级 | Nginx编译安装
  • golang学习笔记——条件表达式
  • 【Dubbo】Dubbo负载均衡实现解析
  • nodejs微信小程序-实验室上机管理系统的设计与实现-安卓-python-PHP-计算机毕业设计
  • 2023数维杯国际赛数学建模竞赛选题建议及B题思路讲解
  • Linux本地docker一键部署traefik+内网穿透工具实现远程访问Web UI管理界面
  • OpenAI 地震!首席执行官被解雇,背后的原因是?
  • linux 定时执行脚本
  • 【Flink】系统架构
  • 力扣372周赛
  • 微机原理练习题_13
  • 计算机网络——物理层-信道的极限容量(奈奎斯特公式、香农公式)
  • ElasticSearch快速入门
  • 【论文阅读】VideoComposer: Compositional Video Synthesis with Motion Controllability
  • 2023/11/15JAVA学习(线程池,Executors,网络编程,InetAddress,UDP,TCP,DatagramSocket)
  • 栈和队列概念
  • Nginx的核心配置文件