View difference between Paste ID: jpJQUpDK and XviWWFZh
SHOW: | | - or go back to the newest paste.
1
Sub LayoutPrep()
2
'
3
'  Macro written by DSpider, around mid 2012.
4
'
5
'  What this does, is it breaks down a Word document into a squeaky clean document,
6
'  with its basic formatting intact, ready for re-applying styles or quick styles
7
'  in Word, or in Adobe InDesign.
8
'
9
'    - Bold text
10
'    - Italic text
11
'    - Underlined text
12
'    - Subscripts
13
'    - Superscripts
14
'    - Paragraph ending marks
15
'    - Line Breaks
16
'    - Page Breaks
17
'
18
'
19
'  Set the desktop variable and the working directory for everything else.
20
'
21
    Dim WshShell As Object
22
    Dim SpecialPath As String
23
    Set WshShell = CreateObject("WScript.Shell")
24
    DESKTOP = WshShell.SpecialFolders("Desktop")
25
    ChangeFileOpenDirectory DESKTOP
26
'
27
'  1. Convert numbered and bulleted lists to regular text. Honestly, they're more
28
'     of a nuisance with scanned text and OCR. Without this they would not make it
29
'     to the second part because Word treats them (along with footnotes) more like
30
'     objects.
31
'
32
    ActiveDocument.ConvertNumbersToText
33
'
34
'
35
'  2. Convert footnotes to regular text so that they too make it intact.
36
'
37
   Dim afootnote As Footnote
38
   Dim NumberOfFootnotes As Integer
39
   Dim i As Integer
40
   Dim aFootnoteReference As String
41
   Dim aFootnoteRefTag As String
42
 
43
 NumberOfFootnotes = ActiveDocument.Footnotes.Count
44
 For i = NumberOfFootnotes To 1 Step -1
45
    Set afootnote = ActiveDocument.Footnotes(i)
46
    afootnote.Range.Select
47
    Selection.MoveStartWhile Cset:=" " & Chr(9)
48
    Selection.Cut
49
    aFootnoteReference = afootnote.Reference.Text
50
    Select Case aFootnoteReference
51
    Case Chr(2)
52
    aFootnoteRefTag = "num"
53
    Case "*"
54
    aFootnoteRefTag = "star"
55
    Case Else
56
    aFootnoteRefTag = "symbol" _
57
    & aFootnoteReference & "/FNRef"
58
 End Select
59
 afootnote.Reference.Select
60
 If afootnote.Reference.Text = Chr(40) Then
61
    With Dialogs(wdDialogInsertSymbol)
62
        aFootnoteRefTag = _
63
        "FNSym," & .Font & "," _
64
        & .CharNum & ""
65
    End With
66
 End If
67
 afootnote.Delete
68
 Selection.InsertBefore ChrW(9616) _
69
 & aFootnoteRefTag
70
 Selection.Collapse (wdCollapseEnd)
71
 Selection.Paste
72
 Selection.InsertAfter ChrW(9612)
73
 Next i
74
'
75
'
76
'  3. Remove all the tab characters by replacing them with the space character.
77
'     This is because FineReader sometimes adds multiple tabs when you only need
78
'     one. Easier to spot too when you're assigning styles.
79
'
80
    Selection.Find.ClearFormatting
81
    Selection.Find.Replacement.ClearFormatting
82
    With Selection.Find
83
        .Text = "^t"
84
        .Replacement.Text = " "
85
        .Forward = True
86
        .Wrap = wdFindContinue
87
        .Format = False
88
        .MatchCase = False
89
        .MatchWholeWord = False
90
        .MatchWildcards = False
91
        .MatchSoundsLike = False
92
        .MatchAllWordForms = False
93
    End With
94
    Selection.Find.Execute Replace:=wdReplaceAll
95
'
96
'
97
'  4. Paragraph-level formatting: Delete the ruler tabs.
98
'
99
    Selection.WholeStory
100
    With Selection.ParagraphFormat
101
        .SpaceBeforeAuto = False
102
        .SpaceAfterAuto = False
103
    End With
104
    Selection.ParagraphFormat.TabStops.ClearAll
105
    ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
106
'
107
'
108
'  5. Paragraph-level formatting: Align everything to the left.
109
'
110
    With Selection.ParagraphFormat
111
        .LeftIndent = InchesToPoints(0)
112
        .RightIndent = InchesToPoints(0)
113
        .SpaceBefore = 0
114
        .SpaceBeforeAuto = False
115
        .SpaceAfter = 0
116
        .SpaceAfterAuto = False
117
        .LineSpacingRule = wdLineSpaceSingle
118
        .Alignment = wdAlignParagraphLeft
119
        .FirstLineIndent = InchesToPoints(0)
120
        .OutlineLevel = wdOutlineLevelBodyText
121
        .CharacterUnitLeftIndent = 0
122
        .CharacterUnitRightIndent = 0
123
        .CharacterUnitFirstLineIndent = 0
124
        .LineUnitBefore = 0
125
        .LineUnitAfter = 0
126
    End With
127
'
128
'
129
'  6. Replace the line break character (but keep the paragraph ending marks or
130
'     else the process would get too slow), then replace the Page Break and
131
'     Section Break.
132
'
133
With ActiveDocument.Content.Find
134
    .ClearFormatting
135
    .Replacement.ClearFormatting
136
    .Forward = True
137
    .Wrap = wdFindContinue
138
    .Format = False
139
    .MatchCase = False
140
    .MatchWholeWord = False
141
    .MatchAllWordForms = False
142
    .MatchSoundsLike = False
143
    .MatchWildcards = False
144
    .Text = "^l"
145
    .Replacement.Text = ChrW(9668)
146
    .Execute Replace:=wdReplaceAll
147
    .Text = "^m"
148
    .Replacement.Text = ChrW(9618)
149
    .Execute Replace:=wdReplaceAll
150
    .Text = "^b"
151
    .Replacement.Text = ChrW(9618)
152
    .Execute Replace:=wdReplaceAll
153
End With
154
'
155
'
156
'  7. Italic characters.
157
'
158
    Selection.Find.ClearFormatting
159
    Selection.Find.Font.Italic = True
160
    Selection.Find.Replacement.ClearFormatting
161
    With Selection.Find
162
        .Text = "(?)"
163
        .Replacement.Text = ChrW(9500) & "\1" & ChrW(9508)
164
        .Forward = True
165
        .Wrap = wdFindContinue
166
        .Format = True
167
        .MatchCase = False
168
        .MatchWholeWord = False
169
        .MatchAllWordForms = False
170
        .MatchSoundsLike = False
171
        .MatchWildcards = True
172
    End With
173
    Selection.Find.Execute Replace:=wdReplaceAll
174
'
175
'
176
'  8. Bold characters.
177
'
178
    Selection.Find.ClearFormatting
179
    Selection.Find.Font.Bold = True
180
    Selection.Find.Replacement.ClearFormatting
181
    With Selection.Find
182
        .Text = "(?)"
183
        .Replacement.Text = ChrW(9568) & "\1" & ChrW(9571)
184
        .Forward = True
185
        .Wrap = wdFindContinue
186
        .Format = True
187
        .MatchCase = False
188
        .MatchWholeWord = False
189
        .MatchAllWordForms = False
190
        .MatchSoundsLike = False
191
        .MatchWildcards = True
192
    End With
193
    Selection.Find.Execute Replace:=wdReplaceAll
194
'
195
'
196
'  9.  Underlined characters.
197
'
198
    Selection.Find.ClearFormatting
199
    Selection.Find.Font.Underline = wdUnderlineSingle
200
    Selection.Find.Replacement.ClearFormatting
201
    With Selection.Find
202
        .Text = "(?)"
203
        .Replacement.Text = ChrW(9556) & "\1" & ChrW(9559)
204
        .Forward = True
205
        .Wrap = wdFindContinue
206
        .Format = True
207
        .MatchCase = False
208
        .MatchWholeWord = False
209
        .MatchAllWordForms = False
210
        .MatchSoundsLike = False
211
        .MatchWildcards = True
212
    End With
213
    Selection.Find.Execute Replace:=wdReplaceAll
214
'
215
'
216
'  10. Superscripts.
217
'
218
    Selection.Find.ClearFormatting
219
    With Selection.Find.Font
220
        .Superscript = True
221
        .Subscript = False
222
    End With
223
    Selection.Find.Replacement.ClearFormatting
224
    With Selection.Find
225
        .Text = "(?)"
226
        .Replacement.Text = ChrW(9560) & "\1" & ChrW(9563)
227
        .Forward = True
228
        .Wrap = wdFindContinue
229
        .Format = True
230
        .MatchCase = False
231
        .MatchWholeWord = False
232
        .MatchAllWordForms = False
233
        .MatchSoundsLike = False
234
        .MatchWildcards = True
235
    End With
236
    Selection.Find.Execute Replace:=wdReplaceAll
237
'
238
'
239
'  11. Subscripts.
240
'
241
    Selection.Find.ClearFormatting
242
    With Selection.Find.Font
243
        .Superscript = False
244
        .Subscript = True
245
    End With
246
    Selection.Find.Replacement.ClearFormatting
247
    With Selection.Find
248
        .Text = "(?)"
249
        .Replacement.Text = ChrW(9554) & "\1" & ChrW(9557)
250
        .Forward = True
251
        .Wrap = wdFindContinue
252
        .Format = True
253
        .MatchCase = False
254
        .MatchWholeWord = False
255
        .MatchAllWordForms = False
256
        .MatchSoundsLike = False
257
        .MatchWildcards = True
258
    End With
259
    Selection.Find.Execute Replace:=wdReplaceAll
260
'
261
'
262
'  12. Save to the desktop as 'Plain Text.txt' and close the file.
263
'
264
    ActiveDocument.SaveAs2 FileName:="Plain Text.txt", FileFormat:= _
265
        wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, _
266
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
267
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
268
        False, Encoding:=1200, InsertLineBreaks:=False, AllowSubstitutions:=False _
269
        , LineEnding:=wdCRLF, CompatibilityMode:=0
270
    ActiveDocument.Close
271
'
272
'
273
' --------------------------
274
'  PART II
275
' --------------------------
276
'
277
' Open the broken down text, to be able to restore the formatting.
278
'
279
    Documents.Open FileName:="Plain Text.txt", ConfirmConversions:=False, _
280
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
281
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
282
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:="", _
283
        Encoding:=1200
284
'
285
'
286
'  1. Delete the useless "Plain Text" style. There's no such thing as plain text
287
'     in Word. Everything has some kind of formatting applied to it.
288
'
289
    ActiveDocument.Styles("Plain Text").Delete
290
'
291
'
292
'  2. Remove all quick styles for the document (default Word templates are intact).
293
'
294
    Dim s As Style
295
    For Each s In ActiveDocument.Styles
296
      If s.Type = wdStyleTypeCharacter Or _
297
         s.Type = wdStyleTypeParagraph Or _
298
         s.Type = wdStyleTypeLinked Then
299
         s.QuickStyle = False
300
      End If
301
    Next s
302
'
303
'
304
'  3.  Create a "Text" style and apply it to the document because "Normal" is too
305
'      mainstream. For various adjustments you can add it to the Quick Styles menu.
306
'
307
    ActiveDocument.Styles.Add Name:="Text", Type:=wdStyleTypeParagraph
308
    ActiveDocument.Content.Style = ActiveDocument.Styles("Text")
309
        With ActiveDocument.Styles("Text").Font
310
        .Name = "Times New Roman"
311
        .Size = 10
312
        .Bold = False
313
        .Italic = False
314
        .Underline = wdUnderlineNone
315
        .UnderlineColor = wdColorAutomatic
316
        .StrikeThrough = False
317
        .DoubleStrikeThrough = False
318
        .Outline = False
319
        .Emboss = False
320
        .Shadow = False
321
        .Hidden = False
322
        .SmallCaps = False
323
        .AllCaps = False
324
        .Color = wdColorAutomatic
325
        .Engrave = False
326
        .Superscript = False
327
        .Subscript = False
328
        .Scaling = 100
329
        .Kerning = 0
330
        .Animation = wdAnimationNone
331
        .Ligatures = wdLigaturesNone
332
        .NumberSpacing = wdNumberSpacingDefault
333
        .NumberForm = wdNumberFormDefault
334
        .StylisticSet = wdStylisticSetDefault
335
        .ContextualAlternates = 0
336
    End With
337
    With ActiveDocument.Styles("Text")
338
        .AutomaticallyUpdate = False
339
        .BaseStyle = ""
340
        .NextParagraphStyle = "Text"
341
    End With
342
'
343
'
344
'  4. Restore subscripts.
345
'
346
    Selection.Find.ClearFormatting
347
    Selection.Find.Replacement.ClearFormatting
348
    With Selection.Find.Replacement.Font
349
        .Superscript = False
350
        .Subscript = True
351
    End With
352
    With Selection.Find
353
        .Text = ChrW(9554) & "(?)" & ChrW(9557)
354
        .Replacement.Text = "\1"
355
        .Forward = True
356
        .Wrap = wdFindContinue
357
        .Format = True
358
        .MatchCase = False
359
        .MatchWholeWord = False
360
        .MatchAllWordForms = False
361
        .MatchSoundsLike = False
362
        .MatchWildcards = True
363
    End With
364
    Selection.Find.Execute Replace:=wdReplaceAll
365
'
366
'
367
'  5. Restore superscripts.
368
'
369
    Selection.Find.ClearFormatting
370
    Selection.Find.Replacement.ClearFormatting
371
    With Selection.Find.Replacement.Font
372
        .Superscript = True
373
        .Subscript = False
374
    End With
375
    With Selection.Find
376
        .Text = ChrW(9560) & "(?)" & ChrW(9563)
377
        .Replacement.Text = "\1"
378
        .Forward = True
379
        .Wrap = wdFindContinue
380
        .Format = True
381
        .MatchCase = False
382
        .MatchWholeWord = False
383
        .MatchAllWordForms = False
384
        .MatchSoundsLike = False
385
        .MatchWildcards = True
386
    End With
387
    Selection.Find.Execute Replace:=wdReplaceAll
388
'
389
'
390
'  6. Replace paragraph endings temporarily so that they too can receive bold and
391
'     italic attributes. It's a little difficult to explain but just know that it's
392
'     needed... For example if you have a block of text with italic (or bold)
393
'     attributes, the paragraph ending marks (¶) will not receive the attribute and
394
'     every line will have separate tags instead of treating it like a whole.
395
'
396
'     This is the reason the "decoding" process takes such a long time. Because you
397
'     basically search and replace throughout a SINGLE 1+ MB paragraph (depending
398
'     on the complexity of the book.
399
'
400
    With ActiveDocument.Content.Find
401
        .ClearFormatting
402
        .Replacement.ClearFormatting
403
        .Forward = True
404
        .Wrap = wdFindContinue
405
        .Format = False
406
        .MatchCase = False
407
        .MatchWholeWord = False
408
        .MatchAllWordForms = False
409
        .MatchSoundsLike = False
410
        .MatchWildcards = False
411
        .Text = "^p"
412
        .Replacement.Text = ChrW(9608)
413
        .Execute Replace:=wdReplaceAll
414
    End With
415
'
416
'
417
'  7. Restore underlined characters.
418
'
419
    Selection.Find.ClearFormatting
420
    Selection.Find.Replacement.ClearFormatting
421
    Selection.Find.Replacement.Font.Underline = wdUnderlineSingle
422
    With Selection.Find
423
        .Text = ChrW(9556) & "(?)" & ChrW(9559)
424
        .Replacement.Text = "\1"
425
        .Forward = True
426
        .Wrap = wdFindContinue
427
        .Format = True
428
        .MatchCase = False
429
        .MatchWholeWord = False
430
        .MatchAllWordForms = False
431
        .MatchSoundsLike = False
432
        .MatchWildcards = True
433
    End With
434
    Selection.Find.Execute Replace:=wdReplaceAll
435
'
436
'
437
'  8. Restore bold characters.
438
'
439
    Selection.Find.ClearFormatting
440
    Selection.Find.Replacement.ClearFormatting
441
    Selection.Find.Replacement.Font.Bold = True
442
    With Selection.Find
443
        .Text = ChrW(9568) & "(?)" & ChrW(9571)
444
        .Replacement.Text = "\1"
445
        .Forward = True
446
        .Wrap = wdFindContinue
447
        .Format = True
448
        .MatchCase = False
449
        .MatchWholeWord = False
450
        .MatchAllWordForms = False
451
        .MatchSoundsLike = False
452
        .MatchWildcards = True
453
    End With
454
    Selection.Find.Execute Replace:=wdReplaceAll
455
'
456
'
457
'  9. Restore italic characters.
458
'
459
    Selection.Find.ClearFormatting
460
    Selection.Find.Replacement.ClearFormatting
461
    Selection.Find.Replacement.Font.Italic = True
462
    With Selection.Find
463
        .Text = ChrW(9500) & "(?)" & ChrW(9508)
464
        .Replacement.Text = "\1"
465
        .Forward = True
466
        .Wrap = wdFindContinue
467
        .Format = True
468
        .MatchCase = False
469
        .MatchWholeWord = False
470
        .MatchAllWordForms = False
471
        .MatchSoundsLike = False
472
        .MatchWildcards = True
473
    End With
474
    Selection.Find.Execute Replace:=wdReplaceAll
475
'
476
'
477
'  10. Restore paragraph ending marks, line breaks and page breaks.
478
'
479
    With ActiveDocument.Content.Find
480
        .ClearFormatting
481
        .Replacement.ClearFormatting
482
        .Forward = True
483
        .Wrap = wdFindContinue
484
        .Format = False
485
        .MatchCase = False
486
        .MatchWholeWord = False
487
        .MatchAllWordForms = False
488
        .MatchSoundsLike = False
489
        .MatchWildcards = False
490
        .Text = ChrW(9608)
491
        .Replacement.Text = "^p"
492
        .Execute Replace:=wdReplaceAll
493
        .Text = ChrW(9668)
494
        .Replacement.Text = "^l"
495
        .Execute Replace:=wdReplaceAll
496
        .Text = ChrW(9618)
497
        .Replacement.Text = "^m"
498
        .Execute Replace:=wdReplaceAll
499
    End With
500
'
501
'
502-
'  11. Save to the desktop as 'Formatted Text.rtf'.
502+
'  11. Save to the desktop as 'Formatted Text.rtf'. RTF chosen because it has the
503
'      highest compatibility with InDesign, and possibly other word processors.
504
'
505
    ActiveDocument.SaveAs2 FileName:="Formatted Text.rtf", FileFormat:= _
506
        wdFormatRTF, LockComments:=False, Password:="", AddToRecentFiles:=True, _
507
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
508
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
509
        False, CompatibilityMode:=0
510
    ActiveDocument.Close
511
'
512-
' 12. Clean-up and show a simple prompt message.
512+
513
' 12. Clean-up, and a simple prompt message when it's done.
514
'
515
    Kill "Plain Text.txt"
516
    Documents.Open FileName:="Formatted Text.rtf"
517
    MsgBox ("Done!")
518
    
519
End Sub