-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuickCards.bas
205 lines (163 loc) · 7.51 KB
/
QuickCards.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
Attribute VB_Name = "QuickCards"
Option Explicit
Public Sub AddQuickCard()
Dim Profile As String
Dim t As Template
Dim Name As String
Dim i As Long
Dim j As Long
Dim k As Long
On Error GoTo Handler
If Selection.Start = Selection.End Then
MsgBox "You must select some text to save a Quick Card", vbOKOnly
Exit Sub
End If
Name = InputBox("What shortcut word/phrase do you want to use for your Quick Card? Usually this is the author's last name.", "Add Quick Card", "")
If Name = "" Then Exit Sub
Profile = GetSetting("Verbatim", "QuickCards", "QuickCardsProfile", "Verbatim1")
If Not Profile Like "Verbatim*" Then Profile = "Verbatim1"
Set t = ActiveDocument.AttachedTemplate
For i = 1 To t.BuildingBlockTypes.Count
If t.BuildingBlockTypes.Item(i).Name = "Custom 1" Then
For j = 1 To t.BuildingBlockTypes.Item(i).Categories.Count
If t.BuildingBlockTypes.Item(i).Categories.Item(j).Name = Profile Then
For k = 1 To t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Count
If LCase$(t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Name) = LCase$(Name) Then
MsgBox "There's already a Quick Card with that name, try again with a different name!", vbOKOnly, "Failed To Add Quick Card"
Exit Sub
End If
Next k
End If
Next j
End If
Next i
t.BuildingBlockEntries.Add Name, wdTypeCustom1, Profile, Selection.Range
t.Save
Ribbon.RefreshRibbon
MsgBox "Successfully created Quick Card with the shortcut """ & Name & """"
Set t = Nothing
Exit Sub
Handler:
Set t = Nothing
MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub
'@Ignore ProcedureNotUsed
Public Sub InsertCurrentQuickCard()
On Error Resume Next
Selection.Range.InsertAutoText
On Error GoTo 0
End Sub
Public Sub InsertQuickCard(ByRef QuickCardName As String)
Dim Profile As String
Dim t As Template
Dim i As Long
Dim j As Long
Dim k As Long
On Error GoTo Handler
Profile = GetSetting("Verbatim", "QuickCards", "QuickCardsProfile", "Verbatim1")
If Not Profile Like "Verbatim*" Then Profile = "Verbatim1"
Set t = ActiveDocument.AttachedTemplate
For i = 1 To t.BuildingBlockTypes.Count
If t.BuildingBlockTypes.Item(i).Name = "Custom 1" Then
For j = 1 To t.BuildingBlockTypes.Item(i).Categories.Count
If t.BuildingBlockTypes.Item(i).Categories.Item(j).Name = Profile Then
For k = 1 To t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Count
If LCase$(t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Name) = LCase$(QuickCardName) Then
t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Insert Selection.Range, True
End If
Next k
End If
Next j
End If
Next i
Set t = Nothing
Exit Sub
Handler:
Set t = Nothing
MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub
Public Sub DeleteQuickCard(Optional ByRef QuickCardName As String)
Dim Profile As String
Dim t As Template
Dim i As Long
Dim j As Long
Dim k As Long
On Error GoTo Handler
If QuickCardName <> "" Or IsNull(QuickCardName) Then
If MsgBox("Are you sure you want to delete the Quick Card """ & QuickCardName & """? This cannot be reversed.", vbYesNo, "Are you sure?") = vbNo Then Exit Sub
Else
If MsgBox("Are you sure you want to delete all saved Quick Cards? This cannot be reversed.", vbYesNo, "Are you sure?") = vbNo Then Exit Sub
End If
Profile = GetSetting("Verbatim", "QuickCards", "QuickCardsProfile", "Verbatim1")
If Not Profile Like "Verbatim*" Then Profile = "Verbatim1"
Set t = ActiveDocument.AttachedTemplate
' Delete all building blocks in the Custom 1/Verbatim category
For i = 1 To t.BuildingBlockTypes.Count
If t.BuildingBlockTypes.Item(i).Name = "Custom 1" Then
For j = 1 To t.BuildingBlockTypes.Item(i).Categories.Count
If t.BuildingBlockTypes.Item(i).Categories.Item(j).Name = Profile Then
For k = t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Count To 1 Step -1
' If name provided, delete just that building block, otherwise delete everything in the category
If QuickCardName <> "" Or IsNull(QuickCardName) Then
If t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Name = QuickCardName Then
t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Delete
End If
Else
t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Delete
End If
Next k
End If
Next j
End If
Next i
t.Save
Set t = Nothing
Exit Sub
Handler:
Set t = Nothing
MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub
'@Ignore ParameterNotUsed, ProcedureNotUsed
'@Ignore ProcedureCanBeWrittenAsFunction
Public Sub GetQuickCardsContent(ByVal c As IRibbonControl, ByRef returnedVal As Variant)
' Get content for dynamic menu for Quick Cards
Dim Profile As String
Dim t As Template
Dim i As Long
Dim j As Long
Dim k As Long
Dim xml As String
Dim QuickCardName As String
Dim DisplayName As String
On Error Resume Next
Profile = GetSetting("Verbatim", "QuickCards", "QuickCardsProfile", "Verbatim1")
If Not Profile Like "Verbatim*" Then Profile = "Verbatim1"
Set t = ActiveDocument.AttachedTemplate
' Start the menu
xml = "<menu xmlns=""http://schemas.microsoft.com/office/2006/01/customui"">"
' Populate the list of current Quick Cards in the Custom 1 / Verbatim gallery
For i = 1 To t.BuildingBlockTypes.Count
If t.BuildingBlockTypes.Item(i).Name = "Custom 1" Then
For j = 1 To t.BuildingBlockTypes.Item(i).Categories.Count
If t.BuildingBlockTypes.Item(i).Categories.Item(j).Name = Profile Then
For k = 1 To t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Count
QuickCardName = t.BuildingBlockTypes.Item(i).Categories.Item(j).BuildingBlocks.Item(k).Name
DisplayName = Strings.OnlySafeChars(QuickCardName)
xml = xml & "<button id=""QuickCard" & Replace(DisplayName, " ", "") & """ label=""" & DisplayName & """ tag=""" & QuickCardName & """ onAction=""QuickCards.InsertQuickCardFromRibbon"" imageMso=""AutoSummaryResummarize"" />"
Next k
End If
Next j
End If
Next i
' Close the menu
xml = xml & "<button id=""QuickCardSettings"" label=""Quick Card Settings"" onAction=""Ribbon.RibbonMain"" imageMso=""AddInManager""" & " />"
xml = xml & "</menu>"
Set t = Nothing
returnedVal = xml
On Error GoTo 0
Exit Sub
End Sub
'@Ignore ProcedureNotUsed
Public Sub InsertQuickCardFromRibbon(ByVal c As IRibbonControl)
QuickCards.InsertQuickCard c.Tag
End Sub