Create a Wiki Table Using a Word Macro
Contents
This page provides a Microsoft Word macro to make it easier to create tables in the wiki. Start with a tab-separated list, and it will transform it into the proper syntax to be a wiki table.
Usage
Start with a tab-delimited list. If you have a normal word table, you can get to a tab-delimited list by selecting the table, copying, and then pasting only values.
Then:
- Select all rows of the tab-delimited list that you want converted
- Run the FormatWikiTable macro (installation steps further below)
The resulting text will be formatted syntax you can paste directly into a wiki page while editing.
Example:
If you start with the following text:
- Header1<tab>Header2<tab>Header3
- Value1<tab>Value2<tab>Value3
You will get this:
{| class="wikitable"
! Header1 !! Header2 !! Header3
|-
| Value1 || Value2 || Value3
|}
Which then displays in the wiki page like this:
Header1 | Header2 | Header3 |
---|---|---|
Value1 | Value2 | Value3 |
Installation
To install, just add the following macro to your Microsoft Word macros.
This can be done a couple of ways, e.g.:
- If you have other macros, then just open the Visual Basic editor and paste the below macro at the end of your existing macros.
- If you do not have any other macros, then record a new macro, and title it FormatWikiTable. Stop recording immediately after starting. Then edit the newly created macro, replacing its existing code with the macro code below.
Macro Code
Place the following macro code in the Visual Basic editor of Word:
Sub FormatWikiTable() ' ' This macro takes a tab-delimited table (tabs delimiting columns, paragraphs delimiting rows) ' and turns it into a wiki-formatted table. ' ' The table contents must be selected prior to running the macro. ' Dim i, iNumberParagraphs ' ' If there is no selection, exit If Len(Selection.Text) = 1 Then MsgBox "Must select the tab-delimited table you want to convert" Exit Sub End If ' ' Get paragraphs before we do anything iNumberParagraphs = Selection.Paragraphs.Count ' ' Replace tabs with double bar (entire selection) With Selection.Find .Text = vbTab .Replacement.Text = " || " .Forward = True .Wrap = False End With Selection.Find.Execute Replace:=wdReplaceAll ' ' Go to beginning Selection.HomeKey Unit:=wdLine ' ' Select 1st paragraph (header line) Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend ' ' Replace double bars with double exclamation With Selection.Find .Text = " || " .Replacement.Text = " !! " .Forward = True .Wrap = False End With Selection.Find.Execute Replace:=wdReplaceAll ' ' Add table instantion line and beginning of header row Selection.HomeKey Unit:=wdLine Selection.TypeText Text:="{| class=""wikitable""" + vbCrLf + _ "! " ' ' Loop through remaining (nonheader) lines ' Starting with the 2nd paragraph i = 1 While i < iNumberParagraphs ' i = i + 1 ' Selection.MoveDown Unit:=wdParagraph, Count:=1 ' ' Create line delimiter and start of row Selection.TypeText Text:="|-" + vbCrLf + _ "| " Wend ' ' Finish off table Selection.MoveDown Unit:=wdParagraph, Count:=1 Selection.TypeText Text:="|}" + vbCrLf ' End Sub
Last revision by Troy.smyrnios, 2014-12-18