How to change the setting of AutoFitBehavior of table columns to "Fixed Column Width" for all new inserted tables in Word?

Anonymous
2024-01-20T12:30:41+00:00

Hello everyone,

I can change table Layout of "AutFit" option to "Fixed Column Width" from Layout tab as it shown in the following image:

Now the question is that how to set AutoFit setting to "Fixed Column Width" option as default so I don't need in future to go to Layout then AutoFit then choosing Fixed Column Width option every time I insert a new table?

Does anyone know how to solve this problem?

Microsoft 365 and Office | Word | For education | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Jay Freedman 206.1K Reputation points Volunteer Moderator
    2024-01-20T19:12:51+00:00

    Hi Ahmed,

    Your Userform is a good start -- it works, and it produces the desired table.

    When you export a userform, Word creates the two files you mentioned, with .frm and .frx extensions. The .frm file is a text file that contains the VBA code of the form, plus a few properties such as its title, size, and screen location. The .frx file is a binary code file that contains the visible parts of the form -- the text boxes, buttons, and so on. When you ask to import a userform into the VBA editor, you need to have both files in the same folder. The Import dialog asks for only the .frm file, but it imports both files at the same time.

    An important point about creating Userforms is that extra care should be taken to make them work the way most users come to expect from the way Microsoft's built-in dialog boxes work. Some of those properties are not obvious, so I'll point them out.

    It's possible to display a userform with just the single line of code

        UserForm2.Show 
    

    but that works well only for very simple userforms such as this one. Many userforms are intended to run many times in the same document, or they may start up other userforms, among other complications. A better policy is to run every userform from a macro in a separate module, with code like this:

    Sub RunForm() 
    
        Dim dlg As UserForm2 
    
        Set dlg = New UserForm2 
    
        dlg.Show 
    
        Set dlg = Nothing 
    
    End Sub 
    

    Another thing about userforms is the difference between the Unload command and the Hide method. Unload tells VBA to remove the userform from memory, including its code. After the Unload command runs, no other code from inside the form can run. Many userforms pass data from their text boxes (or list boxes, etc.) to the macro that started the userform, but Unload won't let that happen. It's better to use the Hide method of the userform to make the form invisible, but leave the code running in the background. Control then returns to the macro (like RunForm), and the code is finally unloaded when the Set dlg = Nothing statement runs.

    Since Hide is a method of the userform, the syntax is

       Me.Hide
    

    where "Me" refers to the running userform.

    In the code below, you'll see that I put the Me.Hide statement at the end of the CommandButton1_Click procedure, instead of having Unload Me at the beginning. I also added the Trim function to the tests for empty text boxes, because a user is likely to press the space bar to get rid of a number in the box instead of using the Delete or Backspace key. The Trim function removes spaces from the start or end of strings, so this way the condition will be True if the box is completely empty or contains only one or more spaces.

    It's often a good idea to supply text boxes with default values. Then, if the defaults are what the user wants, they need only click OK (or press Enter) to accept them. If they want some other values, they can overtype the defaults. To put the defaults in, you use the Userform_Initialize procedure. You can click the Userform item in the left-hand dropdown at the top of the macro editor's code window, and then select Initialize in the right-hand dropdown to insert the proper Sub and End Sub statements. Then fill in the procedure's code. You can see this in the code below.

    One more suggestion: You should always rename modules, userforms, labels, and controls with names that indicate their purpose. For example, it would be useful in this userform to rename TextBox1 as tbxColumns and TextBox2 as tbxRows. The prefixes of the names are optional, but they are useful for avoiding bugs. In this case, "tbx" is an abbreviation of "textbox"; I use "cmd" for command buttons, "lbx" for list boxes, and so forth.

    For more information about userforms, read some of the articles at https://wordmvp.com/FAQs/Userforms.htm .

    Try the following code in your userform.

    Private Sub CommandButton1_Click() 
    
    If Trim(TextBox1.Value) = "" Or Trim(TextBox2.Value) = "" Then 
    
    MsgBox "You did not enter the number of columns or the number of rows.", vbExclamation 
    
    Me.Hide 
    
    Exit Sub 
    
    End If 
    
        Dim oTable As Table 
    
        Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _ 
    
            NumRows:=TextBox2.Value, NumColumns:=TextBox1.Value, AutoFitBehavior:=wdAutoFitFixed) 
    
        With oTable 
    
            '.Columns.Width = InchesToPoints(2) 
    
            .Borders.InsideLineStyle = wdLineStyleSingle 
    
            .Borders.OutsideLineStyle = wdLineStyleSingle 
    
        End With 
    
    Me.Hide 
    
    End Sub 
    
    Private Sub UserForm_Initialize() 
    
        TextBox1.Value = 2 
    
        TextBox2.Value = 1 
    
        ' select the number in TextBox1 
    
        TextBox1.SelStart = 0 
    
        TextBox1.SelLength = 1 
    
        ' make the Enter key "push" the OK button 
    
        CommandButton1.Default = True 
    
    End Sub
    
    2 people found this answer helpful.
    0 comments No comments
Answer accepted by question author
  1. Jay Freedman 206.1K Reputation points Volunteer Moderator
    2024-01-20T13:24:14+00:00

    One way to do this is to create a fixed-width table and add it to the Tables gallery of the Quick Parts collection. Then it will always be available in the Insert Table > Quick Tables menu.

    Start by creating a table of the preferred size in any document and setting it to Fixed Column Width. While the whole table is still selected, go to Insert > Quick Parts > Save Selection to Quick Part Gallery.

    .

    In the dialog that opens, enter a name for the Quick Part, choose the Tables gallery, and select (or create) a category. Also select a template in which to save the Quick Part -- available choices include Normal.dotm, Building Blocks.dotm, plus the template that the current document is based on if not the Normal template. Then click OK.

    .

    When you want a new table in any document, click Insert > Table Quick Tables and click your fixed-width table in the gallery.

    .

    An alternative method is to write a macro that both inserts a table of the preferred size and applies the fixed column width. The macro can be assigned to a Quick Access Toolbar button and/or a keyboard shortcut. The code could be something like this:

    Sub FixedWidthTable() 
    
        Dim oTable As Table 
    
        Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _ 
    
            NumRows:=1, NumColumns:=2, AutoFitBehavior:=wdAutoFitFixed) 
    
        With oTable 
    
            .Columns.Width = InchesToPoints(2) 
    
            .Borders.InsideLineStyle = wdLineStyleSingle 
    
            .Borders.OutsideLineStyle = wdLineStyleSingle 
    
        End With 
    
    End Sub
    
    2 people found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-01-20T17:07:12+00:00

    Hi Jay Freedman,

    Thank you so much for the macro.

    For the first way about making Quick Part, that is helpful.

    For me, I prefer the second way with the code that that you gave.

    I thought about making UserForm to enter the number of columns and number of rows of the table.

    so I took the the code that exists inside the macro you provided, and just I added these few lines when the user don't enter the number of columns or the number of rows:

    If TextBox1.Value = "" Or TextBox2.Value = "" Then
    
    MsgBox "You did not enter the number of columns or the number of rows.", vbExclamation
    
    Exit Sub
    
    End If
    

    Although the person in the following video is talking about something other than this subject but he helped me knowing a method that I used in the UserForm:

    https://youtu.be/8lKzMuk2nO0?si=ptRQJ82ceykw89P3

    Actually I'm not an expert in creating UserForms, so when you have time you can see what I did in the following UserForm and give me any directions if I need:
    https://1drv.ms/f/s!AkZebsOuKoY7uhcCfpYvmepH8geG?e=9upMEn

    In the last link I put, there exist two files of UserForm, one file with frm extension and the other file is with frx, and I'm not sure which one I should put, so I put them both together in one folder.

    In the following, there are some screenshots after I used the UserForm:

    Thank you so much for the help.

    0 comments No comments
  2. Anonymous
    2024-01-20T18:14:57+00:00

    An alternative method is to write a macro that both inserts a table of the preferred size and applies the fixed column width. The macro can be assigned to a Quick Access Toolbar button and/or a keyboard shortcut. The code could be something like this:

    Sub FixedWidthTable() 
    
    
    
        Dim oTable As Table 
    
    
    
        Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _ 
    
    
    
            NumRows:=1, NumColumns:=2, AutoFitBehavior:=wdAutoFitFixed) 
    
    
    
        With oTable 
    
    
    
            .Columns.Width = InchesToPoints(2) 
    
    
    
            .Borders.InsideLineStyle = wdLineStyleSingle 
    
    
    
            .Borders.OutsideLineStyle = wdLineStyleSingle 
    
    
    
        End With 
    
    
    
    End Sub 
    

    Mister Jay I noticed something about this macro, I noticed that if the entered number of columns and the entered number of rows are somehow big, for example, if the entered number of colmns is 25 and the entered number of rows is 25, then it shows the following error message:

    and then I noticed if I make the following line as a comment, then this error message does not appear when I enter somehow a big number for the columns or for the rows:

            .Columns.Width = InchesToPoints(2) 
    

    Also, I noticed that in the last link that I shared in my previous reply, I was adding unneeded lines of code in the UserForm so I deleted them, also I noticed that I did mistakes in the UserForm and then I corrected that mistakes, so I updated the folder that has the UserForm and I deleted the old UserForm and I put other one after the modifications.

    The new UserForm is existing in the last link that I shared in my previous reply.

    0 comments No comments
  3. Charles Kenyon 160.1K Reputation points Volunteer Moderator
    2024-01-20T20:25:09+00:00

    I would, off-hand, suggest saving to the Quick Tables gallery rather than Quick Parts, which I feel is over-used.

    References:

    1 person found this answer helpful.
    0 comments No comments