Guest User

Untitled

a guest
Jan 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. Rem 1) select the text lines representing a table whose columns are separated by the tab character
  2. Rem 2) run this macro
  3. Rem 3) result: the tab characters are replaced by two or more space characters such a way each column
  4. Rem has the same number of characters
  5. Rem 4) you may then copy/paste those text lines inside HTML <pre>...</pre> it will look like a table
  6. Sub tabs_to_spaces()
  7. Dim par As Paragraph
  8. Dim pars() As Paragraph
  9. Dim arr(50) As Integer ' 50 tabs maximum per line
  10.  
  11. Application.ScreenUpdating = False
  12.  
  13. max_ztab = -1
  14. For Each par In Selection.Paragraphs
  15. old_x = 1
  16. ztab = LBound(arr)
  17. Do While old_x <> 0
  18. x = InStr(old_x, par.Range.Text, Chr(9))
  19. If x = 0 Then
  20. zlen = Len(par.Range.Text) - old_x + 1
  21. Else
  22. zlen = x - old_x
  23. End If
  24. If zlen > arr(ztab) Then arr(ztab) = zlen
  25. If ztab > max_ztab Then max_ztab = ztab
  26. ztab = ztab + 1
  27. If x = 0 Then
  28. Exit Do
  29. Else
  30. old_x = x + 1
  31. End If
  32. Loop
  33. Next
  34.  
  35. ReDim pars(Selection.Paragraphs.Count)
  36. For i = 1 To Selection.Paragraphs.Count
  37. Set pars(i) = Selection.Paragraphs(i)
  38. Next
  39.  
  40. For i = 1 To UBound(pars)
  41. Set par = pars(i)
  42. old_x = 1
  43. ztab = LBound(arr)
  44. Do
  45. x = InStr(old_x, par.Range.Text, Chr(9))
  46. If x = 0 Then
  47. Exit Do
  48. End If
  49. Call par.Range.Characters(x).Delete
  50. zlen = arr(ztab) - (x - old_x) + 2 ' + 2 spaces between each column
  51. ztab = ztab + 1
  52. Call par.Range.Characters(x).InsertBefore(Space(zlen))
  53. old_x = x + zlen
  54. Loop
  55. Next
  56.  
  57. Application.ScreenUpdating = True
  58.  
  59. End Sub
  60.  
  61. Sub tabs_to_markdown()
  62. Dim par As Paragraph
  63. Dim pars() As Paragraph
  64. Dim arr(50) As Integer ' 50 tabs maximum per line
  65.  
  66. Application.ScreenUpdating = False
  67.  
  68. ' calculate array "arr" containing column widths, and max_ztab
  69. max_ztab = -1
  70. For Each par In Selection.Paragraphs
  71. old_x = 1
  72. ztab = LBound(arr)
  73. Do While old_x <> 0
  74. x = InStr(old_x, par.Range.Text, Chr(9))
  75. If x = 0 Then
  76. zlen = Len(par.Range.Text) - old_x + 1
  77. Else
  78. zlen = x - old_x
  79. End If
  80. If zlen > arr(ztab) Then arr(ztab) = zlen
  81. If ztab > max_ztab Then max_ztab = ztab
  82. ztab = ztab + 1
  83. If x = 0 Then
  84. Exit Do
  85. Else
  86. old_x = x + 1
  87. End If
  88. Loop
  89. Next
  90.  
  91. ReDim pars(Selection.Paragraphs.Count)
  92. For i = 1 To Selection.Paragraphs.Count
  93. Set pars(i) = Selection.Paragraphs(i)
  94. Next
  95.  
  96. For i = 1 To UBound(pars) ' CAUTION must be 1, not lbound(pars)
  97. Set par = pars(i)
  98. Call par.Range.InsertBefore("| ")
  99. old_x = 1 + Len("| ")
  100. ztab = LBound(arr)
  101. While old_x < par.Range.Characters.Count
  102. ' get the position of the next TAB character
  103. x = InStr(old_x, par.Range.Text, Chr(9))
  104. If x <> 0 Then
  105. ' delete the TAB character
  106. Call par.Range.Characters(x).Delete
  107. Else
  108. x = par.Range.Characters.Count
  109. End If
  110. ' insert
  111. column_right = Space(arr(ztab) - (x - old_x) + 1) + "| "
  112. Call par.Range.Characters(x).InsertBefore(column_right)
  113. old_x = x + Len(column_right)
  114. ' next column
  115. ztab = ztab + 1
  116. Wend
  117. Next
  118.  
  119. Application.ScreenUpdating = True
  120.  
  121. End Sub
Add Comment
Please, Sign In to add comment