Skip to content

Commit

Permalink
ref check can be performed on selection or document, add button to op…
Browse files Browse the repository at this point in the history
…en github webpage
  • Loading branch information
shishouyuan committed May 20, 2021
1 parent cfb291a commit 7b702e7
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 51 deletions.
83 changes: 50 additions & 33 deletions HandyRef.bas
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
'https://github.com/shishouyuan/HandyRefVBA

'A handy way to insert Cross Reference in MS Word
'Author: Shouyuan Shi @ South China University of Technology
'E-mail: [email protected]
'Creating Date: 2021/5/11

'Usage:
'Step 1: Select the contents that needed to be referenced and run macro CreateReferencePoint.
'Step 2: Select the point you want to insert cross reference and run macro InsertCrossReferenceField.

'用于在Word里方便地添加交叉引用
'作者: 史守圆 @ 华南理工大学
'E-mail: [email protected]
'创建时期: 2021/5/11

'用法:
'步骤1:选中要被引用的内容然后执行宏 CreateReferencePoint。
'步骤2:选中想要插入交插引用的地方然后执行宏 InsertCrossReferenceField。


Const HandyRefVersion = "20210520.1434"
Const HandyRefVersion = "20210521.0129"

Const TEXT_HandyRefGithubUrl = "https://github.com/shishouyuan/HandyRefVBA"



Const BookmarkPrefix = "_HandyRef"
Const RefBrokenCommentTitle = "$HANDYREF_REFERENCE_BROKEN_COMMENT$"

Expand All @@ -42,8 +34,10 @@ Const BrokenRefNumPosHolder = "#" '数量占位符
Const TEXT_NonCommecialPrompt = "仅限非商业用途"
Const TEXT_RefBrokenComment = "引用源丢失!"
Const TEXT_BrokenRefFoundPrompt = "发现了 " & BrokenRefNumPosHolder & " 个损坏的引用,已为其添加批注。"
Const TEXT_NoBrokenRefFoundPrompt = "没有发现损坏的索引。"
Const TEXT_NoBrokenRefFoundPrompt = "没有发现损坏的引用。"
Const TEXT_RefBrokenCommentClearedPrompt = "引用损坏批注已清除。"
Const TEXT_RefCheckingForWholeDocPrompt = "当前没有选中的内容,检查整个文档吗?" & vbCrLf & "这可能会需要一些时间。"
Const TEXT_ClearRefBrokenCommentForWholeDocPrompt = "当前没有选中的内容,清除整个文档中的引用损坏批注吗?"

#Else

Expand All @@ -59,7 +53,8 @@ Const BrokenRefNumPosHolder = "#" '数量占位符
Const TEXT_BrokenRefFoundPrompt = BrokenRefNumPosHolder & " broken reference found, and comments are attached."
Const TEXT_NoBrokenRefFoundPrompt = "No broken reference found."
Const TEXT_RefBrokenCommentClearedPrompt = "Reference broken comments cleared."

Const TEXT_RefCheckingForWholeDocPrompt = "Nothing is selected. Check the whole document?" & vbCrLf & "This may take a while."
Const TEXT_ClearRefBrokenCommentForWholeDocPrompt = "Nothing is selected. Clear reference broken comments for the whole document?"
#End If


Expand All @@ -78,7 +73,7 @@ Public Sub HandyRef_CreateReferencePoint()


If rg.End - rg.Start = 0 Then
MsgBox TEXT_CreateReferencePoint_NothingSelected, vbOKOnly, TEXT_HandyRefAppName
MsgBox TEXT_CreateReferencePoint_NothingSelected, vbOKOnly + vbInformation, TEXT_HandyRefAppName
Exit Sub
End If

Expand Down Expand Up @@ -133,43 +128,64 @@ Public Sub HandyRef_InsertCrossReferenceField()
ActiveDocument.Fields.Add Selection.Range, WdFieldType.wdFieldRef, selectedBM.Name
lastBMRefered = True
Else
MsgBox TEXT_InsertCrossReferenceField_CannotCrossFile, vbOKOnly, TEXT_HandyRefAppName
MsgBox TEXT_InsertCrossReferenceField_CannotCrossFile, vbOKOnly + vbInformation, TEXT_HandyRefAppName
End If
Else
Set selectedBM = Nothing
GoTo noRefPointPrompt
End If
Else
noRefPointPrompt:
MsgBox TEXT_InsertCrossReferenceField_NoRefPoint, vbOKOnly, TEXT_HandyRefAppName
MsgBox TEXT_InsertCrossReferenceField_NoRefPoint, vbOKOnly + vbInformation, TEXT_HandyRefAppName
End If
End Sub

Public Sub HandyRef_ClearRefBrokenComment_RibbonFun(ByVal control As IRibbonControl)
HandyRef_ClearRefBrokenComment
MsgBox TEXT_RefBrokenCommentClearedPrompt, vbOKOnly, TEXT_HandyRefAppName

If Application.Selection.End - Application.Selection.Start = 0 Then
If MsgBox(TEXT_ClearRefBrokenCommentForWholeDocPrompt, vbOKCancel + vbQuestion, TEXT_HandyRefAppName) = vbOK Then
HandyRef_ClearRefBrokenComment ActiveDocument.Range
Else
Exit Sub
End If
Else
HandyRef_ClearRefBrokenComment Application.Selection.Range
End If
MsgBox TEXT_RefBrokenCommentClearedPrompt, vbOKOnly + vbInformation, TEXT_HandyRefAppName

End Sub

Public Sub HandyRef_ClearRefBrokenComment()
Public Sub HandyRef_ClearRefBrokenComment(targetRange As Range)
Dim cmt As Comment
Dim s As String
For Each cmt In ActiveDocument.Comments
s = cmt.Range.Paragraphs.Last.Range.Text
s = Replace(s, vbCr, "")
s = Replace(s, vbLf, "")
If StrComp(s, RefBrokenCommentTitle) = 0 Then
cmt.DeleteRecursively
For Each cmt In targetRange.Comments
If cmt.Reference.InRange(targetRange) Then 'targetRange.Comments will also return comments before targentRange. may be a bug or misunderstanding
s = cmt.Range.Paragraphs.Last.Range.Text
s = Replace(s, vbCr, "")
s = Replace(s, vbLf, "")
s = Trim(s)
If StrComp(s, RefBrokenCommentTitle) = 0 Then
cmt.DeleteRecursively

End If
End If
Next cmt
End Sub

Public Sub HandyRef_CheckForBrokenRef_RibbonFun(ByVal control As IRibbonControl)
HandyRef_CheckForBrokenRef
If Application.Selection.End - Application.Selection.Start = 0 Then
If MsgBox(TEXT_RefCheckingForWholeDocPrompt, vbOKCancel + vbQuestion, TEXT_HandyRefAppName) = vbOK Then
HandyRef_CheckForBrokenRef ActiveDocument.Range
End If
Else
HandyRef_CheckForBrokenRef Application.Selection.Range
End If

End Sub

Public Sub HandyRef_CheckForBrokenRef()
Public Sub HandyRef_CheckForBrokenRef(checkingRange As Range)

HandyRef_ClearRefBrokenComment
HandyRef_ClearRefBrokenComment checkingRange

Dim refRegExp As Object
Set refRegExp = CreateObject("VBScript.RegExp")
Expand All @@ -185,7 +201,7 @@ Public Sub HandyRef_CheckForBrokenRef()
Dim fd As Field
Dim bmName As String
Dim cmt As Comment
For Each fd In ActiveDocument.Fields
For Each fd In checkingRange.Fields
If fd.Type = wdFieldRef Then
Set r = refRegExp.Execute(fd.Code.Text)
If r.Count > 0 Then
Expand All @@ -212,9 +228,9 @@ Public Sub HandyRef_CheckForBrokenRef()
Next fd

If brokenCount = 0 Then
MsgBox TEXT_NoBrokenRefFoundPrompt, vbOKOnly, TEXT_HandyRefAppName
MsgBox TEXT_NoBrokenRefFoundPrompt, vbOKOnly + vbInformation, TEXT_HandyRefAppName
Else
MsgBox Replace(TEXT_BrokenRefFoundPrompt, BrokenRefNumPosHolder, CStr(brokenCount)), vbOKOnly, TEXT_HandyRefAppName
MsgBox Replace(TEXT_BrokenRefFoundPrompt, BrokenRefNumPosHolder, CStr(brokenCount)), vbOKOnly + vbInformation, TEXT_HandyRefAppName
End If

End Sub
Expand All @@ -226,9 +242,10 @@ Public Sub HandyRef_About_RibbonFun(ByVal control As IRibbonControl)
End Sub

Public Sub HandyRef_About()

MsgBox TEXT_HandyRefAppName + vbCrLf + TEXT_HandyRefDescription + vbCrLf + TEXT_NonCommecialPrompt + vbCrLf + vbCrLf + TEXT_HandyRefAuthor + vbCrLf + TEXT_VersionPrompt + HandyRefVersion + vbCrLf + TEXT_HandyRefGithubUrl, vbOKOnly, TEXT_HandyRefAppName

MsgBox TEXT_HandyRefAppName + vbCrLf + TEXT_HandyRefDescription + vbCrLf + TEXT_NonCommecialPrompt + vbCrLf + vbCrLf + TEXT_HandyRefAuthor + vbCrLf + TEXT_VersionPrompt + HandyRefVersion + vbCrLf + TEXT_HandyRefGithubUrl, vbOKOnly + vbInformation, TEXT_HandyRefAppName
End Sub

Public Sub HandyRef_GetLatestVersion_RibbonFun(ByVal control As IRibbonControl)
Shell "explorer.exe " & TEXT_HandyRefGithubUrl
End Sub

Binary file modified HandyRef/HandyRef-Dev.dotm
Binary file not shown.
5 changes: 3 additions & 2 deletions HandyRef/customUI/handyRefUI.en-us.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<tabs>
<tab idMso="TabReferences">
<group id="HandyRefGroup" label="HandyRef">
<button id="HandyRef_CreateReferencePoint_Button" label="Create Source" screentip="Create Reference Source Point (Ctrl + Alt + C)" size="large" imageMso="TableOfContentsAddTextGallery" onAction="HandyRef_CreateReferencePoint_RibbonFun" />
<button id="HandyRef_InsertCrossReferenceField_Button" label="Insert Reference" screentip="Insert Cross Reference (Ctrl + Alt + V)" size="large" imageMso="CrossReferenceInsert" onAction="HandyRef_InsertCrossReferenceField_RibbonFun" />
<button id="HandyRef_CreateReferencePoint_Button" label="Create Source" screentip="Create Reference Source Point (Ctrl+Alt+C)" size="large" imageMso="TableOfContentsAddTextGallery" onAction="HandyRef_CreateReferencePoint_RibbonFun" />
<button id="HandyRef_InsertCrossReferenceField_Button" label="Insert Reference" screentip="Insert Cross Reference (Ctrl+Alt+V)" size="large" imageMso="CrossReferenceInsert" onAction="HandyRef_InsertCrossReferenceField_RibbonFun" />
<button id="HandyRef_CheckForBrokenRef_Button" label="Check Reference" screentip="Check for broken reference in the document and add comments for them" size="large" imageMso="ErrorChecking" onAction="HandyRef_CheckForBrokenRef_RibbonFun" />
<button id="HandyRef_ClearRefBrokenComment_Button" label="Clear Comments" screentip="Clear all reference broken comments" size="normal" imageMso="Clear" onAction="HandyRef_ClearRefBrokenComment_RibbonFun" />
<button id="HandyRef_GetLatestVersion_Button" label="Latest Vesion" screentip="Open webpage to get the latest version" size="normal" imageMso="Refresh" onAction="HandyRef_GetLatestVersion_RibbonFun" />
<button id="HandyRef_About_Button" label="About HandyRef" size="normal" imageMso="Info" onAction="HandyRef_About_RibbonFun" />
</group >
</tab>
Expand Down
31 changes: 16 additions & 15 deletions HandyRef/customUI/handyRefUI.zh-cn.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab idMso="TabReferences">
<group id="HandyRefGroup" label="HandyRef-快引">
<button id="HandyRef_CreateReferencePoint_Button" label="创建引用源" screentip="创建引用源 (Ctrl + Alt + C)" size="large" imageMso="TableOfContentsAddTextGallery" onAction="HandyRef_CreateReferencePoint_RibbonFun" />
<button id="HandyRef_InsertCrossReferenceField_Button" label="交叉引用" screentip="插入交叉引用 (Ctrl + Alt + V)" size="large" imageMso="CrossReferenceInsert" onAction="HandyRef_InsertCrossReferenceField_RibbonFun" />
<button id="HandyRef_CheckForBrokenRef_Button" label="检查引用" screentip="查找文档中是否存在损坏的引用,若找到将为其添加批注" size="large" imageMso="ErrorChecking" onAction="HandyRef_CheckForBrokenRef_RibbonFun" />
<button id="HandyRef_ClearRefBrokenComment_Button" label="清除批注" screentip="清除所有针对损坏引用的批注" size="normal" imageMso="Clear" onAction="HandyRef_ClearRefBrokenComment_RibbonFun" />
<button id="HandyRef_About_Button" label="关于“快引”" size="normal" imageMso="Info" onAction="HandyRef_About_RibbonFun" />
</group >
</tab>
</tabs>
</ribbon>
</customUI>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab idMso="TabReferences">
<group id="HandyRefGroup" label="HandyRef-快引">
<button id="HandyRef_CreateReferencePoint_Button" label="创建引用源" screentip="创建引用源 (Ctrl+Alt+C)" size="large" imageMso="TableOfContentsAddTextGallery" onAction="HandyRef_CreateReferencePoint_RibbonFun" />
<button id="HandyRef_InsertCrossReferenceField_Button" label="交叉引用" screentip="插入交叉引用 (Ctrl+Alt+V)" size="large" imageMso="CrossReferenceInsert" onAction="HandyRef_InsertCrossReferenceField_RibbonFun" />
<button id="HandyRef_CheckForBrokenRef_Button" label="检查引用" screentip="查找文档中是否存在损坏的引用,若找到将为其添加批注" size="large" imageMso="ErrorChecking" onAction="HandyRef_CheckForBrokenRef_RibbonFun" />
<button id="HandyRef_ClearRefBrokenComment_Button" label="清除批注" screentip="清除所有针对损坏引用的批注" size="normal" imageMso="Clear" onAction="HandyRef_ClearRefBrokenComment_RibbonFun" />
<button id="HandyRef_GetLatestVersion_Button" label="获取更新" screentip="打开网页获取最新版本" size="normal" imageMso="Refresh" onAction="HandyRef_GetLatestVersion_RibbonFun" />
<button id="HandyRef_About_Button" label="关于“快引”" size="normal" imageMso="Info" onAction="HandyRef_About_RibbonFun" />
</group >
</tab>
</tabs>
</ribbon>
</customUI>
2 changes: 1 addition & 1 deletion HandyRef/word/vbaData.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wne:vbaSuppData xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><wne:mcds><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_ABOUT" wne:name="HandyRef.Main.HandyRef_About" wne:bEncrypt="00" wne:cmg="56"/><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_CREATEREFERENCEPOINT" wne:name="HandyRef.Main.HandyRef_CreateReferencePoint" wne:bEncrypt="00" wne:cmg="56"/><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_INSERTCROSSREFERENCEFIELD" wne:name="HandyRef.Main.HandyRef_InsertCrossReferenceField" wne:bEncrypt="00" wne:cmg="56"/><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_CHECKFORBROKENREF" wne:name="HandyRef.Main.HandyRef_CheckForBrokenRef" wne:bEncrypt="00" wne:cmg="56"/><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_CLEARREFBROKENCOMMENT" wne:name="HandyRef.Main.HandyRef_ClearRefBrokenComment" wne:bEncrypt="00" wne:cmg="56"/></wne:mcds></wne:vbaSuppData>
<wne:vbaSuppData xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14"><wne:mcds><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_CREATEREFERENCEPOINT" wne:name="HandyRef.Main.HandyRef_CreateReferencePoint" wne:bEncrypt="00" wne:cmg="56"/><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_INSERTCROSSREFERENCEFIELD" wne:name="HandyRef.Main.HandyRef_InsertCrossReferenceField" wne:bEncrypt="00" wne:cmg="56"/><wne:mcd wne:macroName="HANDYREF.MAIN.HANDYREF_ABOUT" wne:name="HandyRef.Main.HandyRef_About" wne:bEncrypt="00" wne:cmg="56"/></wne:mcds></wne:vbaSuppData>
Binary file modified HandyRef/word/vbaProject.en-us.bin
Binary file not shown.
Binary file modified HandyRef/word/vbaProject.zh-cn.bin
Binary file not shown.
Binary file modified Release/HandyRef-English.dotm
Binary file not shown.
Binary file modified Release/HandyRef-中文.dotm
Binary file not shown.

0 comments on commit 7b702e7

Please sign in to comment.