A pedant that hangs out in the dark corner-cases of the web.

Tuesday, May 08, 2012

Updated: Office macro to paste and format TSV text

Outlook (Word) macro to paste TSV text, convert it to a table, then format it using a new style each time.

Sub PasteFormattedTable()
    Dim doc As Word.Document
    Dim sel As Word.Selection
    Dim start As Integer
    Set doc = Application.ActiveInspector.WordEditor
    Set sel = doc.Windows(1).Selection
    start = sel.start
    sel.PasteSpecial Link:=False, DataType:=wdPasteText
    sel.start = start
    sel.ConvertToTable DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitContent
    styles = Array(-162, -208, -250, -222, -236, -176, -194)
    sel.Style = styles(doc.Tables.Count Mod (UBound(styles) + 1))
End Sub

See Getting Started with VBA in Outlook 2010 to get started.

For more style codes, see Create list of built-in style names.

Monday, May 07, 2012

Office macro to paste and format TSV text

Outlook (Word) macro to paste TSV text, convert it to a table, then format it.
Sub PasteBlackTable()
    Dim doc As Word.Document
    Dim sel As Word.Selection
    Dim start As Integer
    Set doc = Application.ActiveInspector.WordEditor
    Set sel = doc.Windows(1).Selection
    start = sel.start
    sel.PasteSpecial Link:=False, DataType:=wdPasteText
    sel.start = start
    sel.ConvertToTable DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitContent
    sel.Style = wdStyleTableMediumShading1 ' medium shading 1 (black) = -162
    'sel.Style = wdStyleTableMediumShading1Accent1 ' medium shading 1 accent 1 (blue) = -176
    'sel.Style = -194 ' medium shading 1 accent 3 (red)
    'sel.Style = -208 ' medium shading 1 accent 3 (green)
    'sel.Style = -222 ' medium shading 1 accent 4 (purple)
    'sel.Style = -236 ' medium shading 1 accent 5 (teal)
    'sel.Style = -250 ' medium shading 1 accent 6 (orange)
End Sub
See Getting Started with VBA in Outlook 2010 to get started.
For more style codes, see Create list of built-in style names.