Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2010
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 KB | None | 0 0
  1. Sub Word2Textile()
  2.  
  3. Application.ScreenUpdating = False
  4.  
  5. ConvertH1
  6. ConvertH2
  7. ConvertH3
  8. ConvertH4
  9. ConvertH5
  10.  
  11. ConvertItalic
  12. ConvertBold
  13. ConvertUnderline
  14.  
  15. ConvertLists
  16. ConvertTables
  17. ReplaceQuotes
  18.  
  19. ' Copy to clipboard
  20. ActiveDocument.Content.Copy
  21.  
  22. Application.ScreenUpdating = True
  23. End Sub
  24.  
  25. Private Sub ConvertH1()
  26. Dim normalStyle As Style
  27. Set normalStyle = ActiveDocument.Styles(wdStyleNormal)
  28.  
  29. ActiveDocument.Select
  30.  
  31. With Selection.Find
  32.  
  33. .ClearFormatting
  34. .Style = ActiveDocument.Styles(wdStyleHeading1)
  35. .Text = ""
  36.  
  37. .Format = True
  38. .MatchCase = False
  39. .MatchWholeWord = False
  40. .MatchWildcards = False
  41. .MatchSoundsLike = False
  42. .MatchAllWordForms = False
  43.  
  44. .Forward = True
  45. .Wrap = wdFindContinue
  46.  
  47. Do While .Execute
  48. With Selection
  49. If InStr(1, .Text, vbCr) Then
  50. ' Just process the chunk before any newline characters
  51. ' We'll pick-up the rest with the next search
  52. .Collapse
  53. .MoveEndUntil vbCr
  54. End If
  55.  
  56. ' Don't bother to markup newline characters (prevents a loop, as well)
  57. If Not .Text = vbCr Then
  58. .InsertBefore "h3. "
  59. End If
  60.  
  61. .Style = normalStyle
  62. End With
  63. Loop
  64. End With
  65. End Sub
  66.  
  67. Private Sub ConvertH2()
  68. Dim normalStyle As Style
  69. Set normalStyle = ActiveDocument.Styles(wdStyleNormal)
  70.  
  71. ActiveDocument.Select
  72.  
  73. With Selection.Find
  74.  
  75. .ClearFormatting
  76. .Style = ActiveDocument.Styles(wdStyleHeading2)
  77. .Text = ""
  78.  
  79. .Format = True
  80. .MatchCase = False
  81. .MatchWholeWord = False
  82. .MatchWildcards = False
  83. .MatchSoundsLike = False
  84. .MatchAllWordForms = False
  85.  
  86. .Forward = True
  87. .Wrap = wdFindContinue
  88.  
  89. Do While .Execute
  90. With Selection
  91. If InStr(1, .Text, vbCr) Then
  92. ' Just process the chunk before any newline characters
  93. ' We'll pick-up the rest with the next search
  94. .Collapse
  95. .MoveEndUntil vbCr
  96. End If
  97.  
  98. ' Don't bother to markup newline characters (prevents a loop, as well)
  99. If Not .Text = vbCr Then
  100. .InsertBefore "h3. "
  101. End If
  102.  
  103. .Style = normalStyle
  104. End With
  105. Loop
  106. End With
  107. End Sub
  108.  
  109. Private Sub ConvertH3()
  110. Dim normalStyle As Style
  111. Set normalStyle = ActiveDocument.Styles(wdStyleNormal)
  112.  
  113. ActiveDocument.Select
  114.  
  115. With Selection.Find
  116.  
  117. .ClearFormatting
  118. .Style = ActiveDocument.Styles(wdStyleHeading3)
  119. .Text = ""
  120.  
  121. .Format = True
  122. .MatchCase = False
  123. .MatchWholeWord = False
  124. .MatchWildcards = False
  125. .MatchSoundsLike = False
  126. .MatchAllWordForms = False
  127.  
  128. .Forward = True
  129. .Wrap = wdFindContinue
  130.  
  131. Do While .Execute
  132. With Selection
  133. If InStr(1, .Text, vbCr) Then
  134. ' Just process the chunk before any newline characters
  135. ' We'll pick-up the rest with the next search
  136. .Collapse
  137. .MoveEndUntil vbCr
  138. End If
  139.  
  140. ' Don't bother to markup newline characters (prevents a loop, as well)
  141. If Not .Text = vbCr Then
  142. .InsertBefore "h3. "
  143. End If
  144.  
  145. .Style = normalStyle
  146. End With
  147. Loop
  148. End With
  149. End Sub
  150.  
  151. Private Sub ConvertH4()
  152. Dim normalStyle As Style
  153. Set normalStyle = ActiveDocument.Styles(wdStyleNormal)
  154.  
  155. ActiveDocument.Select
  156.  
  157. With Selection.Find
  158.  
  159. .ClearFormatting
  160. .Style = ActiveDocument.Styles(wdStyleHeading4)
  161. .Text = ""
  162.  
  163. .Format = True
  164. .MatchCase = False
  165. .MatchWholeWord = False
  166. .MatchWildcards = False
  167. .MatchSoundsLike = False
  168. .MatchAllWordForms = False
  169.  
  170. .Forward = True
  171. .Wrap = wdFindContinue
  172.  
  173. Do While .Execute
  174. With Selection
  175. If InStr(1, .Text, vbCr) Then
  176. ' Just process the chunk before any newline characters
  177. ' We'll pick-up the rest with the next search
  178. .Collapse
  179. .MoveEndUntil vbCr
  180. End If
  181.  
  182. ' Don't bother to markup newline characters (prevents a loop, as well)
  183. If Not .Text = vbCr Then
  184. .InsertBefore "h4. "
  185. End If
  186.  
  187. .Style = normalStyle
  188. End With
  189. Loop
  190. End With
  191. End Sub
  192.  
  193. Private Sub ConvertH5()
  194. Dim normalStyle As Style
  195. Set normalStyle = ActiveDocument.Styles(wdStyleNormal)
  196.  
  197. ActiveDocument.Select
  198.  
  199. With Selection.Find
  200.  
  201. .ClearFormatting
  202. .Style = ActiveDocument.Styles(wdStyleHeading4)
  203. .Text = ""
  204.  
  205. .Format = True
  206. .MatchCase = False
  207. .MatchWholeWord = False
  208. .MatchWildcards = False
  209. .MatchSoundsLike = False
  210. .MatchAllWordForms = False
  211.  
  212. .Forward = True
  213. .Wrap = wdFindContinue
  214.  
  215. Do While .Execute
  216. With Selection
  217. If InStr(1, .Text, vbCr) Then
  218. ' Just process the chunk before any newline characters
  219. ' We'll pick-up the rest with the next search
  220. .Collapse
  221. .MoveEndUntil vbCr
  222. End If
  223.  
  224. ' Don't bother to markup newline characters (prevents a loop, as well)
  225. If Not .Text = vbCr Then
  226. .InsertBefore "h5. "
  227. End If
  228.  
  229. .Style = normalStyle
  230. End With
  231. Loop
  232. End With
  233. End Sub
  234.  
  235. Private Sub ConvertBold()
  236. ActiveDocument.Select
  237.  
  238. With Selection.Find
  239.  
  240. .ClearFormatting
  241. .Font.Bold = True
  242. .Text = ""
  243.  
  244. .Format = True
  245. .MatchCase = False
  246. .MatchWholeWord = False
  247. .MatchWildcards = False
  248. .MatchSoundsLike = False
  249. .MatchAllWordForms = False
  250.  
  251. .Forward = True
  252. .Wrap = wdFindContinue
  253.  
  254. Do While .Execute
  255. With Selection
  256. If InStr(1, .Text, vbCr) Then
  257. ' Just process the chunk before any newline characters
  258. ' We'll pick-up the rest with the next search
  259. .Font.Bold = False
  260. .Collapse
  261. .MoveEndUntil vbCr
  262. End If
  263.  
  264. ' Don't bother to markup newline characters (prevents a loop, as well)
  265. If Not .Text = vbCr Then
  266. .InsertBefore "<b>"
  267. .InsertAfter "</b>"
  268. End If
  269.  
  270. .Font.Bold = False
  271. End With
  272. Loop
  273. End With
  274. End Sub
  275.  
  276. Private Sub ConvertItalic()
  277. ActiveDocument.Select
  278.  
  279. With Selection.Find
  280.  
  281. .ClearFormatting
  282. .Font.Italic = True
  283. .Text = ""
  284.  
  285. .Format = True
  286. .MatchCase = False
  287. .MatchWholeWord = False
  288. .MatchWildcards = False
  289. .MatchSoundsLike = False
  290. .MatchAllWordForms = False
  291.  
  292. .Forward = True
  293. .Wrap = wdFindContinue
  294.  
  295. Do While .Execute
  296. With Selection
  297. If InStr(1, .Text, vbCr) Then
  298. ' Just process the chunk before any newline characters
  299. ' We'll pick-up the rest with the next search
  300. .Font.Italic = False
  301. .Collapse
  302. .MoveEndUntil vbCr
  303. End If
  304.  
  305. ' Don't bother to markup newline characters (prevents a loop, as well)
  306. If Not .Text = vbCr Then
  307. .InsertBefore "<i>"
  308. .InsertAfter "</i>"
  309. End If
  310.  
  311. .Font.Italic = False
  312. End With
  313. Loop
  314. End With
  315. End Sub
  316.  
  317. Private Sub ConvertUnderline()
  318. ActiveDocument.Select
  319.  
  320. With Selection.Find
  321.  
  322. .ClearFormatting
  323. .Font.Underline = True
  324. .Text = ""
  325.  
  326. .Format = True
  327. .MatchCase = False
  328. .MatchWholeWord = False
  329. .MatchWildcards = False
  330. .MatchSoundsLike = False
  331. .MatchAllWordForms = False
  332.  
  333. .Forward = True
  334. .Wrap = wdFindContinue
  335.  
  336. Do While .Execute
  337. With Selection
  338. If InStr(1, .Text, vbCr) Then
  339. ' Just process the chunk before any newline characters
  340. ' We'll pick-up the rest with the next search
  341. .Font.Underline = False
  342. .Collapse
  343. .MoveEndUntil vbCr
  344. End If
  345.  
  346. ' Don't bother to markup newline characters (prevents a loop, as well)
  347. If Not .Text = vbCr Then
  348. .InsertBefore "<u>"
  349. .InsertAfter "</u>"
  350. End If
  351.  
  352. .Font.Underline = False
  353. End With
  354. Loop
  355. End With
  356. End Sub
  357.  
  358. Private Sub ConvertLists()
  359. Dim para As Paragraph
  360. For Each para In ActiveDocument.ListParagraphs
  361. With para.Range
  362. .InsertBefore " "
  363. For i = 1 To .ListFormat.ListLevelNumber
  364. If .ListFormat.ListType = wdListBullet Then
  365. .InsertBefore "*"
  366. Else
  367. .InsertBefore "#"
  368. End If
  369. Next i
  370. .ListFormat.RemoveNumbers
  371. End With
  372. Next para
  373. End Sub
  374.  
  375. Private Sub ConvertTables()
  376. Dim oTable As Table
  377. For Each oTable In ActiveDocument.Tables
  378. With oTable
  379. ReDim x(1 To oTable.Rows.Count, 1 To oTable.Columns.Count)
  380. i = 0
  381. For Each a In oTable.Rows
  382. i = i + 1
  383. j = 0
  384. For Each b In a.Cells
  385. j = j + 1
  386. strText = b.Range.Text
  387. x(i, j) = Left(strText, Len(strText) - 2)
  388. Next b
  389. Next a
  390. .Range.InsertParagraphAfter
  391. .Range.InsertAfter ("{| border=1")
  392. .Range.InsertParagraphAfter
  393. For k = 1 To i
  394. For l = 1 To j
  395. .Range.InsertAfter " || " + x(k, l)
  396. '.Range.InsertParagraphAfter
  397. Next
  398. .Range.InsertParagraphAfter
  399. .Range.InsertAfter "|-"
  400. .Range.InsertParagraphAfter
  401. Next
  402. .Range.InsertAfter ("|}")
  403. .Range.InsertParagraphAfter
  404. End With
  405. Next oTable
  406. End Sub
  407.  
  408. ' Replace all smart quotes with their dumb equivalents
  409.  
  410. Private Sub ReplaceQuotes()
  411.  
  412. Dim quotes As Boolean
  413.  
  414. quotes = Options.AutoFormatAsYouTypeReplaceQuotes
  415.  
  416. Options.AutoFormatAsYouTypeReplaceQuotes = False
  417.  
  418. ReplaceString ChrW(8220), """"
  419.  
  420. ReplaceString ChrW(8221), """"
  421.  
  422. ReplaceString "‘", "'"
  423.  
  424. ReplaceString "’", "'"
  425.  
  426. ReplaceString "", "'"
  427.  
  428. ReplaceString "^p", "^p^p"
  429.  
  430. ReplaceString "^p^p^p", "^p^p"
  431.  
  432. Options.AutoFormatAsYouTypeReplaceQuotes = quotes
  433.  
  434. End Sub
  435.  
  436. Private Function ReplaceString(findStr As String, replacementStr As String)
  437. Selection.Find.ClearFormatting
  438. Selection.Find.Replacement.ClearFormatting
  439. With Selection.Find
  440. .Text = findStr
  441. .Replacement.Text = replacementStr
  442. .Forward = True
  443. .Wrap = wdFindContinue
  444. .Format = False
  445. .MatchCase = False
  446. .MatchWholeWord = False
  447. .MatchWildcards = False
  448. .MatchSoundsLike = False
  449. .MatchAllWordForms = False
  450. End With
  451. Selection.Find.Execute Replace:=wdReplaceAll
  452. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement