又到一年毕业季 ,毕业论文令人头痛不已,
已经有大佬利用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")