tony8310的个人博客分享 http://blog.sciencenet.cn/u/tony8310

博文

zotero引用及doi等字体改成蓝色-vba

已有 306 次阅读 2025-12-16 12:50 |系统分类:科研笔记

change the font color of zotero citaion, cross reference, doi in the references, as blue.

在word中用vba 实现了以下的显示。

    (Kamishima and Takeuchi, 2016).       (Figure 1 a).

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. DOI:10.1016/j.agwat.2025.109770.

Corona, C.R., Ge, S., Anderson, S.P., 2023. Water-table response to extreme precipitation events. J. Hydrol. 618, 129140. https://doi.org/10.1016/j.jhydrol.2023.129140

借助Qwen,deepseek,修改后的代码如下:

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



https://blog.sciencenet.cn/blog-331295-1514553.html

上一篇:电脑微信4 与 微信3 之间的聊天记录互转
收藏 IP: 202.113.99.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2025-12-17 04:42

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部