Use VBA to process word and Word tables

Source: Internet
Author: User

1. Create a table in word and insert text
This example inserts a table with three rows and six columns at the beginning of the document. You can use the for each... Next structure to traverse every cell in the table cyclically. In for each... in the next structure, the insertafter method is used to add text ("1st cells", "2nd cells", etc.) to table cells, otable. the AutoFormat attribute is used to specify the table format. ++ ++
'Basic function: Insert a table with three columns and six rows, and insert data to each table row.
'Reference component: You can only use Word. Application by referencing or Microsoft Word x.0 Object Library in the project.
'Prepared by: flashasp
++ ++
Dim WD as new word. Application
WD. Documents. Add documenttype: = wdnewblankdocument
Set odoc = WD. activedocument
Set otable = odoc. Tables. Add (range: = odoc. Range (START: = 0, end: = 0), numrows: = 6, numcolumns: = 3)
Icount = 1
For each ocell in otable. range. Cells
Ocell. range. insertafter "no." & icount & "cell"
Icount = icount + 1
Next ocell
Otable. AutoFormat format: = wdtableformatcolorful2, applyborders: = true, applyfont: = true, applycolor: = true
'Move the cursor to the end
WD. selection. endkey unit: = wdstory
'Insert pagination Operator
WD. selection. insertbreak type: = wdpagebreak
WD. Visible = true
WD. showme
Set WD = nothing
++ ++

2. insert sequence numbers in columns and rows of the table
1. if you want to insert the sequence number in the first column of the table, you only need to insert the for each... the content in the next structure can be changed to the following program lines. The insertafter method is used to add sequence numbers ("1st rows", "2nd rows", and so on) to table cells ).
If icount mod 4 = 1 then
Ocell. range. insertafter "no." & (icount-1)/4 + 1 & "row"
End if
Icount = icount + 1
2. If you want to insert the sequence number from the second row of the table, replace the above Code:
If icount mod 4 = 1 and icount> 4 then
Ocell. range. insertafter "no." & (icount-1)/4 & "row"
End if
Icount = icount + 1
 
3. insert date in the column of the table

(1) If you want to insert a date in the first column of the table, you can use for each... next structure to traverse every cell in the table cyclically. When a cell is determined to be the first column, the insert date is used. Formart (date,...) is used to specify the date format. In the following example, the date starts from date + 1 (the second day of the current date). You can customize it as needed.
Set odoc = activedocument
Set otable = odoc. Tables. Add (range: = odoc. Range (START: = 0, end: = 0), numrows: =
4, numcolumns: = 4)
Icount = 1
For each ocell in otable. range. Cells
If icount mod 4 = 1 and icount> 4 then
Ocell. range. insertafter format (date + (icount-1)/4, "YYYY. Mm. dd ")
End if
If icount mod 4 = 2 and icount> 4 then
Ocell. range. insertaftercweekname (weekday (date + (icount-1)/4 ))
End if
Icount = icount + 1
Next ocell
Otable. AutoFormat format: = wdtableformatcolorful1, applyborders: = true, AP
Plyfont: = true, applycolor: = true

(2) to insert the week value in the second column of the table, insert the following rows in the for each... Next structure in the preceding example:
If icount mod 4 = 2 and icount> 4 then
Ocell. range. insertafter cweekname (weekday (date + (icount-1)/4 ))
End if
Where, weekday (date) returns a value (1 ~ 7), indicating "Sunday "~ "Saturday", the cweekname array must be defined:
Dim cweekname (7)
Cweekname (1) = "Sunday"
Cweekname (2) = "Monday"
Cweekname (7) = "Saturday"

4. Set different formats based on the content of cells.
The preceding table is used as an example. to change the format of all "Saturday" and "Sunday" rows to a blue background, you only need to add the following lines after the preceding program (the table format is changed to wdtableformatcolorful2, change the number of rows to 12 ). Use for each again in the program... the next structure traverses each row in the table (rows). If a row is detected to meet the conditions ("Saturday" or "Sunday"), select a row (selection. selectrow), and change its attribute to the required format (in this example, the blue background is used ).
Icount = 1
For each rows in otable. range. Rows
If (weekday (date + (icount-1) = 7 or weekday (date + (icount-1) = 1)
And icount> 1
Then
Selection. selectrow
With selection. Cells
With. Shading
. Texture = wdtexturenone
. Foregroundpatterncolorindex = wdauto
. Backgroundpatterncolorindex = wdblue
End
End
End if
Icount = icount + 1
Selection. movedown unit: = wdline, Count: = 1
Next rows

5. Enable a pre-defined Word template and replace a specified string in the template
'*************************************** **************************************** *********************
'Reference component: You can only use Word. Application by referencing or Microsoft Word x.0 Object Library in the project.
'Name: openwordandreplacechar (filename as string, replacedstr as string, replacementstr as string)
'Function: Enable a pre-defined Word template and replace a specified string in the template
'Call: Call openwordandreplacechar ("001.doc"," $1 "," Baby :)")
'Parameter: Filename: name of the Word file, replacedstr: character to be replaced in word, replacementstr: string passed to the word in the program
'Prepared by: flashasp
'*************************************** **************************************** ***********************
Public sub openwordandreplacechar (filename as string, replacedstr as string, replacementstr as string)
Dim wordapp as new word. Application
Dim wordarange as word. Range
Dim wordselection as word. Selection
Dim replacesign as Boolean
Dim I as integer
Filename = app. Path & "/" & filename
Set wordapp = Createobject ("word. application ")
Wordapp. Visible = true
Wordapp. Documents. Open (filename)
Set wordselection = wordapp. Selection
'Specifies the file editing location
Set wordarange = wordapp. activedocument. Range (0, 1)
'Activate the editing location
Wordarange. Select
'Indicates whether the initialization is successful
If replacedstr <> "" And replacementstr <> "" then
Replacesign = true
Do While replacesign
Replacesign = wordarange. Find. Execute (replacedstr, matchcase, wdfindcontinue, replacementstr, true)
Loop
End if
'Back to print
Wordapp. activewindow. View. type = wdprintview
End sub

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.