Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jan 2007
    Location
    Denmark
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am writing a macro that will convert text to a table. The text shows two formulas separated by a mathematical arrow. I inserted two tabs on either side of the arrow to make it easier to read.

    I want the table to fit to contents. Using the macro recorder, I converted to table and set the AutoFit Behavior to Autofit to Contents. When I record the macro, it works fine. When I run it later, it fits the table to the window.

    Sub Formula_Table()
    '
    ' Formula_Table Macro
    ' Macro recorded 07/01/2010 by bill
    '
    Selection.Style = ActiveDocument.Styles("Formula")
    Selection.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=5, _
    NumRows:=3, AutoFitBehavior:=dAutoFitContent
    With Selection.Tables(1)
    .Style = "Table Grid"
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With
    With Selection.Tables(1)
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
    .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    End With
    With Options
    .DefaultBorderLineStyle = wdLineStyleSingle
    .DefaultBorderLineWidth = wdLineWidth050pt
    .DefaultBorderColor = wdColorAutomatic
    End With
    End Sub

    What am I missing?

    Thanks! Bill

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Try this version
    Code:
    Sub Formula_Table()
    Selection.Style = Activedocument.Styles("Formula")
    Selection.ConvertToTable Separator:=wdSeparateByTabs
    With Selection.Tables(1)
      .Style = "Table Grid"
      .ApplyStyleHeadingRows = True
      .ApplyStyleLastRow = True
      .ApplyStyleFirstColumn = True
      .ApplyStyleLastColumn = True
      .Columns.Autofit
    End With
    End Sub
    You shouldn't need to define the number of rows and columns in the ConvertToTable since this is usually worked out correctly based on what you have selected. Similarly you won't need to set all the borders specifically since this will be defined by the Table Style you have applied - modify the style if the borders aren't right.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    New Lounger
    Join Date
    Jan 2007
    Location
    Denmark
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Andrew,

    Thanks for the suggestion. Unfortunately, it doesnít work either. I attach a doc showing what I start with, the current result, and what Iím going for.

    I have tried to figure out how to set the autofit behavior in the table style, but canít figure that out. I also tried to delete reference to the table style in the macro, but that doesnít help either.

    Cheers, Bill
    Attached Files Attached Files

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    The problem is that you have two empty columns that Word guesses you will want to use so it makes them 2.2cm wide when it autofits the columns. I guess you could have expected this width to be tiny since the columns are empty but that doesn't appear to be what happens. What about deleting those columns?
    Code:
    Sub Formula_Table()
    Selection.Style = Activedocument.Styles("Formula")
    Selection.ConvertToTable Separator:=wdSeparateByTabs
    With Selection.Tables(1)
      .Style = "Table Grid"
      .ApplyStyleHeadingRows = True
      .ApplyStyleLastRow = True
      .ApplyStyleFirstColumn = True
      .ApplyStyleLastColumn = True
      .Columns(4).Delete
      .Columns(2).Delete
      .Columns.Autofit
    End With
    End Sub
    You could avoid these columns if your source text didn't have double tabs before you converted the text to a table. You could also modify the code to specifically size the empty columns after the autofit. Note that you would be constrained to a minimum column width which may depend on the spacing settings in the table options.
    Code:
      .Columns.AutoFit
      .Columns(2).Width = CentimetersToPoints(0.4)
      .Columns(4).Width = CentimetersToPoints(0.4)
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    New Lounger
    Join Date
    Jan 2007
    Location
    Denmark
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Andrew,

    Thanks for the help. You are right indeed that the two tabs on either side of the arrow were confusing Word. We will just limit them to one tab on each side when we create the formulas. No problem.

    I would have liked the middle column with the arrow to be a minimum of 0.5Ē, because it sets the arrow off better. But when I do that, for some reason, Work makes columns 1 and 3 wider than ďfit to contentĒ.

    So here is the macro that finally gets the job done. Itís probably not very elegant, but itís the best I can do at the moment. I appreciate your input. Further comments appreciated.

    Cheers, Bill

    Sub Formula_Table ()
    ' Formula_Table4 Macro
    ' Macro recorded 18/01/2010 by bill
    '
    Selection.Style = ActiveDocument.Styles("Formula")
    Selection.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=3, _
    NumRows:=3, AutoFitBehavior:=wdAutoFitContent
    With Selection.Tables(1)
    .Style = "Table Grid"
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With
    With Selection.Tables(1)
    .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    .Borders(wdBorderRight).LineStyle = wdLineStyleNone
    .Borders(wdBorderTop).LineStyle = wdLineStyleNone
    .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
    .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
    .Borders.Shadow = False
    End With
    With Options
    .DefaultBorderLineStyle = wdLineStyleSingle
    .DefaultBorderLineWidth = wdLineWidth050pt
    .DefaultBorderColor = wdColorAutomatic
    End With
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.SelectColumn
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

    With Selection.Tables(1)
    .Columns.AutoFit
    End With

    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.MoveUp Unit:=wdLine, Count:=1

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •