Current location - Training Enrollment Network - Books and materials - Use VBA to locate tables and fill in data in word template.
Use VBA to locate tables and fill in data in word template.
Give you an example, and then you can improve your:

Dimwdapapp is WORD. The application defines a word object.

Turn wdDoc into Word. "Document" defines a WORD document object.

Set Work Order as Worksheet defines worksheet objects.

Dim as long as I am

Instantiate a WORD object.

Set wdApp = new word. app; application

Create a WORD document

Set wdDoc = wdApp. Documents. adding

Assign values to worksheet variables.

Set wkSheet = ThisWorkbook. Worksheet ("WriteWord")

Save the newly created WORD document.

wdDoc。 Save as this workbook. Path and path. " \test.docx "

WdDoc。 Close closes the newly created WORD document.

Use wdApp

Open a WORD document

. Documents. Open this workbook. Path and path. " \test.docx "

Loop converts the records in the worksheet into the contents of the WORD document.

For I = 2 to wkSheet. Scope ("a 1048576"). End (xlUp). row

Title content

. choose Type the text "Dear"&; Trim (work order. Cell (i, 1). Value)

Determine people's address according to gender.

If wkSheet. Cell (I, 2). Value = "male" and then.

. choose Type the text "Sir:"

other

choose Type the text "Madam:"

.........