Author: Yang Zhongxun Major: Computer software development and application Language ability: TOFEL633 GRE2140
Email: [email protected]
In the actual development of the Mis system, we sometimes need to download the report data on the current page to the local computer in the format of a Word document. This implementation is not difficult. But sometimes we need to make some settings for the format of the downloaded Word document, such as title color, font size, word spacing, etc. In this case, we have to use the macro function that comes with Word.
For example, we want to display the title of this report in the following format in a Word document: 14-point font, bold, and centered. First we need to record the corresponding macro command in Word. Open Word, create a new document, manually type a line of text, then select Tools->Macro->Record New Macro command, give the new macro a name such as Macro1, and perform the above actions (size 14, bold, center-aligned), Word automatically saves these actions as corresponding Vbscript commands. Then select Tools->Macro->Macro Command, select the macro Macro1 we just defined, and you can view its content. In this example, the macro command we saved is as follows:
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter 'Center alignment
Selection.Font.Bold = wdToggle 'Bold display
Selection.Font.Size = 14 '
Because the script language of the macro command is Vbscript, we can use the above statement in VB without making any changes. In this way, we can write the following VB code to achieve the functions we require. The code is as follows:
WdApp.Selection.Font.Bold = wdToggle 'Bold display
WdApp.Selection.Font.Size = 14 '14 size font
WdApp.Selection.TypeText ("Report Title") 'Report Title
WdApp.Selection.ParagraphFormat.lignment = wdAlignParagraphCenter 'Align in the center
WdApp.Selection.Font.Bold = wdToggle 'Cancel bolding.
Similarly, if we want to perform other processing on the Word document, just repeat the above steps. The following provides a complete example of my processing of Word documents:
Private Function SaveAsWord(ByRef MyRecord As Recordset, ByVal DocFileName As String, ByRef OutMessage As String) As Integer
'************************************************ ************************
'
'Description: Save the data in the dataset as a DOC file
'
'parameter:
'
'MyRecord data set
'DocFileName WORD file name (no path, see the instance variable sPath for the path)
'Return information of OutMessage operation
'
'Return: 1 success -1 failure
'
'************************************************ ************************
'Initialize the Word application
err.Clear
On Error GoTo Err_All
Dim WdApp As Word.Application
Set WdApp = CreateObject("Word.Application")
'Insert data
Dim colloop As Integer 'Column number
Dim rowloop As Integer 'row number
Dim colMax As Integer 'Number of columns
Dim rowMax As Integer 'Number of rows
Dim wdcell As Integer 'width
Dim UnitEnd As Integer 'Intercept end point
Dim UnitName As String 'Unit name
Dim BbDate As String 'Report period
wdcell=12
colMax = MyRecord.Fields.count
rowMax = MyRecord.RecordCount
WdApp.Documents.Add
'Get the report unit
UnitEnd = InStr(sBBDetail, "period")
UnitName = Mid(sBBDetail, 1, UnitEnd - 2)
BbDate = Mid(sBBDetail, UnitEnd, Len(sBBDetail))
If MyRecord.Fields.count >= 10 Then
WdApp.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
Else
WdApp.ActiveDocument.PageSetup.Orientation = wdOrientPortrait
End If
'Report name
WdApp.Selection.Font.Bold = wdToggle
WdApp.Selection.Font.Size = 14
WdApp.Selection.TypeText (sbbmc)
WdApp.Selection.ParagraphFormat.lignment = wdAlignParagraphCenter
WdApp.Selection.Font.Bold = wdToggle
WdApp.Selection.TypeParagraph
'Reporting unit name
WdApp.Selection.Font.color = wdColorBlack
WdApp.Selection.Font.Size = 11
WdApp.Selection.TypeText(UnitName)
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
WdApp.Selection.TypeParagraph
'Report period
WdApp.Selection.TypeText(BbDate)
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
WdApp.Selection.TypeParagraph
WdApp.Selection.TypeParagraph
'Generate column headers
'wdApp.Selection.HomeKey wdLine, wdExtend
'dApp.Selection.Font.Bold = wdToggle
WdApp.ActiveDocument.Tables.Add WdApp.Selection.Range, rowMax, colMax
Dim i As Integer
Do
For colloop = 0 To colMax - 1
WdApp.Selection.Font.Size = 9
If i = 0 Then
'The title in the table is displayed in bold
WdApp.Selection.Font.Bold = wdToggle
'Set the background color of the table title row to gray, with a grayscale of 30
With WdApp.Selection.Cells
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray30
End With
End With
End If
'The last line is right-aligned and the rest are left-aligned
If i > 0 Then
If MyRecord.Fields.Item(colloop).Name = "ZBMC" Or MyRecord.Fields.Item(colloop).Name = "Indicator Name" Then
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
Else
WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
End If
End If
If i = 0 And (MyRecord.Fields.Item(colloop).Name = "SXH" Or MyRecord.Fields.Item(colloop).Name = "Sequence Number") Then
WdApp.Selection.TypeText ("serial number")
Else
WdApp.Selection.TypeText (CStr(MyRecord.Fields.Item(colloop).value))
End If
If (i <> rowMax - 1 Or (i = rowMax - 1 And colloop < colMax - 1)) Then
WdApp.Selection.MoveRight (wdcell)
End If
Next
i = i + 1
MyRecord.MoveNext
Loop Until MyRecord.EOF
WdApp.ActiveDocument.SaveAs DocFileName, 0, False, "", True, "", False, False, False, False, False
WdApp.Quit
SaveAsWord = 1
Exit Function
Err_All:
Set WdApp = Nothing
SaveAsWord = -1
OutMessage = err.Description
Exit Function
End Function
Okay, so far, I think you have some understanding of using Word macro commands to develop ASP components in VB. As long as you use it more, you will become familiar with it quickly.