none
Formatting into two columns RRS feed

  • Question

  • I'm making a VBA program to generate a Word document from Excel data. It needs to be in a certain format; so far things have been going ok. My problem is formatting a table into two columns.

    The problem is I mean two page columns, not table columns. So we can ignore the table, say I have a paragraph of text or something! I want two columns like a newspaper etc

    I can't find anything on this because everything that comes up is related to table columns, not page columns - I don't have a whole lot of experience with VBA, so I hope someone can help me.

    Cheers

    Friday, December 16, 2011 12:58 PM

Answers

  • The best thing to do would be to start with a template that is set up with the two columns in it.

    However, to start from scratch, use:

    Dim DocumentwithColumns As Document
    Set DocumentwithColumns = Documents.Add
    With DocumentwithColumns.PageSetup.TextColumns
        .SetCount Numcolumns:=1
        .Add EvenlySpaced:=True
        .Spacing = 10 'points
    End With


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by hanvyj Monday, December 19, 2011 1:25 PM
    • Unmarked as answer by hanvyj Monday, December 19, 2011 2:18 PM
    • Marked as answer by hanvyj Monday, December 19, 2011 2:55 PM
    Saturday, December 17, 2011 5:44 AM

All replies

  • The best thing to do would be to start with a template that is set up with the two columns in it.

    However, to start from scratch, use:

    Dim DocumentwithColumns As Document
    Set DocumentwithColumns = Documents.Add
    With DocumentwithColumns.PageSetup.TextColumns
        .SetCount Numcolumns:=1
        .Add EvenlySpaced:=True
        .Spacing = 10 'points
    End With


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by hanvyj Monday, December 19, 2011 1:25 PM
    • Unmarked as answer by hanvyj Monday, December 19, 2011 2:18 PM
    • Marked as answer by hanvyj Monday, December 19, 2011 2:55 PM
    Saturday, December 17, 2011 5:44 AM
  • The best thing to do would be to start with a template that is set up with the two columns in it.

    However, to start from scratch, use:

    Dim DocumentwithColumns As Document
    Set DocumentwithColumns = Documents.Add
    With DocumentwithColumns.PageSetup.TextColumns
        .SetCount Numcolumns:=1
        .Add EvenlySpaced:=True
        .Spacing = 10 'points
    End With


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org


    Thanks, thats exactly what I need, however it seems to work fine then revert back when I scroll up or down...

    The issue was that when I do that it changes the whole document. Which is what happens when you just press the button in word. What I needed was a specific table to be in two rows, which can only be done by selecting it, then choosing two columns. I think I have it working by using a nasty loop to select all the rows in the table and using Selection.PageSetup.

    Thanks!

    Edit: didn't go as well as I thought, see reply below


    • Edited by hanvyj Monday, December 19, 2011 2:30 PM
    Monday, December 19, 2011 1:54 PM
  • Actually Scrap that, I'm still having a problem...

    There seems to be a discrepancy with textColumns behavior in VBA compared to Word. For example, when I select some text in word and press the two columns icon only the selected text will go into two columns (the behavior I want).

    If I go to "Record Macro", select the text and press the go into two columns it repeats this behavior, looking at the macro it seems fine:

    With wrdApp.Selection.PageSetup.TextColumns
                .SetCount NumColumns:=1
                .EvenlySpaced = False
                .LineBetween = True
            End With
            wrdApp.Selection.PageSetup.TextColumns.Add Width:=CentimetersToPoints(11.79), _
                Spacing:=CentimetersToPoints(1.27), EvenlySpaced:=False
    

    However, running the macro I just created shows the same behavior as my VBA program - it makes the whole document (rather than just the selection) into two columns.

    Is this a bug? I can't think of a work arround.

    Monday, December 19, 2011 2:24 PM
  • Solved other problem, used section breaks!

    InsertBreak wdSectionBreakContinuous

    Monday, December 19, 2011 2:55 PM