Use VBA programming in Word to make test paper in writing

Source: Internet
Author: User
Tags extend first row

Chinese test, often used in writing manuscript paper, Word with the template, but there are many restrictions (such as paper type, number of rows and so on relatively fixed), in the examination papers typesetting is quite inconvenient. After trying, the author found a way to use Word VBA programming to realize the test writing manuscript by drawing the form.

I. Preparation of procedures

1. Start Word, click "Tools" continuously, "macros, record new macros ..., fill in the macro name (this example is" writing paper ") in the pop-up dialog box, select the location where the macro will be saved (either in the template or in the current document), as shown in Figure 1.

2. For ease of operation, you can specify this macro as a button in the toolbar. Click the toolbar button in the Record Macro dialog box, on the right side of the pop-up custom dialog box, select the macro command "Normal.newmacros. Writing manuscript", drag it to the "standard" toolbar with the mouse, right-click on it, select the appropriate command to modify the name, Edit a small icon for it, and the result is as shown in Figure 2.

3. Click the Close button in the Record Macro dialog box to display a Record macro toolbar and click the Stop button to stop recording.

4. Select "Tools", "macro", "macro ..." in the dialog box, select the macro name "writing manuscript", click the "Edit" button, open the VBA programming window. Input the following code:

Sub Writing manuscript ()

UserForm1.CommandButton1.Enabled = True

UserForm1.Show

End Sub

5. Insert a form USERFORM1, insert 4 labels, 4 text boxes, and a button. Set their related properties, where the Text property value of the required number of lines box is set to 25, number of columns required the text box has a value of 20, the Text property value of the line spacing textbox is set to 0.5, and the text box The property value is set to 0.4, as shown in Figure 3

6. Double-click the command button CommandButton1, input the following code:

Private Sub CommandButton1_Click ()

Dim N As Integer ' defines a variable as an integral type

n = 1

ActiveDocument.Tables.Add Range:=Selection.Range, Numrows:=val (TextBox1.Text) * 2 + 1, NumColumns _

: =val (TextBox2.Text), Defaulttablebehavior:=wdword9tablebehavior, autofitbehavior:=wdautofitfixed

Selection.endkey Unit:=wdrow, Extend:=true

Selection.Cells.Borders (wdbordervertical). LineStyle = wdLineStyleNone

Selection.tables (1). Rows.heightrule = wdrowheightexactly

' Set table row height to fixed value

Selection.tables (1). Rows.height = CentimetersToPoints (Val (TextBox3.Text))

' Set table row height to set value as line spacing

Selection.tables (1). Rows (1). Height = CentimetersToPoints (Val (Textbox4.text))

' Set first row height to set value

Do While n < Val (TextBox1.Text) + 1

Selection.endkey Unit:=wdline

Selection.MoveRight Unit:=wdcharacter, count:=2

' Move the insertion point to the next line

Selection.tables (1). Rows (2 * n). Height = Selection.tables (1). Columns (1). PreferredWidth

' Set row is higher than column width

Selection.endkey Unit:=wdrow, Extend:=true

Selection.endkey Unit:=wdline

Selection.MoveRight Unit:=wdcharacter, count:=2

' Move the insertion point to the next line

Selection.endkey Unit:=wdrow, Extend:=true

Selection.Cells.Borders (wdbordervertical). LineStyle = wdLineStyleNone

' Get rid of the inner border of this line, just the Borders

n = n + 1

Loop

Selection.tables (1). Rows (Val (TextBox1.Text) * 2 + 1). Height = CentimetersToPoints (Val (Textbox4.text))

' Set last row height to set value

Selection.endkey Unit:=wdrow, Extend:=true

Selection.Cells.Borders (wdbordervertical). LineStyle = wdLineStyleNone

Selection.tables (1). Rows.alignment = wdAlignRowCenter

' Table Center

With Selection.tables (1)

. Borders (Wdborderleft). LineWidth = wdlinewidth150pt

. Borders (wdborderright). LineWidth = wdlinewidth150pt

. Borders (Wdbordertop). LineWidth = wdlinewidth150pt

. Borders (Wdborderbottom). LineWidth = wdlinewidth150pt

' Set the table border to a thick line

End With

Selection.endkey Unit:=wdline

Unload Me

End Sub

Second, the use

This program's manuscript is clearly not suitable for direct use in the document compared to Word's Paper wizard or pad add-in, but it has an obvious advantage in making composition papers. One is not limited by paper type, second, because it is a table, so can be free to add, delete ranks as needed, but also in the appropriate place to mark the words required by the composition.

Before applying, should set up the test paper template first, then carry on the experiment, in order to find out the most suitable size and row number of the test paper, when you need to make a manuscript, you can click the "Writing Pad" button directly on the "standard" toolbar, or you can click "Tools", "Macros", "Macros ...", and then open the Macros dialog box, and in the left combo box, select "Composition manuscript" macro, and then click "Run", you can start the macro program, in the subsequent "composition of the paper set" window, modify the default settings as needed, and finally, click the "OK" button, the program will automatically draw the composition of the paper form, if the paper is more than one page (or a column), select the middle of the paper to delete Figure 4 Below is the A3 paper in the two columns of 20x16, 20x10 manuscript.

Third, export import

In the VBA programming window, select the UserForm1 form and Newmacros module in Project Explorer, and Export the Userform1.frm, USERFORM1.FRX, and Newmacros.bas files. As shown in Figure 5.

When you need to use this program on another computer, copy the backup, start the VBA programming window, import userform1.frm, and Newmacros.bas through "file", "Import file", and use it in Word.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.