Advertisement
Calion

Word2Textile

Jul 18th, 2011
1,911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Word2Textile()
  2. '
  3. ' Word2Textile Macro
  4. ' Macro created 7/18/11 by Jim Syler, zarquon42@aol.com
  5. ' Modified from Word2MediaWiki, <http://www.infpro.com/Word2MediaWiki.aspx>
  6. ' Textile format information available at <http://redcloth.org/hobix.com/textile/>
  7. '
  8.  
  9.     Application.ScreenUpdating = False
  10.     ReplaceQuotes
  11.     'TextileEscapeChars
  12.    TextileConvertHyperlinks
  13.     TextileConvertH1
  14.     TextileConvertH2
  15.     TextileConvertH3
  16.     TextileConvertH4
  17.     TextileConvertH5
  18.     TextileConvertItalic
  19.     TextileConvertBold
  20.     TextileConvertUnderline
  21.     TextileConvertStrikeThrough
  22.     TextileConvertSuperscript
  23.     TextileConvertSubscript
  24.     TextileConvertLists
  25.     TextileConvertTables
  26.     ' Copy to clipboard
  27.   ActiveDocument.Content.Copy
  28.    Application.ScreenUpdating = True
  29. End Sub
  30.  
  31. Private Sub TextileConvertH1()
  32.     ReplaceHeading wdStyleHeading1, "h1. "
  33. End Sub
  34.  
  35. Private Sub TextileConvertH2()
  36.     ReplaceHeading wdStyleHeading2, "h2. "
  37. End Sub
  38.  
  39. Private Sub TextileConvertH3()
  40.     ReplaceHeading wdStyleHeading3, "h3. "
  41. End Sub
  42.  
  43. Private Sub TextileConvertH4()
  44.     ReplaceHeading wdStyleHeading4, "h4. "
  45. End Sub
  46.  
  47. Private Sub TextileConvertH5()
  48.     ReplaceHeading wdStyleHeading5, "h5. "
  49. End Sub
  50.  
  51. Private Sub TextileConvertBold()
  52.     ActiveDocument.Select
  53.     With Selection.Find
  54.  
  55.    
  56.  
  57.         .ClearFormatting
  58.  
  59.         .Font.Bold = True
  60.  
  61.         .Text = ""
  62.  
  63.        
  64.  
  65.         .Format = True
  66.  
  67.         .MatchCase = False
  68.  
  69.         .MatchWholeWord = False
  70.  
  71.         .MatchWildcards = False
  72.  
  73.         .MatchSoundsLike = False
  74.  
  75.         .MatchAllWordForms = False
  76.  
  77.        
  78.  
  79.         .Forward = True
  80.  
  81.         .Wrap = wdFindContinue
  82.  
  83.        
  84.  
  85.         Do While .Execute
  86.  
  87.             With Selection
  88.  
  89.                 If Len(.Text) > 1 And InStr(1, .Text, vbCr) Then
  90.  
  91.                     ' Just process the chunk before any newline characters
  92.  
  93.                     ' We'll pick-up the rest with the next search
  94.  
  95.                     .Collapse
  96.  
  97.                     .MoveEndUntil vbCr
  98.  
  99.                 End If
  100.  
  101.                                      
  102.  
  103.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  104.  
  105.                 If Not .Text = vbCr Then
  106.  
  107.                     .InsertBefore "*"
  108.  
  109.                     .InsertAfter "*"
  110.  
  111.                 End If
  112.  
  113.                
  114.  
  115.                 .Style = ActiveDocument.Styles("Default Paragraph Font")
  116.  
  117.                 .Font.Bold = False
  118.  
  119.             End With
  120.  
  121.         Loop
  122.  
  123.     End With
  124.  
  125. End Sub
  126.  
  127.  
  128.  
  129. Private Sub TextileConvertItalic()
  130.  
  131.     ActiveDocument.Select
  132.  
  133.    
  134.  
  135.     With Selection.Find
  136.  
  137.    
  138.  
  139.         .ClearFormatting
  140.  
  141.         .Font.Italic = True
  142.  
  143.         .Text = ""
  144.  
  145.        
  146.  
  147.         .Format = True
  148.  
  149.         .MatchCase = False
  150.  
  151.         .MatchWholeWord = False
  152.  
  153.         .MatchWildcards = False
  154.  
  155.         .MatchSoundsLike = False
  156.  
  157.         .MatchAllWordForms = False
  158.  
  159.        
  160.  
  161.         .Forward = True
  162.  
  163.         .Wrap = wdFindContinue
  164.  
  165.        
  166.  
  167.         Do While .Execute
  168.  
  169.             With Selection
  170.  
  171.                 If Len(.Text) > 1 And InStr(1, .Text, vbCr) Then
  172.  
  173.                     ' Just process the chunk before any newline characters
  174.  
  175.                     ' We'll pick-up the rest with the next search
  176.  
  177.                     .Collapse
  178.  
  179.                     .MoveEndUntil vbCr
  180.  
  181.                 End If
  182.  
  183.                                      
  184.  
  185.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  186.  
  187.                 If Not .Text = vbCr Then
  188.  
  189.                     .InsertBefore "_"
  190.  
  191.                     .InsertAfter "_"
  192.  
  193.                 End If
  194.  
  195.                
  196.  
  197.                 .Style = ActiveDocument.Styles("Default Paragraph Font")
  198.  
  199.                 .Font.Italic = False
  200.  
  201.             End With
  202.  
  203.         Loop
  204.  
  205.     End With
  206.  
  207. End Sub
  208.  
  209.  
  210.  
  211. Private Sub TextileConvertUnderline()
  212.  
  213.     ActiveDocument.Select
  214.  
  215.    
  216.  
  217.     With Selection.Find
  218.  
  219.    
  220.  
  221.         .ClearFormatting
  222.  
  223.         .Font.Underline = True
  224.  
  225.         .Text = ""
  226.  
  227.        
  228.  
  229.         .Format = True
  230.  
  231.         .MatchCase = False
  232.  
  233.         .MatchWholeWord = False
  234.  
  235.         .MatchWildcards = False
  236.  
  237.         .MatchSoundsLike = False
  238.  
  239.         .MatchAllWordForms = False
  240.  
  241.        
  242.  
  243.         .Forward = True
  244.  
  245.         .Wrap = wdFindContinue
  246.  
  247.        
  248.  
  249.         Do While .Execute
  250.  
  251.             With Selection
  252.  
  253.                 If Len(.Text) > 1 And InStr(1, .Text, vbCr) Then
  254.  
  255.                     ' Just process the chunk before any newline characters
  256.  
  257.                     ' We'll pick-up the rest with the next search
  258.  
  259.                     .Collapse
  260.  
  261.                     .MoveEndUntil vbCr
  262.  
  263.                 End If
  264.  
  265.                                        
  266.  
  267.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  268.  
  269.                 If Not .Text = vbCr Then
  270.  
  271.                     .InsertBefore "+"
  272.  
  273.                     .InsertAfter "+"
  274.  
  275.                 End If
  276.  
  277.                
  278.  
  279.                 .Style = ActiveDocument.Styles("Default Paragraph Font")
  280.  
  281.                 .Font.Underline = False
  282.  
  283.             End With
  284.  
  285.         Loop
  286.  
  287.     End With
  288.  
  289. End Sub
  290.  
  291.  
  292.  
  293. Private Sub TextileConvertStrikeThrough()
  294.  
  295.     ActiveDocument.Select
  296.  
  297.    
  298.  
  299.     With Selection.Find
  300.  
  301.    
  302.  
  303.         .ClearFormatting
  304.  
  305.         .Font.StrikeThrough = True
  306.  
  307.         .Text = ""
  308.  
  309.        
  310.  
  311.         .Format = True
  312.  
  313.         .MatchCase = False
  314.  
  315.         .MatchWholeWord = False
  316.  
  317.         .MatchWildcards = False
  318.  
  319.         .MatchSoundsLike = False
  320.  
  321.         .MatchAllWordForms = False
  322.  
  323.        
  324.  
  325.         .Forward = True
  326.  
  327.         .Wrap = wdFindContinue
  328.  
  329.        
  330.  
  331.         Do While .Execute
  332.  
  333.             With Selection
  334.  
  335.                 If Len(.Text) > 1 And InStr(1, .Text, vbCr) Then
  336.  
  337.                     ' Just process the chunk before any newline characters
  338.  
  339.                     ' We'll pick-up the rest with the next search
  340.  
  341.                     .Collapse
  342.  
  343.                     .MoveEndUntil vbCr
  344.  
  345.                 End If
  346.  
  347.                                      
  348.  
  349.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  350.  
  351.                 If Not .Text = vbCr Then
  352.  
  353.                     .InsertBefore ("-")
  354.  
  355.                     .InsertAfter ("-")
  356.  
  357.                 End If
  358.  
  359.                
  360.  
  361.                 .Style = ActiveDocument.Styles("Default Paragraph Font")
  362.  
  363.                 .Font.StrikeThrough = False
  364.  
  365.             End With
  366.  
  367.         Loop
  368.  
  369.     End With
  370.  
  371. End Sub
  372.  
  373.  
  374.  
  375. Private Sub TextileConvertSuperscript()
  376.  
  377.     ActiveDocument.Select
  378.  
  379.    
  380.  
  381.     With Selection.Find
  382.  
  383.    
  384.  
  385.         .ClearFormatting
  386.  
  387.         .Font.Superscript = True
  388.  
  389.         .Text = ""
  390.  
  391.        
  392.  
  393.         .Format = True
  394.  
  395.         .MatchCase = False
  396.  
  397.         .MatchWholeWord = False
  398.  
  399.         .MatchWildcards = False
  400.  
  401.         .MatchSoundsLike = False
  402.  
  403.         .MatchAllWordForms = False
  404.  
  405.        
  406.  
  407.         .Forward = True
  408.  
  409.         .Wrap = wdFindContinue
  410.  
  411.        
  412.  
  413.         Do While .Execute
  414.  
  415.             With Selection
  416.  
  417.                 .Text = Trim(.Text)
  418.  
  419.                 If Len(.Text) > 1 And InStr(1, .Text, vbCr) Then
  420.  
  421.                     ' Just process the chunk before any newline characters
  422.  
  423.                     ' We'll pick-up the rest with the next search
  424.  
  425.                     .Collapse
  426.  
  427.                     .MoveEndUntil vbCr
  428.  
  429.                 End If
  430.  
  431.                                        
  432.  
  433.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  434.  
  435.                 If Not .Text = vbCr Then
  436.  
  437.                     .InsertBefore ("^")
  438.  
  439.                     .InsertAfter ("^")
  440.  
  441.                 End If
  442.  
  443.                
  444.  
  445.                 .Style = ActiveDocument.Styles("Default Paragraph Font")
  446.  
  447.                 .Font.Superscript = False
  448.  
  449.             End With
  450.  
  451.         Loop
  452.  
  453.     End With
  454.  
  455. End Sub
  456.  
  457.  
  458.  
  459. Private Sub TextileConvertSubscript()
  460.  
  461.     ActiveDocument.Select
  462.  
  463.    
  464.  
  465.     With Selection.Find
  466.  
  467.    
  468.  
  469.         .ClearFormatting
  470.  
  471.         .Font.Subscript = True
  472.  
  473.         .Text = ""
  474.  
  475.        
  476.  
  477.         .Format = True
  478.  
  479.         .MatchCase = False
  480.  
  481.         .MatchWholeWord = False
  482.  
  483.         .MatchWildcards = False
  484.  
  485.         .MatchSoundsLike = False
  486.  
  487.         .MatchAllWordForms = False
  488.  
  489.        
  490.  
  491.         .Forward = True
  492.  
  493.         .Wrap = wdFindContinue
  494.  
  495.        
  496.  
  497.         Do While .Execute
  498.  
  499.             With Selection
  500.  
  501.                 .Text = Trim(.Text)
  502.  
  503.                 If Len(.Text) > 1 And InStr(1, .Text, vbCr) Then
  504.  
  505.                     ' Just process the chunk before any newline characters
  506.  
  507.                     ' We'll pick-up the rest with the next search
  508.  
  509.                     .Collapse
  510.  
  511.                     .MoveEndUntil vbCr
  512.  
  513.                 End If
  514.  
  515.                                        
  516.  
  517.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  518.  
  519.                 If Not .Text = vbCr Then
  520.  
  521.                     .InsertBefore ("~")
  522.  
  523.                     .InsertAfter ("~")
  524.  
  525.                 End If
  526.  
  527.                
  528.  
  529.                 .Style = ActiveDocument.Styles("Default Paragraph Font")
  530.  
  531.                 .Font.Subscript = False
  532.  
  533.             End With
  534.  
  535.         Loop
  536.  
  537.     End With
  538.  
  539. End Sub
  540.  
  541.  
  542.  
  543. Private Sub TextileConvertLists()
  544.  
  545.     Dim para As Paragraph
  546.  
  547.     For Each para In ActiveDocument.ListParagraphs
  548.  
  549.         With para.Range
  550.  
  551.             .InsertBefore " "
  552.  
  553.             For i = 1 To .ListFormat.ListLevelNumber
  554.  
  555.                 If .ListFormat.ListType = wdListBullet Then
  556.  
  557.                     .InsertBefore "*"
  558.  
  559.                 Else
  560.  
  561.                     .InsertBefore "#"
  562.  
  563.                 End If
  564.  
  565.             Next i
  566.  
  567.             .ListFormat.RemoveNumbers
  568.  
  569.         End With
  570.  
  571.     Next para
  572.  
  573. End Sub
  574.  
  575.  
  576.  
  577. Private Sub TextileConvertTables()
  578.  
  579.     Dim thisTable As Table
  580.  
  581.     For Each thisTable In ActiveDocument.Tables
  582.  
  583.         With thisTable
  584.  
  585.             For Each aRow In thisTable.Rows
  586.  
  587.                 With aRow
  588.  
  589.                 For Each aCell In aRow.Cells
  590.  
  591.                     With aCell
  592.  
  593.                         'aCell.Range.InsertBefore "|"
  594.  
  595.                         'aCell.Range.InsertAfter "|"
  596.  
  597.                     End With
  598.  
  599.                 Next aCell
  600.  
  601.                 .Range.InsertBefore "|"
  602.  
  603.                 .Range.InsertAfter "|"
  604.                 'vbCrLf + "|-"
  605.  
  606.                 End With
  607.  
  608.             Next aRow
  609.  
  610.         '.Range.InsertBefore "{|" + vbCrLf
  611.  
  612.         '.Range.InsertAfter vbCrLf + "|}"
  613.  
  614.         .ConvertToText "|"
  615.  
  616.         End With
  617.  
  618.     Next thisTable
  619.  
  620. End Sub
  621.  
  622.  
  623.  
  624.  
  625. Private Sub TextileConvertHyperlinks()
  626.  
  627.     Dim hyperCount As Integer
  628.  
  629.    
  630.  
  631.     hyperCount = ActiveDocument.Hyperlinks.Count
  632.  
  633.    
  634.  
  635.     For i = 1 To hyperCount
  636.  
  637.         With ActiveDocument.Hyperlinks(1)
  638.  
  639.             Dim addr As String
  640.  
  641.             addr = .Address
  642.  
  643.             .Delete
  644.  
  645.             .Range.InsertBefore """"
  646.  
  647.             .Range.InsertAfter """" & ":" & addr
  648.  
  649.         End With
  650.  
  651.     Next i
  652.  
  653. End Sub
  654.  
  655.  
  656.  
  657. ' Replace all smart quotes with their dumb equivalents
  658.  
  659. Private Sub ReplaceQuotes()
  660.  
  661.     Dim quotes As Boolean
  662.  
  663.     quotes = Options.AutoFormatAsYouTypeReplaceQuotes
  664.  
  665.     Options.AutoFormatAsYouTypeReplaceQuotes = False
  666.  
  667.     ReplaceString ChrW(8220), """"
  668.  
  669.     ReplaceString ChrW(8221), """"
  670.  
  671.     ReplaceString "ë", "'"
  672.  
  673.     ReplaceString "í", "'"
  674.    
  675.    
  676.     ReplaceString "—", "--"
  677.     'This is the em-dash symbol on the Mac.
  678.    
  679.     ReplaceString "–", " - "
  680.     'This is the en-dash symbol on the Mac.
  681.    
  682.     ReplaceString "…", "..."
  683.     'This is the elipsis symbol on the Mac.
  684.  
  685.     Options.AutoFormatAsYouTypeReplaceQuotes = quotes
  686.  
  687. End Sub
  688.  
  689.  
  690.  
  691. Private Sub TextileEscapeChars()
  692.  
  693.     EscapeCharacter "*"
  694.  
  695.     EscapeCharacter "#"
  696.  
  697.     'EscapeCharacter "_"
  698.  
  699.     'EscapeCharacter "-"
  700.  
  701.     'EscapeCharacter "+"
  702.  
  703.     EscapeCharacter "{"
  704.  
  705.     EscapeCharacter "}"
  706.  
  707.     EscapeCharacter "["
  708.  
  709.     EscapeCharacter "]"
  710.  
  711.     EscapeCharacter "~"
  712.  
  713.     EscapeCharacter "^^"
  714.  
  715.     EscapeCharacter "|"
  716.  
  717.     EscapeCharacter "'"
  718.  
  719. End Sub
  720.  
  721.  
  722.  
  723. Private Function ReplaceHeading(styleHeading As String, headerPrefix As String)
  724.  
  725.     Dim normalStyle As Style
  726.  
  727.     Set normalStyle = ActiveDocument.Styles(wdStyleNormal)
  728.  
  729.    
  730.  
  731.     ActiveDocument.Select
  732.  
  733.    
  734.  
  735.     With Selection.Find
  736.  
  737.    
  738.  
  739.         .ClearFormatting
  740.  
  741.         .Style = ActiveDocument.Styles(styleHeading)
  742.  
  743.         .Text = ""
  744.  
  745.  
  746.        
  747.  
  748.         .Format = True
  749.  
  750.         .MatchCase = False
  751.  
  752.         .MatchWholeWord = False
  753.  
  754.         .MatchWildcards = False
  755.  
  756.         .MatchSoundsLike = False
  757.  
  758.         .MatchAllWordForms = False
  759.  
  760.        
  761.  
  762.         .Forward = True
  763.  
  764.         .Wrap = wdFindContinue
  765.  
  766.        
  767.  
  768.         Do While .Execute
  769.  
  770.             With Selection
  771.  
  772.                 If InStr(1, .Text, vbCr) Then
  773.  
  774.                     ' Just process the chunk before any newline characters
  775.  
  776.                     ' We'll pick-up the rest with the next search
  777.  
  778.                     .Collapse
  779.  
  780.                     .MoveEndUntil vbCr
  781.  
  782.                 End If
  783.  
  784.                                        
  785.  
  786.                 ' Don't bother to markup newline characters (prevents a loop, as well)
  787.                If Not .Text = vbCr Then
  788.                     .InsertBefore headerPrefix
  789.                     .InsertBefore vbCr
  790.                     '.InsertAfter headerPrefix
  791.                End If
  792.                 .Style = normalStyle
  793.             End With
  794.         Loop
  795.     End With
  796. End Function
  797.  
  798. Private Function EscapeCharacter(char As String)
  799.     ReplaceString char, "\" & char
  800. End Function
  801.  
  802. Private Function ReplaceString(findStr As String, replacementStr As String)
  803.     Selection.Find.ClearFormatting
  804.     Selection.Find.Replacement.ClearFormatting
  805.     With Selection.Find
  806.         .Text = findStr
  807.         .Replacement.Text = replacementStr
  808.         .Forward = True
  809.         .Wrap = wdFindContinue
  810.         .Format = False
  811.         .MatchCase = False
  812.         .MatchWholeWord = False
  813.         .MatchWildcards = False
  814.         .MatchSoundsLike = False
  815.         .MatchAllWordForms = False
  816.     End With
  817.     Selection.Find.Execute Replace:=wdReplaceAll
  818. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement