forked from mattretzer/Word-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleanup Macro
1076 lines (938 loc) · 39.4 KB
/
Cleanup Macro
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Option Explicit
Option Base 1
Dim activeRng As Range
Sub MacmillanManuscriptCleanup()
''''''''''''''''''''''''''''''''
'''created by Matt Retzer - matthew.retzer@macmillan.com
''''''''''''''''''''''''''''''
'version 3.4.3
'last updated 2014-10-20 by Erica Warren
''' - moved StylesHyperlink sub after PreserveWhiteSpaceinBrkStylesA sub to prevent styled blank paragraphs from being removed
'''''''''''''''''''''''''''''''''''''''
'version 3.4.2: template style updates only, not macro updates
''''''''''''''''''''''''''''''''''''''
'version 3.4.1
'last updated 2014-10-08 by Erica Warren
''' - added new Column Break (cbr) style to preserve white space macro
'''''''''''''''''''''''''''''''''''''''
'version 3.4
'last updated 2014-10-07 by Erica Warren
''' - removed Section Break (sbr) from RmNonWildcardItems sub
''' - added RemoveBookmarks sub
''' - added StyleHyperlinks sub, removed hyperlinks stuff from earlier version
'''''''''''''''''''''''''''''''''''''''
'version 3.3.1
'last updated 2014-09-17 by Erica Warren
''' - fixed space break style names that were changed in template
''''''''''''''''''''''''''''''''''''''
'version 3.3
'last updated 2014-09-16 by Erica Warren
''' - added to RmWhiteSpaceB:
''' - remove space before closing parens, closing bracket, closing braces
''' - remove space after opening parens, opening bracket, opening braces, dollar sign
''' - added double space to preserve character style search/replace
'''''''''''''''''''''''''''''''''
'version 3.2
'last updated 2014-09-12 by Erica Warren - erica.warren@macmillan.com
''' - changed double- and single- quotes replace to find only straight quotes
''' - added PreserveStyledPageBreaksA and PreserveStyledPageBreaksB, now required for correct InDesign import
''' - added PC_BestStylesView, Mac_BestStylesView, and StylesViewLaunch macros
''' - edited some msgBox text to make it a little more fun
'''''''''''''''''''''''''''''''
'''version 3.1
'''last updated 07/08/14:
''' - split Localstyle replace into to private subs
''' - style report bug fix
''' - adding f/ replace nbs, nbh, oh: to wildcard f/r
''' - added completion message for Cleanup macro
''' - changed TagHyperlink sub to tagexistingchrlinks, added 6 more hyperlink stylesarstyles, including hype
''' - added a backtick on closing tags for preserved break styles, and a call to remove paras trailing these breaks
''' - adding ' endash ' turn to emdash as per EW
''' - added 'save in place' msgbox for Cleanup macro.
''' - fixed embedded filed code hyperlink bug, just giving them a leading space
''' - prepared tagging for 'combinatrions' of local styles
''' - combined highlight removal with local style find loop
''' - combined smart quotes with existing no wildcard sub, made array/loop setup for same
''' - changing default tags for local and char styles to be asymmetrical: `X|tagged item|X`
''' - updated error check for incidental tags to match asymmetric tags
''' - added in 3 combo styles to LocalFind and LocalReplace
''' - added status bar updates
''' - added additional repair to embedded hyperlink, also related to leading spaces (`Q` tag)
''' - update version in Document properties
'''''''''''''
'''version 3.0
'''last updated 6/10/14:
''' - added Style Report Macro Sub
''' - added srErrorCheck Function
'''version2.1 - 5/27/14:
''' - added 7 styles for preserving white space,
''' - preserving superscript & subscript - converting to char styles.
''' - added prelim checks for protected documents, incidental pre-existing backtick tags
''' - consolidated all preliminary error checks into one function
''' - updating char styles to match new prefixes, in style replacements, hyperlink finds, and errorcheck1
''' - fixed field object hyperlink bug
''' - add find/replace for any extra hyperlink tags `H`
''' - removed .Forward = True from all Find/Replaces as it is redundant when wrap = Continue
''' - made all Subs Private except for the Main one
'-----------run preliminary error checks------------
Dim exitOnError As Boolean
exitOnError = zz_errorChecks()
If exitOnError <> False Then
Exit Sub
End If
'-----------Remove White Space------------
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
Call zz_clearFind 'Clear find object
Application.StatusBar = "Fixing quotes, unicode, section breaks": DoEvents
Call RmNonWildcardItems 'has to be alone b/c Match Wildcards has to be disabled: Smart Quotes, Unicode (ellipse), section break
Call zz_clearFind
Application.StatusBar = "Preserving styled white-space pt. 1": DoEvents
Call PreserveWhiteSpaceinBrkStylesA 'Part A tags paragraphs in 3 styles so consecutive ones don't get wiped
Call zz_clearFind
Call StyleHyperlinks 'Styles hyperlinks, must be performed after PreserveWhiteSpaceinBrkStylesA
Call zz_clearFind
Call PreserveStyledCharactersA ' EW added v. 3.2, tags styled page breaks, tabs
Call zz_clearFind
Application.StatusBar = "Removing whitespace, fixing ellipses and dashes": DoEvents
Call RmWhiteSpaceB 'also does ellipses, section breaks
Call zz_clearFind
Application.StatusBar = "Preserving styled white-space pt. 1": DoEvents
Call PreserveStyledCharactersB ' EW added v. 3.2, replaces aracter tags with actual character
Call zz_clearFind
Call PreserveWhiteSpaceinBrkStylesB 'Part B removes the tags and reapplies the styles
Application.ScreenUpdating = True
Application.ScreenRefresh
'-----------Replace Local Styles-----------
Application.ScreenUpdating = False
Call RemoveBookmarks
Call zz_clearFind 'Clear find object
Application.StatusBar = "Tagging character styles and hyperlinks": DoEvents
Call TagExistingCharStyles 'tag existing styled items + hyperlinks, style unstyled hyperlinks
Call zz_clearFind
Application.StatusBar = "Tagging and clearing local styles": DoEvents
Call LocalStyleTag 'tag local styling, reset local styling, remove text highlights
Call zz_clearFind
Application.StatusBar = "Applying Macmillan styles": DoEvents
Call LocalStyleReplace 'reapply local styling through char styles
Call zz_clearFind
Application.ScreenUpdating = True
Application.ScreenRefresh
MsgBox "Hurray, the Macmillan Cleanup macro has finished running! Your manuscript looks great!" 'v. 3.1 patch / request v. 3.2 made a little more fun
End Sub
Private Sub RemoveBookmarks()
Dim bkm As Bookmark
For Each bkm In ActiveDocument.Bookmarks
bkm.Delete
Next bkm
End Sub
Private Sub StyleHyperlinks()
' added by Erica 2014-10-07, v. 3.4
' removes all live hyperlinks but leaves hyperlink text intact
' then styles all URLs as "span hyperlink (url)" style
' -----------------------------------------
' this first bit removes all live hyperlinks from document
' we want to remove these from urls AND text; will add back to just urls later
Set activeRng = ActiveDocument.Range
' remove all embedded hyperlinks regardless of character style style
With activeRng
While .Hyperlinks.Count > 0
.Hyperlinks(1).Delete
Wend
End With
'------------------------------------------
'removes all hyperlink styles
Dim HyperlinkStyleArray(3) As String
Dim p As Long
HyperlinkStyleArray(1) = "Hyperlink" 'built-in style applied automatically to links
HyperlinkStyleArray(2) = "FollowedHyperlink" 'built-in style applied automatically
HyperlinkStyleArray(3) = "span hyperlink (url)" 'Macmillan template style for links
For p = 1 To UBound(HyperlinkStyleArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = HyperlinkStyleArray(p)
.Replacement.Style = ActiveDocument.Styles("Default Paragraph Font")
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
'--------------------------------------------------
' converts all URLs to hyperlinks with Hyperlink style
' because some show up as plain text
' Note this also removes all blank paragraphs regardless of style, so needs to come after sub PreserveWhiteSpaceinBrkA
Dim f1 As Boolean, f2 As Boolean, f3 As Boolean
Dim f4 As Boolean, f5 As Boolean, f6 As Boolean
Dim f7 As Boolean, f8 As Boolean, f9 As Boolean
Dim f10 As Boolean
With Options
' Save current AutoFormat settings
f1 = .AutoFormatApplyHeadings
f2 = .AutoFormatApplyLists
f3 = .AutoFormatApplyBulletedLists
f4 = .AutoFormatApplyOtherParas
f5 = .AutoFormatReplaceQuotes
f6 = .AutoFormatReplaceSymbols
f7 = .AutoFormatReplaceOrdinals
f8 = .AutoFormatReplaceFractions
f9 = .AutoFormatReplacePlainTextEmphasis
f10 = .AutoFormatReplaceHyperlinks
' Only convert URLs
.AutoFormatApplyHeadings = False
.AutoFormatApplyLists = False
.AutoFormatApplyBulletedLists = False
.AutoFormatApplyOtherParas = False
.AutoFormatReplaceQuotes = False
.AutoFormatReplaceSymbols = False
.AutoFormatReplaceOrdinals = False
.AutoFormatReplaceFractions = False
.AutoFormatReplacePlainTextEmphasis = False
.AutoFormatReplaceHyperlinks = True
' Perform AutoFormat
ActiveDocument.Content.AutoFormat
' Restore original AutoFormat settings
.AutoFormatApplyHeadings = f1
.AutoFormatApplyLists = f2
.AutoFormatApplyBulletedLists = f3
.AutoFormatApplyOtherParas = f4
.AutoFormatReplaceQuotes = f5
.AutoFormatReplaceSymbols = f6
.AutoFormatReplaceOrdinals = f7
.AutoFormatReplaceFractions = f8
.AutoFormatReplacePlainTextEmphasis = f9
.AutoFormatReplaceHyperlinks = f10
End With
'--------------------------------------------------
' apply macmillan URL style to hyperlinks we just tagged
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = "Hyperlink"
.Replacement.Style = ActiveDocument.Styles("span hyperlink (url)")
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
' -----------------------------------------------
' Removes all hyperlinks from the document (that were added with AutoFormat)
' Text to display is left intact, macmillan style is left intact
With activeRng
While .Hyperlinks.Count > 0
.Hyperlinks(1).Delete
Wend
End With
End Sub
Private Sub RmNonWildcardItems() 'v. 3.1 patch : redid this whole thing as an array, addedsmart quotes, wrap toggle var
Set activeRng = ActiveDocument.Range
Dim noWildTagArray(3) As String ' number of items in array should be declared here
Dim noWildReplaceArray(3) As String ' number of items in array should be declared here
Dim c As Long
Dim wrapToggle As String
wrapToggle = "wdFindContinue"
Application.Options.AutoFormatAsYouTypeReplaceQuotes = True
noWildTagArray(1) = "^u8230"
noWildTagArray(2) = "^39" 'v. 3.2: EW changed to straight single quote only
noWildTagArray(3) = "^34" 'v. 3.2: EW changed to straight double quote only
noWildReplaceArray(1) = " . . . "
noWildReplaceArray(2) = "'"
noWildReplaceArray(3) = """"
For c = 1 To UBound(noWildTagArray())
If c = 3 Then wrapToggle = "wdFindStop"
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = noWildTagArray(c)
.Replacement.Text = noWildReplaceArray(c)
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub PreserveWhiteSpaceinBrkStylesA()
Set activeRng = ActiveDocument.Range
Dim tagArray(11) As String ' number of items in array should be declared here
Dim StylePreserveArray(11) As String ' number of items in array should be declared here
Dim e As Long
StylePreserveArray(1) = "Space Break (#)"
StylePreserveArray(2) = "Space Break with Ornament (orn)"
StylePreserveArray(3) = "Space Break with ALT Ornament (orn2)"
StylePreserveArray(4) = "Section Break (sbr)"
StylePreserveArray(5) = "Part Start (pts)"
StylePreserveArray(6) = "Part End (pte)"
StylePreserveArray(7) = "Page Break (pb)"
StylePreserveArray(8) = "Space Break - 1-Line (ls1)"
StylePreserveArray(9) = "Space Break - 2-Line (ls2)"
StylePreserveArray(10) = "Space Break - 3-Line (ls3)"
StylePreserveArray(11) = "Column Break (cbr)"
tagArray(1) = "`1`^&`1``" 'v. 3.1 patch added extra backtick on trailing tag for all of these.
tagArray(2) = "`2`^&`2``"
tagArray(3) = "`3`^&`3``"
tagArray(4) = "`4`^&`4``"
tagArray(5) = "`5`^&`5``"
tagArray(6) = "`6`^&`6``"
tagArray(7) = "`7`^&`7``"
tagArray(8) = "`8`^&`8``"
tagArray(9) = "`9`^&`9``"
tagArray(10) = "`0`^&`0``"
tagArray(11) = "`L`^&`L``"
For e = 1 To UBound(StylePreserveArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13"
.Replacement.Text = tagArray(e)
.Wrap = wdFindContinue
.Format = True
.Style = StylePreserveArray(e)
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub PreserveStyledCharactersA()
' added by EW v. 3.2
' replaces correctly styled characters with placeholder so they don't get removed
Set activeRng = ActiveDocument.Range
Dim preserveCharFindArray(4) As String ' declare number of items in array
Dim preserveCharReplaceArray(4) As String 'delcare number of items in array
Dim preserveCharStyleArray(4) As String ' ditto
Dim m As Long
preserveCharFindArray(1) = "^m" 'page breaks
preserveCharFindArray(2) = "^t" 'tabs
preserveCharFindArray(3) = " " ' two spaces
preserveCharFindArray(4) = " " 'three spaces
preserveCharReplaceArray(1) = "`R|"
preserveCharReplaceArray(2) = "`E|"
preserveCharReplaceArray(3) = "`G|"
preserveCharReplaceArray(4) = "`J|"
preserveCharStyleArray(1) = "Page Break (pb)"
preserveCharStyleArray(2) = "span preserve characters (pre)"
preserveCharStyleArray(3) = "span preserve characters (pre)"
preserveCharStyleArray(4) = "span preserve characters (pre)"
For m = 1 To UBound(preserveCharFindArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = preserveCharFindArray(m)
.Replacement.Text = preserveCharReplaceArray(m)
.Wrap = wdFindContinue
.Format = True
.Style = preserveCharStyleArray(m)
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub RmWhiteSpaceB()
Set activeRng = ActiveDocument.Range
Dim wsFindArray(31) As String 'number of items in array should be declared here
Dim wsReplaceArray(31) As String 'and here
Dim i As Long
wsFindArray(1) = ".{4,}" '4 or more consecutive periods, into proper 4 dot ellipse
wsFindArray(2) = "..." '3 consecutive periods, into 3 dot ellipse
wsFindArray(3) = "^s" 'non-breaking space replaecd with space v. 3.1 patch
wsFindArray(4) = "([! ]). . ." 'add leading space for ellipse if not present
wsFindArray(5) = ". . .([! ])" 'add trailing space for ellipse if not present
wsFindArray(6) = "^t{1,}" 'tabs deleted
wsFindArray(7) = "^l{1,}" 'manual line breaks
wsFindArray(8) = "^m{1,}" 'manual page breaks
wsFindArray(9) = " {2,}" '2 or more spaces
wsFindArray(10) = "^13 " 'paragraph, space
wsFindArray(11) = " ^13" 'space, paragraph
wsFindArray(12) = "^13{2,}" '2 or more paragraphs
wsFindArray(13) = "^-" 'optional hyphen deleted v. 3.1 patch
wsFindArray(14) = "^~" 'non-breaking hyphen replaced with reg hyphen v. 3.1 patch
wsFindArray(15) = " ^= " 'endash w/ spaces convert to emdash (no spaces) v. 3.1 patch
wsFindArray(16) = "---" '3 hyphens to emdash
wsFindArray(17) = "--" '2 hyphens to endash
wsFindArray(18) = " -" 'hyphen leading space-remove
wsFindArray(19) = "- " 'hyphen trailing space-remove
wsFindArray(20) = " ^+" 'emdash leading space-remove
wsFindArray(21) = "^+ " 'emdash trailing space-remove
wsFindArray(22) = " ^=" 'endash leading space-remove
wsFindArray(23) = "^= " 'endash trailing space-remove
wsFindArray(24) = "(`[0-9]``)^13" 'remove para following a preserved break style v. 3.1 patch
wsFindArray(25) = "\( " ' space after open parens v. 3.3
wsFindArray(26) = " \)" 'space before closing parens v. 3.3
wsFindArray(27) = "\[ " 'space after opening bracket v. 3.3
wsFindArray(28) = " \]" 'space before closing bracket v. 3.3
wsFindArray(29) = "\{ " 'space after opening curly bracket v. 3.3
wsFindArray(30) = " \}" 'space before closing curly bracket v. 3.3
wsFindArray(31) = "$ " 'space after dollar sign v. 3.3
wsReplaceArray(1) = ". . . . " ' v. 3.2 EW removed leading space--not needed, 1st dot is a period
wsReplaceArray(2) = " . . . "
wsReplaceArray(3) = " "
wsReplaceArray(4) = "\1 . . ."
wsReplaceArray(5) = ". . . \1"
wsReplaceArray(6) = ""
wsReplaceArray(7) = "^p"
wsReplaceArray(8) = "^p"
wsReplaceArray(9) = " "
wsReplaceArray(10) = "^p"
wsReplaceArray(11) = "^p"
wsReplaceArray(12) = "^p"
wsReplaceArray(13) = ""
wsReplaceArray(14) = "-"
wsReplaceArray(15) = "^+"
wsReplaceArray(16) = "^+"
wsReplaceArray(17) = "^="
wsReplaceArray(18) = "-"
wsReplaceArray(19) = "-"
wsReplaceArray(20) = "^+"
wsReplaceArray(21) = "^+"
wsReplaceArray(22) = "^="
wsReplaceArray(23) = "^="
wsReplaceArray(24) = "\1"
wsReplaceArray(25) = "("
wsReplaceArray(26) = ")"
wsReplaceArray(27) = "["
wsReplaceArray(28) = "]"
wsReplaceArray(29) = "{"
wsReplaceArray(30) = "}"
wsReplaceArray(31) = "$"
For i = 1 To UBound(wsFindArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = wsFindArray(i)
.Replacement.Text = wsReplaceArray(i)
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub PreserveStyledCharactersB()
' added by EW v. 3.2
' replaces placeholders with original characters
Set activeRng = ActiveDocument.Range
Dim preserveCharFindArray(4) As String ' declare number of items in array
Dim preserveCharReplaceArray(4) As String 'declare number of items in array
Dim preserveCharStyleArray(4) As String ' ditto
Dim n As Long
preserveCharFindArray(1) = "`R|" 'page breaks
preserveCharFindArray(2) = "`E|" 'tabs
preserveCharFindArray(3) = "`G|" ' two spaces
preserveCharFindArray(4) = "`J|`" 'three spaces
preserveCharReplaceArray(1) = "^m"
preserveCharReplaceArray(2) = "^t"
preserveCharReplaceArray(3) = " "
preserveCharReplaceArray(3) = " "
preserveCharStyleArray(1) = "Page Break (pb)"
preserveCharStyleArray(2) = "span preserve characters (pre)"
preserveCharStyleArray(3) = "span preserve characters (pre)"
preserveCharStyleArray(4) = "span preserve characters (pre)"
For n = 1 To UBound(preserveCharFindArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = preserveCharFindArray(n)
.Replacement.Text = preserveCharReplaceArray(n)
.Wrap = wdFindContinue
.Format = True
.Style = preserveCharStyleArray(n)
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub PreserveWhiteSpaceinBrkStylesB()
Set activeRng = ActiveDocument.Range
Dim tagArrayB(11) As String ' number of items in array should be declared here
Dim f As Long
tagArrayB(1) = "`1`(^13)`1``" 'v. 3.1 patch added extra backtick on trailing tag for all of these.
tagArrayB(2) = "`2`(^13)`2``"
tagArrayB(3) = "`3`(^13)`3``"
tagArrayB(4) = "`4`(^13)`4``"
tagArrayB(5) = "`5`(^13)`5``"
tagArrayB(6) = "`6`(^13)`6``"
tagArrayB(7) = "`7`(^13)`7``"
tagArrayB(8) = "`8`(^13)`8``"
tagArrayB(9) = "`9`(^13)`9``"
tagArrayB(10) = "`0`(^13)`0``"
tagArrayB(11) = "`L`(^13)`L``" ' for new column break, added v. 3.4.1
For f = 1 To UBound(tagArrayB())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = tagArrayB(f)
.Replacement.Text = "\1"
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub TagExistingCharStyles()
Set activeRng = ActiveDocument.Range 'this whole sub (except last stanza) is basically a v. 3.1 patch. correspondingly updated sub name, call in main, and replacements go along with bold and common replacements
Dim tagCharStylesArray(8) As String ' number of items in array should be declared here
Dim CharStylePreserveArray(8) As String ' number of items in array should be declared here
Dim d As Long
CharStylePreserveArray(1) = "span hyperlink (url)"
CharStylePreserveArray(2) = "span symbols (sym)"
CharStylePreserveArray(3) = "span accent characters (acc)"
CharStylePreserveArray(4) = "span cross-reference (xref)"
CharStylePreserveArray(5) = "span material to come (tk)"
CharStylePreserveArray(6) = "span carry query (cq)"
CharStylePreserveArray(7) = "span key phrase (kp)"
CharStylePreserveArray(8) = "span preserve characters (pre)" 'added v. 3.2
tagCharStylesArray(1) = "`H|^&|H`"
tagCharStylesArray(2) = "`Z|^&|Z`"
tagCharStylesArray(3) = "`Y|^&|Y`"
tagCharStylesArray(4) = "`X|^&|X`"
tagCharStylesArray(5) = "`W|^&|W`"
tagCharStylesArray(6) = "`V|^&|V`"
tagCharStylesArray(7) = "`T|^&|T`"
tagCharStylesArray(8) = "`F|^&|F`"
For d = 1 To UBound(CharStylePreserveArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = tagCharStylesArray(d)
.Wrap = wdFindContinue
.Format = True
.Style = CharStylePreserveArray(d)
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub LocalStyleTag()
Set activeRng = ActiveDocument.Range
'------------tag key styles
Dim tagStyleFindArray(10) As Boolean ' number of items in array should be declared here
Dim tagStyleReplaceArray(10) As String 'and here
Dim g As Long
tagStyleFindArray(1) = False 'Bold
tagStyleFindArray(2) = False 'Italic
tagStyleFindArray(3) = False 'Underline
tagStyleFindArray(4) = False 'Smallcaps
tagStyleFindArray(5) = False 'Subscript
tagStyleFindArray(6) = False 'Superscript
tagStyleFindArray(7) = False 'Highlights v. 3.1 update
tagStyleReplaceArray(1) = "`B|^&|B`"
tagStyleReplaceArray(2) = "`I|^&|I`"
tagStyleReplaceArray(3) = "`U|^&|U`"
tagStyleReplaceArray(4) = "`M|^&|M`"
tagStyleReplaceArray(5) = "`S|^&|S`"
tagStyleReplaceArray(6) = "`P|^&|P`"
tagStyleReplaceArray(8) = "`A|^&|A`"
tagStyleReplaceArray(9) = "`C|^&|C`"
tagStyleReplaceArray(10) = "`D|^&|D`"
For g = 1 To UBound(tagStyleFindArray())
tagStyleFindArray(g) = True
If tagStyleFindArray(8) = True Then tagStyleFindArray(1) = True: tagStyleFindArray(2) = True 'bold and italic v. 3.1 update
If tagStyleFindArray(9) = True Then tagStyleFindArray(1) = True: tagStyleFindArray(4) = True: tagStyleFindArray(2) = False 'bold and smallcaps v. 3.1 update
If tagStyleFindArray(10) = True Then tagStyleFindArray(2) = True: tagStyleFindArray(4) = True: tagStyleFindArray(1) = False 'smallcaps and italic v. 3.1 update
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = tagStyleReplaceArray(g)
.Wrap = wdFindContinue
.Format = True
.Font.Bold = tagStyleFindArray(1)
.Font.Italic = tagStyleFindArray(2)
.Font.Underline = tagStyleFindArray(3)
.Font.SmallCaps = tagStyleFindArray(4)
.Font.Subscript = tagStyleFindArray(5)
.Font.Superscript = tagStyleFindArray(6)
.Highlight = tagStyleFindArray(7) ' v. 3.1 update
.Replacement.Highlight = False ' v. 3.1 update
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
tagStyleFindArray(g) = False
Next
'-------------Reset everything
activeRng.Font.Reset
End Sub
Private Sub LocalStyleReplace()
Set activeRng = ActiveDocument.Range
'-------------apply styles to tags
Dim tagFindArray(17) As String ' number of items in array should be declared here
Dim tagReplaceArray(17) As String 'and here
Dim h As Long
tagFindArray(1) = "`B|(*)|B`"
tagFindArray(2) = "`I|(*)|I`"
tagFindArray(3) = "`U|(*)|U`"
tagFindArray(4) = "`M|(*)|M`"
tagFindArray(5) = "`H|(*)|H`"
tagFindArray(6) = "`S|(*)|S`"
tagFindArray(7) = "`P|(*)|P`"
tagFindArray(8) = "`Z|(*)|Z`"
tagFindArray(9) = "`Y|(*)|Y`"
tagFindArray(10) = "`X|(*)|X`"
tagFindArray(11) = "`W|(*)|W`"
tagFindArray(12) = "`V|(*)|V`"
tagFindArray(13) = "`T|(*)|T`"
tagFindArray(14) = "`A|(*)|A`" 'v. 3.1 patch
tagFindArray(15) = "`C|(*)|C`" 'v. 3.1 patch
tagFindArray(16) = "`D|(*)|D`" 'v. 3.1 patch
tagFindArray(17) = "`F|(*)|F`"
tagReplaceArray(1) = ActiveDocument.Styles("span boldface characters (bf)")
tagReplaceArray(2) = ActiveDocument.Styles("span italic characters (ital)")
tagReplaceArray(3) = ActiveDocument.Styles("span underscore characters (us)")
tagReplaceArray(4) = ActiveDocument.Styles("span small caps characters (sc)")
tagReplaceArray(5) = ActiveDocument.Styles("span hyperlink (url)")
tagReplaceArray(6) = ActiveDocument.Styles("span subscript characters (sub)")
tagReplaceArray(7) = ActiveDocument.Styles("span superscript characters (sup)")
tagReplaceArray(8) = ActiveDocument.Styles("span symbols (sym)")
' the last 9 items here are of course v. 3.1 patches
tagReplaceArray(9) = ActiveDocument.Styles("span accent characters (acc)")
tagReplaceArray(10) = ActiveDocument.Styles("span cross-reference (xref)")
tagReplaceArray(11) = ActiveDocument.Styles("span material to come (tk)")
tagReplaceArray(12) = ActiveDocument.Styles("span carry query (cq)")
tagReplaceArray(13) = ActiveDocument.Styles("span key phrase (kp)")
tagReplaceArray(14) = ActiveDocument.Styles("span bold ital (bem)")
tagReplaceArray(15) = ActiveDocument.Styles("span smcap bold (scbold)")
tagReplaceArray(16) = ActiveDocument.Styles("span smcap ital (scital)")
tagReplaceArray(17) = ActiveDocument.Styles("span preserve characters (pre)")
For h = 1 To UBound(tagFindArray())
With activeRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = tagFindArray(h)
.Replacement.Text = "\1"
.Wrap = wdFindContinue
.Format = True
.Replacement.Style = tagReplaceArray(h)
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End Sub
Private Sub zz_clearFind()
Dim clearRng As Range
Set clearRng = ActiveDocument.Words.First
With clearRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
Function zz_errorChecks()
zz_errorChecks = False
Dim mainDoc As Document
Set mainDoc = ActiveDocument
Dim iReply As Integer
'-----make sure Style template is attached
Dim keyStyle As Word.Style
Dim styleCheck As Boolean
On Error Resume Next
Set keyStyle = mainDoc.Styles("span boldface characters (bf)") '''Style from template to check against
styleCheck = keyStyle Is Nothing
If styleCheck Then
MsgBox "Oops! Required style template 'macmillan.dotx' is not attached.", , "Error"
zz_errorChecks = True
Exit Function
End If
'-----make sure document is saved
Dim docSaved As Boolean 'v. 3.1 update
docSaved = mainDoc.Saved
If docSaved = False Then
iReply = MsgBox("Your document '" & mainDoc & "' contains unsaved changes." & vbNewLine & vbNewLine & _
"Click OK and I will save the document and run the Cleanup macro." & vbNewLine & vbNewLine & "Click 'Cancel' to exit.", vbOKCancel, "Alert")
If iReply = vbOK Then
mainDoc.Save
Else
zz_errorChecks = True
Exit Function
End If
End If
'-----test protection
If ActiveDocument.ProtectionType <> wdNoProtection Then
MsgBox "Uh oh ... protection is enabled on document '" & mainDoc & "'." & vbNewLine & "Please unprotect the document and run the macro again." & vbNewLine & vbNewLine & "TIP: If you don't know the protection password, try pasting contents of this file into a new file, and run the macro on that.", , "Error 2"
zz_errorChecks = True
Exit Function
End If
'-----test if backtick style tag already exists
Set activeRng = ActiveDocument.Range
Application.ScreenUpdating = False
Dim existingTagArray(3) As String ' number of items in array should be declared here
Dim b As Long
Dim foundBad As Boolean
foundBad = False
existingTagArray(1) = "`[0-9]`"
existingTagArray(2) = "`[A-Z]|"
existingTagArray(3) = "|[A-Z]`"
For b = 1 To UBound(existingTagArray())
With activeRng.Find
.ClearFormatting
.Text = existingTagArray(b)
.Wrap = wdFindContinue
.MatchWildcards = True
End With
If activeRng.Find.Execute Then foundBad = True: Exit For
Next
Application.ScreenUpdating = True
Application.ScreenRefresh
If foundBad = True Then 'If activeRng.Find.Execute Then
MsgBox "Something went wrong! The Cleanup Macro cannot be run on Document:" & vbNewLine & "'" & mainDoc & "'" & vbNewLine & vbNewLine & "Please contact Digital Workflow group for support, I am sure they will be happy to help.", , "Error Code: 1"
zz_errorChecks = True
End If
End Function
Sub MacmillanStyleReport()
'-----------run preliminary error checks------------
Dim exitOnError As Boolean
exitOnError = srErrorCheck()
If exitOnError <> False Then
Exit Sub
End If
''''''''''''''''''''''
''Timer opening
'Dim aTime As Double, bTime As Double
'aTime = Timer
'''''''''''''''''''''
Dim activeDoc As Document
Set activeDoc = ActiveDocument
Dim stylesGood() As String
Dim stylesGoodLong As Long
stylesGoodLong = 400 'could maybe reduce this number
ReDim stylesGood(stylesGoodLong)
Dim stylesBad(100) As String 'could maybe reduce this number too
Dim styleGoodCount As Integer
Dim styleBadCount As Integer
Dim styleBadOverflow As Boolean
Dim activeParaCount As Integer
Dim J As Integer, K As Integer, L As Integer
Dim paraStyle As String
'''''''''''''''''''''
Dim activeParaRange As Range
Dim pageNumber As Integer
Dim activeDocName As String
Dim activeDocPath As String
Dim styleReportDoc As String
Dim fnum As Integer
Dim TheOS As String
TheOS = System.OperatingSystem
activeDocName = Left(activeDoc.Name, InStrRev(activeDoc.Name, ".doc") - 1)
activeDocPath = Replace(activeDoc.Path, activeDoc.Name, "")
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
'Alter built-in Normal (Web) style temporarily (later, maybe forever?)
ActiveDocument.Styles("Normal (Web)").NameLocal = "_"
' Collect all styles being used
styleGoodCount = 0
styleBadCount = 0
styleBadOverflow = False
activeParaCount = activeDoc.Paragraphs.Count
For J = 1 To activeParaCount
'Next two lines are for the status bar
Application.StatusBar = "Checking paragraph: " & J & " of " & activeParaCount
If J Mod 100 = 0 Then DoEvents
paraStyle = activeDoc.Paragraphs(J).Style
'If InStrRev(paraStyle, ")", -1, vbTextCompare) Then 'ALT calculation to "Right", can speed test
If Right(paraStyle, 1) = ")" Then
For K = 1 To styleGoodCount
If paraStyle = stylesGood(K) Then 'stylereport bug fix #1 v. 3.1
K = styleGoodCount 'stylereport bug fix #1 v. 3.1
Exit For 'stylereport bug fix #1 v. 3.1
End If 'stylereport bug fix #1 v. 3.1
Next K
If K = styleGoodCount + 1 Then
styleGoodCount = K
stylesGood(styleGoodCount) = paraStyle
End If
Else
For L = 1 To styleBadCount
'If paraStyle = stylesBad(L) Then Exit For 'Not needed, since we want EVERY instance of bad style
Next L
If L > 100 Then
styleBadOverflow = True
Exit For
End If
If L = styleBadCount + 1 Then
styleBadCount = L
Set activeParaRange = ActiveDocument.Paragraphs(J).Range
pageNumber = activeParaRange.Information(wdActiveEndPageNumber) 'alt: (wdActiveEndAdjustedPageNumber)
stylesBad(styleBadCount) = "Page " & pageNumber & " (Paragraph " & J & "): " & vbTab & paraStyle
End If
End If
Next J
'Change Normal (Web) back (if you want to)
ActiveDocument.Styles("Normal (Web),_").NameLocal = "Normal (Web)"
'Sort good styles
If K <> 0 Then
ReDim Preserve stylesGood(K)
WordBasic.SortArray stylesGood()
End If
'create text file
styleReportDoc = activeDocPath & activeDocName & "_StyleReport.txt"
''''for 32 char Mc OS bug- could check if this is Mac OS too< PART 1
If Not TheOS Like "*Mac*" Then 'If Len(activeDocName) > 18 Then (legacy, does not take path into account)
styleReportDoc = activeDocPath & "\" & activeDocName & "_StyleReport.txt"
Else
Dim styleReportDocAlt As String
Dim placeholdDocName As String
placeholdDocName = "filenamePlacehold_Styleport.txt"
styleReportDocAlt = styleReportDoc
styleReportDoc = "Macintosh HD:private:tmp:" & placeholdDocName
End If
'set and open file for output
fnum = FreeFile()
Open styleReportDoc For Output As fnum
Print #fnum, "-----" & styleGoodCount & " Macmillan Styles In Use: -----" '"----- Good Styles In Use: -----"
For J = 1 To styleGoodCount
Print #fnum, stylesGood(J)
Next J
Print #fnum, vbCr
Print #fnum, vbCr
If styleBadCount <> 0 Then
Print #fnum, "----- " & styleBadCount & " PARAGRAPHS WITH BAD STYLES FOUND: ----- " & vbCr
Print #fnum, "(Please apply Macmillan styles to the following paragraphs:)",
Print #fnum, vbCr
For J = 1 To styleBadCount
Print #fnum, stylesBad(J)
Next J
Else
Print #fnum, "----- great job! no bad paragraph styles found ----- "
End If
Close #fnum
Application.ScreenUpdating = True
Application.ScreenRefresh
''''for 32 char Mc OS bug-<PART 2
If styleReportDocAlt <> "" Then
Name styleReportDoc As styleReportDocAlt
End If
If styleBadOverflow = True Then
MsgBox "Macmillan Style Report has finished running." & vbCr & "PLEASE NOTE: more than 100 paragraphs have non-Macmillan styles." & vbCr & "Only the first 100 are shown in the Style report.", , "Alert"
Else
MsgBox "The Macmillan Style Report macro has finished running. Go take a look at the results!"
End If
''Timer closing
'bTime = Timer
'MsgBox "CreateStyleListEBranch: " & Format(Round(bTime - aTime, 2), "00:00:00") & " for " & activeParaCount & " paragraphs"
End Sub
Function srErrorCheck()
srErrorCheck = False
Dim mainDoc As Document
Set mainDoc = ActiveDocument