Advertisement
Guest User

Untitled

a guest
Nov 12th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1. Sub FlareOutputClean()
  2. '
  3. ' FlareOutputClean Macro
  4. ' Automatically performs some basic cleaning functions on documents created by MadCap Flare's Word Output
  5.  
  6. Dim objDoc As Document
  7. Set objDoc = ActiveDocument
  8.  
  9. Dim tempRange As Range
  10.  
  11. 'Application.ScreenUpdating = False
  12.  
  13. 'Dim objShape As InlineShape
  14.  
  15. '
  16. 'DOES NOT WORK, DISABLED: Automatically embeds all images into document
  17. '
  18. ' For Each objShape In objDoc.InlineShapes
  19. ' objShape.LinkFormat.SavePictureWithDocument = True
  20. ' Next objShape
  21.  
  22. 'Fix document title wrapping
  23. objDoc.ActiveWindow.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
  24.  
  25. With Selection.ParagraphFormat
  26. .SpaceBeforeAuto = False
  27. .SpaceAfterAuto = False
  28. .FirstLineIndent = InchesToPoints(-0.5)
  29. End With
  30. With Selection.ParagraphFormat
  31. .LeftIndent = InchesToPoints(0.5)
  32. .SpaceBeforeAuto = False
  33. .SpaceAfterAuto = False
  34. End With
  35. With Selection.ParagraphFormat
  36. .SpaceBeforeAuto = False
  37. .SpaceAfterAuto = False
  38. .FirstLineIndent = InchesToPoints(0)
  39. End With
  40.  
  41. '
  42. ' Automatically fixes Note: and Caution: autonumbering for Flare Word Output docs.
  43. '
  44. objDoc.Range.Select
  45. Selection.Find.ClearFormatting
  46. Selection.Find.Replacement.ClearFormatting
  47. Selection.Find.Replacement.Font.Bold = True
  48. With Selection.Find
  49. .Text = "C: {b}Caution: {/b}"
  50. .Replacement.Text = "Caution:^t"
  51. .Forward = True
  52. .Wrap = wdFindContinue
  53. .Format = True
  54. .MatchCase = False
  55. .MatchWholeWord = False
  56. .MatchWildcards = False
  57. .MatchSoundsLike = False
  58. .MatchAllWordForms = False
  59. End With
  60. Selection.Find.Execute Replace:=wdReplaceAll
  61.  
  62. With Selection.Find
  63. .Text = "N: {b}Note{/b}:"
  64. .Replacement.Text = "Note:"
  65. .Forward = True
  66. .Wrap = wdFindContinue
  67. .Format = True
  68. .MatchCase = False
  69. .MatchWholeWord = False
  70. .MatchWildcards = False
  71. .MatchSoundsLike = False
  72. .MatchAllWordForms = False
  73. End With
  74. Selection.Find.Execute Replace:=wdReplaceAll
  75.  
  76. '
  77. 'Automatically repairs errors with cross references. The search is performed by link color to avoid false positives.
  78. '
  79. Selection.Find.ClearFormatting
  80. Selection.Find.Font.Color = 16748574
  81. Selection.Find.Replacement.ClearFormatting
  82.  
  83. 'Add a space after quotes. Extra spaces will be added but cleaned up later.
  84. With Selection.Find
  85. .Text = """"
  86. .Replacement.Text = """ "
  87. .Forward = True
  88. .Wrap = wdFindContinue
  89. .Format = True
  90. .MatchCase = False
  91. .MatchWholeWord = False
  92. .MatchWildcards = False
  93. .MatchSoundsLike = False
  94. .MatchAllWordForms = False
  95. End With
  96. Selection.Find.Execute Replace:=wdReplaceAll
  97.  
  98. 'Extra spaces added above are removed, along with an extra space that existed previous to macro execution
  99. With Selection.Find
  100. .Text = """ "
  101. .Replacement.Text = """"
  102. .Forward = True
  103. .Wrap = wdFindContinue
  104. .Format = True
  105. .MatchCase = False
  106. .MatchWholeWord = False
  107. .MatchWildcards = False
  108. .MatchSoundsLike = False
  109. .MatchAllWordForms = False
  110. End With
  111. Selection.Find.Execute Replace:=wdReplaceAll
  112.  
  113. 'Extra space between the auto-number and heading text is removed
  114. With Selection.Find
  115. .Text = ": "
  116. .Replacement.Text = ": "
  117. .Forward = True
  118. .Wrap = wdFindContinue
  119. .Format = True
  120. .MatchCase = False
  121. .MatchWholeWord = False
  122. .MatchWildcards = False
  123. .MatchSoundsLike = False
  124. .MatchAllWordForms = False
  125. End With
  126. Selection.Find.Execute Replace:=wdReplaceAll
  127.  
  128. 'Stray error message caused by cross-references that do not have an auto-number is removed.
  129. With Selection.Find
  130. .Text = " UNRESOLVED CROSS-REFERENCE "
  131. .Replacement.Text = ""
  132. .Forward = True
  133. .Wrap = wdFindContinue
  134. .Format = True
  135. .MatchCase = True
  136. .MatchWholeWord = False
  137. .MatchWildcards = False
  138. .MatchSoundsLike = False
  139. .MatchAllWordForms = False
  140. End With
  141. Selection.Find.Execute Replace:=wdReplaceAll
  142.  
  143. '
  144. 'Properly colors page reference portion of cross references
  145. '
  146.  
  147. 'Start selection after table of contents to avoid unwanted formatting
  148. objDoc.Range.Select
  149. Selection.Find.ClearFormatting
  150. Selection.Find.Replacement.ClearFormatting
  151.  
  152. 'Need to iterate twice because TOC also has a Chapter 1 entry (d'oh!)
  153. For i = 1 To 2
  154. With Selection.Find
  155. .Text = "Chapter 1:"
  156. .Forward = True
  157. .Wrap = wdFindStop
  158. .Format = True
  159. .MatchCase = False
  160. .MatchWholeWord = False
  161. .MatchWildcards = False
  162. .MatchSoundsLike = False
  163. .MatchAllWordForms = False
  164. End With
  165. Selection.Find.Execute
  166. Next i
  167.  
  168. objDoc.ActiveWindow.Selection.MoveEnd Unit:=wdStory
  169. Set tempRange = ActiveWindow.Selection.Range
  170. Dim fc As Field
  171. 'reformat color of pageref field codes within selected area
  172. For Each fc In tempRange.Fields
  173. If fc.Type = wdFieldPageRef Then
  174. fc.Select
  175. objDoc.ActiveWindow.Selection.Font.Color = 16748574
  176. End If
  177. Next fc
  178.  
  179.  
  180. '
  181. 'properly configures document styles
  182. '
  183. Dim sty As Style
  184. Dim styl As Style
  185.  
  186. 'Clear quick style gallery of all styles that you don't want to add
  187. 'Comment out if undesired
  188. For Each styl In objDoc.Styles
  189. If styl.Type = wdStyleTypeCharacter Or _
  190. styl.Type = wdStyleTypeParagraph Or _
  191. styl.Type = wdStyleTypeLinked Then
  192. styl.QuickStyle = False
  193. End If
  194. Next styl
  195.  
  196. 'cycle through each style and make necessary changes
  197. For Each sty In objDoc.Styles
  198.  
  199. 'Fix indenting and add core note styles to QSG
  200. If sty.NameLocal = "p_note" Then
  201.  
  202. sty.NameLocal = "Note"
  203. With sty.ParagraphFormat
  204. .LeftIndent = InchesToPoints(0.52)
  205. End With
  206. sty.QuickStyle = True
  207. End If
  208.  
  209. If sty.NameLocal = "p_note2" Then
  210. sty.NameLocal = "Note2"
  211. sty.ParagraphFormat.LeftIndent = InchesToPoints(0.67)
  212. sty.QuickStyle = True
  213. End If
  214.  
  215. If sty.NameLocal = "p_note3" Then
  216. sty.NameLocal = "Note3"
  217. sty.ParagraphFormat.LeftIndent = InchesToPoints(0.9)
  218. sty.QuickStyle = True
  219. End If
  220.  
  221. If sty.NameLocal = "p_noteBull" Then
  222. sty.NameLocal = "NoteBullet"
  223. sty.ParagraphFormat.LeftIndent = InchesToPoints(0.77)
  224. sty.QuickStyle = True
  225. End If
  226.  
  227. 'Fix and add core Caution styles
  228. If sty.NameLocal = "p_Caution" Then
  229. sty.NameLocal = "Caution"
  230. sty.ParagraphFormat.LeftIndent = InchesToPoints(0.75)
  231. sty.ParagraphFormat.Borders.DistanceFromLeft = 7
  232. sty.QuickStyle = True
  233. End If
  234.  
  235. If sty.NameLocal = "p_CautionN2" Then
  236. sty.NameLocal = "Caution2"
  237. sty.ParagraphFormat.LeftIndent = InchesToPoints(0.9)
  238. sty.ParagraphFormat.Borders.DistanceFromLeft = 7
  239. sty.QuickStyle = True
  240. End If
  241.  
  242. If sty.NameLocal = "p_CautionN3" Then
  243. sty.NameLocal = "Caution3"
  244. sty.ParagraphFormat.LeftIndent = InchesToPoints(1.13)
  245. sty.ParagraphFormat.Borders.DistanceFromLeft = 7
  246. sty.QuickStyle = True
  247. End If
  248.  
  249.  
  250.  
  251. 'Fix heading styles' outline level
  252. If sty.NameLocal = "h1" Then
  253. sty.ParagraphFormat.OutlineLevel = 1
  254. sty.QuickStyle = True
  255. End If
  256.  
  257. If sty.NameLocal = "h1_heading2" Then
  258. sty.ParagraphFormat.OutlineLevel = 1
  259. sty.QuickStyle = True
  260. End If
  261.  
  262. If sty.NameLocal = "p_ChapterNumber" Then
  263. sty.ParagraphFormat.OutlineLevel = 1
  264. End If
  265.  
  266. If sty.NameLocal = "p_ChapterNumber" Then
  267. sty.ParagraphFormat.OutlineLevel = 1
  268. End If
  269.  
  270. If sty.NameLocal = "p_AppendixHeading" Then
  271. sty.ParagraphFormat.OutlineLevel = 1
  272. End If
  273.  
  274.  
  275.  
  276. If sty.NameLocal = "h2" Then
  277. sty.ParagraphFormat.OutlineLevel = 2
  278. sty.QuickStyle = True
  279. End If
  280.  
  281. If sty.NameLocal = "h2_Heading1" Then
  282. sty.ParagraphFormat.OutlineLevel = 2
  283. End If
  284.  
  285. If sty.NameLocal = "h2_Heading2" Then
  286. sty.ParagraphFormat.OutlineLevel = 2
  287. End If
  288.  
  289. If sty.NameLocal = "h2_Heading3" Then
  290. sty.ParagraphFormat.OutlineLevel = 2
  291. End If
  292.  
  293.  
  294. If sty.NameLocal = "h3" Then
  295. sty.ParagraphFormat.OutlineLevel = 3
  296. sty.QuickStyle = True
  297. End If
  298.  
  299. If sty.NameLocal = "h2_ExerciseTitle" Then
  300. sty.ParagraphFormat.OutlineLevel = 2
  301. sty.QuickStyle = True
  302. End If
  303.  
  304. If sty.NameLocal = "h3_ExerciseTitle" Then
  305. sty.ParagraphFormat.OutlineLevel = 3
  306. sty.QuickStyle = True
  307. End If
  308.  
  309. If sty.NameLocal = "h3_Heading1" Then
  310. sty.ParagraphFormat.OutlineLevel = 3
  311. End If
  312.  
  313. If sty.NameLocal = "h3_Heading2" Then
  314. sty.ParagraphFormat.OutlineLevel = 3
  315. End If
  316.  
  317. If sty.NameLocal = "h3_Heading3" Then
  318. sty.ParagraphFormat.OutlineLevel = 3
  319. End If
  320.  
  321.  
  322. If sty.NameLocal = "h4" Then
  323. sty.ParagraphFormat.OutlineLevel = 4
  324. sty.QuickStyle = True
  325. End If
  326.  
  327. If sty.NameLocal = "h4_Heading3" Then
  328. sty.ParagraphFormat.OutlineLevel = 4
  329. End If
  330.  
  331. If sty.NameLocal = "h4_Heading4" Then
  332. sty.ParagraphFormat.OutlineLevel = 4
  333. End If
  334.  
  335.  
  336. If sty.NameLocal = "h5" Then
  337. sty.ParagraphFormat.OutlineLevel = 5
  338. sty.QuickStyle = True
  339. End If
  340.  
  341.  
  342. If sty.NameLocal = "h6" Then
  343. sty.ParagraphFormat.OutlineLevel = 6
  344. End If
  345.  
  346.  
  347.  
  348. 'Fix odd extra note styles
  349. If sty.NameLocal = "p_note_1" Then
  350. With sty.ParagraphFormat
  351. .LeftIndent = InchesToPoints(0.77)
  352. End With
  353. End If
  354.  
  355. If sty.NameLocal = "p_note_2" Then
  356. With sty.ParagraphFormat
  357. .LeftIndent = InchesToPoints(0.77)
  358. End With
  359. End If
  360.  
  361. If sty.NameLocal = "p_note_3" Then
  362. With sty.ParagraphFormat
  363. .LeftIndent = InchesToPoints(1.04)
  364. End With
  365. End If
  366.  
  367. If sty.NameLocal = "p_note_4" Then
  368. With sty.ParagraphFormat
  369. .LeftIndent = InchesToPoints(0.95)
  370. End With
  371. End If
  372.  
  373. 'Add other important styles to QuickStyle gallery
  374. '
  375.  
  376. If sty.NameLocal = "p_ActionFirst" Then
  377. sty.NameLocal = "ActionFirst"
  378. sty.QuickStyle = True
  379. End If
  380.  
  381. If sty.NameLocal = "p_Action" Then
  382. sty.NameLocal = "Action"
  383. sty.QuickStyle = True
  384. End If
  385.  
  386. If sty.NameLocal = "p_Step1" Then
  387. sty.NameLocal = "StepFirst"
  388. sty.QuickStyle = True
  389. End If
  390.  
  391. If sty.NameLocal = "p_Step" Then
  392. sty.NameLocal = "Step"
  393. sty.QuickStyle = True
  394. End If
  395.  
  396. If sty.NameLocal = "p" Then
  397. sty.NameLocal = "Normal1"
  398. sty.QuickStyle = True
  399. End If
  400.  
  401. If sty.NameLocal = "p_Normal2" Then
  402. sty.NameLocal = "Normal2"
  403. sty.QuickStyle = True
  404. End If
  405.  
  406. If sty.NameLocal = "p_Normal3" Then
  407. sty.NameLocal = "Normal3"
  408. sty.QuickStyle = True
  409. End If
  410. Next sty
  411.  
  412.  
  413. '
  414. 'Add borders to all images. Must be last step in processing.
  415. '
  416. Dim compare As Boolean
  417. 'loop through inline shapes
  418. Dim iShape As InlineShape
  419.  
  420.  
  421. Dim shapeCount As Integer
  422. shapeCount = 1
  423. 'loop through shapes
  424. For Each iShape In objDoc.InlineShapes
  425.  
  426. 'check to make sure each image is only processed once
  427. shapeCount = shapeCount + 1
  428.  
  429. If shapeCount <= objDoc.InlineShapes.Count Then
  430.  
  431. 'check if the current shape is an picture
  432. iShape.Select
  433. If iShape.Type = wdInlineShapeLinkedPicture Or wdInlineShapePicture Then
  434. compare = True
  435. End If
  436. If iShape.Type = wdInlineShapePicture Or wdInlineShapeLinkedPicture Then
  437.  
  438. 'if necessary, change the border color to black
  439. iShape.Borders.OutsideLineStyle = wdLineStyleSingle
  440.  
  441. 'set border width to 1
  442. iShape.Borders.OutsideLineWidth = wdLineWidth100pt
  443. End If
  444. Else
  445. Exit Sub
  446. End If
  447. Next iShape
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454. 'Application.ScreenUpdating = True
  455. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement