Zotero在word中插入带超链接的参考文献/交叉引用/跳转参考文献时题目报错问题解决

又到一年毕业季 ,毕业论文令人头痛不已,

已经有大佬利用word宏解决了大部分的问题http://t.csdnimg.cn/HspZV

但我实际使用中出现了题目报错,大佬的经验我看都看不懂...经过我与GPT一下午的赋能,终于解决问题:1.题目第一个为数字,如3D-printing,报错;2.题目中包含”/“、”<“、”>“等特殊符号的报错。

直接贴代码,用于没时间自己去解决的小伙伴,但要是还有啥问题直接问你的GPT!(笑,我自己的一下午,不如GPT的30分钟

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
        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 = Replace(Replace(Replace(titleAnchor, "/", "_"), "<", "_"), ">", "_")

            titleAnchor = Left(titleAnchor, 40)
 ' 第一个为数字就添加一个下划线在前头
           If IsNumeric(Left(title, 1)) Then
                titleAnchor = "_" & titleAnchor
            End If
            
            ' 添加书签,名称为处理后的标题锚点
            ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=titleAnchor
            
            
            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
            With Selection.Find
                .Text = "^#"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            
            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
            
            numOrYear = Selection.Range.Text & ""
            
            pos = Len(numOrYear)
            
            style = Selection.style
                        
            ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", SubAddress:=titleAnchor, ScreenTip:="", TextToDisplay:="" & numOrYear
            aField.Select
            
            Selection.style = style
            Selection.style = ActiveDocument.Styles("s-citation")
            
            fieldCode = Mid(fieldCode, n2 + 1, Len(fieldCode) - n2 - 1)
        
        Loop
    End If
Next aField
ActiveDocument.Range(nStart, nEnd).Select
End Sub


我自己用的自己的word样式,就是字体那些,上标啥的,小伙伴用的时候还是要自己设置的哈,更改s-citation为你设置的样式名

Selection.style = ActiveDocument.Styles("s-citation")

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值