Share via

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

Answer accepted by question author

Jay Freedman 207.7K 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

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K 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

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2024-01-20T21:12:30+00:00

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

    Regardless of whether you go through Insert > Quick Parts > Save Selection to Quick Part Gallery or Insert > Tables > Save Selection to Quick Tables Gallery, or just Alt+F3, you get the same Create New Building Block dialog. The only difference is whether the Gallery dropdown is initially set to Tables or Quick Parts or AutoText; if you select Tables there, the result is the same in all three cases.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Charles Kenyon 167.5K 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:

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. 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.

    Was this answer helpful?

    0 comments No comments