2010年12月16日星期四

Automatic processing Using Word VBA form

Microsoft Word 97 is the familiar word processing software, powerful features for our work provides a great help. The Visual Basic for Applications (VBA) Word 97 application to add a lot more features, reasonable and appropriate use of VBA to provide users with great convenience. Introduce a few to use the following automatic processing of Word VBA programming form examples.
1. To create tables, insert text
In this case the function is inserted in the document at the beginning of a 3 line 4 columns. For Each ... Next structure can be used to loop through each cell in the table. In the For Each ... Next structure, InsertAfter method is used to add text to the table cell ("Cell 1", "cell 2", etc.), oTable.AutoFormat attribute is used to specify the form to apply format. Run shown in Figure 1:
@ @ 0869600.JPG; Figure 1 @ @
Set oDoc = ActiveDocument
Set oTable = oDoc.Tables.Add (Range: = oDoc.Range (Start: = 0, End: = 0), NumRows
: = 3, NumColumns: = 4)
iCount = 1
For Each oCell In oTable.Range.Cells
oCell.Range.InsertAfter "s" & iCount & "cell"
iCount = iCount + 1
Next oCell
oTable.AutoFormat Format: = wdTableFormatColorful2, ApplyBorders: = True, App
lyFont: = True, ApplyColor: = True
2. In the form of columns, rows, insert the serial number
1. If you need to insert the first column of the table number, just in the For Each ... Next structure, the contents of the line can be changed to the following procedure, which InsertAfter method is used to add the serial number table cells (the " Line 1 "," line 2 ", etc.).
If iCount Mod 4 = 1 Then
oCell.Range.InsertAfter "s" & (iCount - 1) / 4 + 1 & "line"
End If
iCount = iCount + 1
2. If you need to start the second row from the table, insert the serial number, the code should read:
If iCount Mod 4 = 1 And iCount> 4 Then
oCell.Range.InsertAfter "s" & (iCount - 1) / 4 & "OK"
End If
iCount = iCount + 1
3. Insert date column in the table
(1) For the first column of the table insert date, For Each ... Next structure can be used to loop through each cell in the table, when a cell is to determine the first column, insert the date. Formart (Date ,...) is used to specify the date format, in the example below dates from the Date +1 (ie the current date the next day), the user can customize.
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
@ @ 0869601.JPG; Figure 2 @ @
(2) If you need to insert the second column of the table the day, in the case of the For Each ... Next structure, insert the following lines:
If iCount Mod 4 = 2 And iCount> 4 Then
oCell.Range.InsertAfter cWeekName (WeekDay (Date + (iCount - 1) / 4))
End If
Which, WeekDay (Date) returns a value (1 ~ 7), respectively, "Sunday" ~ "Saturday", CWeekName array requires prior is defined as:
Dim cWeekName (7)
cWeekName (1) = "Sunday"
cWeekName (2) = "Monday"
......
cWeekName (7) = "Saturday"
4. According to the contents of the cell to set a different format
Form the above case, for example, if you need all the "Saturday" and "Sunday" where the line format changed to a blue background, as long as the procedures followed in the above example you can append the following lines (table format to wdTableFormatColorful2, the number of rows changed 12 line). The program again using the For Each ... Next structure, traversing each row in the table (Rows), if it detects a line to meet the conditions ("Saturday" or "Sunday"), then select the line (Selection.SelectRow), to property to the format required (in this case a blue background).
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 With
End With
End If
iCount = iCount + 1
Selection.MoveDown Unit: = wdLine, Count: = 1
Next Rows
@ @ 0869602.JPG; Figure 3 @ @
Introduced more than a few cases of automatic processing using Word VBA examples of the form (for Visual Basic events, methods, objects, attributes, see the detailed use of Microsoft Word Visual Basic Help.) Customers can be compiled Visual Basic code as a Word macro to a toolbar or a shortcut for later use

没有评论:

发表评论