zotero引用及doi等字体改成蓝色-vba
<p>change the font color of zotero citaion, cross reference, doi in the references, as blue.</p><p>在word中用vba 实现了以下的显示。</p><p><span style="font-size:16px;line-height: 115%;font-family:'Times New Roman',serif"> </span><span style="font-size:16px;line-height:115%;font-family:'Times New Roman',serif;color:blue">(Kamishima and Takeuchi, 2016)</span><span style="font-size:16px;line-height:115%;font-family:'Times New Roman',serif">. (<span style="color:blue">Figure 1 a</span>). </span></p><p><span style="font-size:16px;line-height:115%;font-family:'Times New Roman',serif"><span style="font-size:14px;font-family:'Times New Roman',serif">XIA W X, DONG J K, ZHANG B, et al. Optimization of agricultural planting structure in major grain-producing areas based on supply and demand and water resources: A case study of Hebei Province[J]. Agricultural Water Management, 2025, 319: 109770. <span style="color:blue">DOI:10.1016/j.agwat.2025.109770.</span></span></span></p><p style="line-height:200%"><span style="font-size:16px;line-height:200%;font-family:'Times New Roman',serif">Corona, C.R., Ge, S., Anderson, S.P., 2023. Water-table response to extreme precipitation events. J. Hydrol. 618, 129140. <span style="color:blue">https://doi.org/10.1016/j.jhydrol.2023.129140</span></span></p><p style="line-height:200%"><span style="line-height: 200%; font-family: " times="" new="" roman",="" serif;="" font-size:="" 18px;="" color:="" #000000;"="">借助Qwen,deepseek,修改后的代码如下:</span><span style="line-height: 200%; color: blue; font-size: 14px; font-family: " times="" new="" roman",="" serif;"=""></span></p><p style="line-height:200%"><span style="font-size: 16px; line-height: 200%; font-family: " times="" new="" roman",="" serif;="" color:="" #000000;"="">Sub CitingColorDOI() Dim fld As Field Dim para As Paragraph Dim rngPara As Range Dim regExp As Object Dim matches As Object Dim match As Object Dim i As Long ' =============== 第一部分:高亮引用域(REF / EndNote / Zotero)=============== For Each fld In ActiveDocument.Fields If Left(fld.Code, 4) = " REF" Or _ Left(fld.Code, 14) = " ADDIN EN.CITE" Or _ Left(fld.Code, 31) = " ADDIN ZOTERO_ITEM CSL_CITATION" Then fld.Result.Font.Color = wdColorBlue End If Next fld ' =============== 第二部分:高亮 DOI 文本(使用正则 + 精确 Range)=============== Set regExp = CreateObject("VBScript.RegExp") With regExp ' 匹配两种完整 DOI 格式(忽略大小写) .Pattern = "(?:DOI:\s*|https?://doi\.org/)(10\.\d{4,}/[^\s\[\]\{\}<>""']+)" .IgnoreCase = True .Global = True End With ' 遍历每个段落 For Each para In ActiveDocument.Paragraphs Set rngPara = para.Range ' 排除段落标记 ¶(避免干扰) If rngPara.Characters.Count > 1 Then rngPara.MoveEnd Unit:=wdCharacter, Count:=-1 End If If Len(Trim(rngPara.Text)) = 0 Then GoTo NextPara ' 在当前段落文本中执行正则匹配 Set matches = regExp.Execute(rngPara.Text) If matches.Count > 0 Then ' 从后往前处理,防止因修改格式导致位置偏移(虽然只改颜色,但良好习惯) For i = matches.Count - 1 To 0 Step -1 Set match = matches(i) ' 创建匹配范围:起始 = 段落起始 + 匹配位置,长度 = 匹配长度 Dim rngMatch As Range Set rngMatch = rngPara.Duplicate rngMatch.Start = rngMatch.Start + match.FirstIndex rngMatch.End = rngMatch.Start + match.Length ' 高亮整个匹配字符串(包括 "DOI:" 或 "https://doi.org/") rngMatch.Font.Color = wdColorBlue Next i End IfNextPara: Next para ' 清理对象 Set regExp = Nothing Set matches = Nothing MsgBox "引用和 DOI 已成功高亮为蓝色!注意检查,部分可能有错误!", vbInformationEnd Sub</span></p><p style="line-height:200%"><span style="font-size:16px;line-height:200%;font-family:'Times New Roman',serif"><span style="color:blue"> </span></span></p><p></p> <br><br>
<label style="font-size:13px; color:#850f0f">转载本文请联系原作者获取授权,同时请注明本文来自张兵科学网博客。<br>链接地址:</label><a href="https://blog.sciencenet.cn/blog-331295-1514553.html" target="_blank" style="font-size:13px; color:#850f0f">https://blog.sciencenet.cn/blog-331295-1514553.html </a>
<br><br>上一篇:<a href="https://blog.sciencenet.cn/blog-331295-1507984.html" target="_black">电脑微信4 与 微信3 之间的聊天记录互转</a><br>下一篇:<a href="https://blog.sciencenet.cn/blog-331295-1518483.html" target="_black">程序在cmd窗口能运行,服务无法正常运行</a><br><br><a href="https://blog.sciencenet.cn/blog-45-1520553.html" target="_blank"><label style="font-size:13px; color:#850f0f">欢迎参加科学网十佳博文评选活动!</label></a><br><br><label style="font-size:13px;">主办单位:</label> <a href="https://www.sciencenet.cn/" target="_blank"><img src="https://imgproxy.citrusreader.com/xxy6X5eDf9qApuFXY-Bys3MFAle12ma5dsDknAUXGFE/raw:1/aHR0cHM6Ly9pbWFnZS5zY2llbmNlbmV0LmNuL2hvbWUvMjAyNjA2LzAzLzE3MDM0Nmc2aDZqemxkZHp4bnZnazEuanBn" width="90px" referrerpolicy="no-referrer"></a> <label style="font-size:13px;">支持单位: <a href="https://ais.cn/u/Qnia6z" target="_blank"><img src="https://imgproxy.citrusreader.com/_od6bAJWBFdeI0LiXbod5O6YqC2AE05O_qhiC_7wG8Y/raw:1/aHR0cHM6Ly9pbWFnZS5zY2llbmNlbmV0LmNuL2hvbWUvMjAyNjA2LzA0LzE1MDAzM2kxamI3ZHBidjEycGJzYnMuanBn" width="144px" referrerpolicy="no-referrer"></a></label><br>