Home > Software engineering >  VBS command
VBS command

Time:03-03

Private Sub docout_Click () 'export WORD button
If rs1. RecordCount & lt; 1 Then
MsgBox "export failure, currently there is no record in the list!"
Outstate1. Visible=False
The Exit Sub
End the If
On Error GoTo not_installword 'when don't word Error handling
If MsgBox (CRH (13) + "will present in the list of export data for WORD data? ", vbQuestion + vbYesNo)=vbNo Then Exit Sub

Dim wdApp As Word. Application 'Word variables
Dim wdDoc 'word document variables
Dim wdTable 'WORD form variables
Dim FieldLen deposit () 'field length value
Dim FieldLen1 As Integer 'store the maximum width of each column
Dim FieldValue As String
Dim iRow, iCol As Integer
Dim iRowCount, iColCount As Integer 'store rows and columns numerical
The main, Enabled=False
Outstate1. Visible=True 'export status display
Outstate1. Caption="are exported, please later... "
With rs1

. MoveLast
IRowCount=. RecordCount + 2 'record total
IColCount=. Fields. Total Count 'field
. MoveFirst
End With

'to redefine the number of columns
ReDim FieldLen (iColCount)
'add a word document and table
The Set wdApp=New Word. Application
WdApp. Documents. Add 'new Word document
The Set wdTable=wdApp. Selection. Tables. The Add (wdApp. Selection. Range, iRowCount + 1, iColCount, wdWord9TableBehavior, wdAutoFitFixed)
With rs1
"Read the headline width as the column width initial value
For iCol=1 To iColCount
FieldLen (iCol)=LenB (StrConv (the Fields (iCol - 1). The Name, vbFromUnicode))
Next iCol
For iRow=1 To iRowCount
For iCol=1 To iColCount
'read the field value, return to text type
If the Fields (iCol - 1). The Value & lt;> "" Then
If the Fields (iCol - 1). Type=10 Then
FieldValue=https://bbs.csdn.net/topics/Trim (. Fields (iCol - 1). The Value)
The Else
FieldValue=https://bbs.csdn.net/topics/CStr (. Fields (iCol - 1). The Value)
End the If
The Else
FieldValuehttps://bbs.csdn.net/topics/=""
End the If
The Select Case iRow
Case 1
'first header row, set behind
Case 2 'in the second line inserted into the field name
ICol, wdTable. Cell (iRow) Range. InsertAfter (the Fields (iCol - 1). The Name)
'set the field name center
WdTable. Cell (iRow, iCol) Range. ParagraphFormat. Alignment=wdAlignParagraphCenter
'set to bold font
WdTable. Cell (iRow, iCol) Range. The Font, Bold=wdToggle
Case Else 'starting from the third row insert record
'the calculation length of the field values, the return value is the unit of byte length
FieldLen1=LenB (StrConv (FieldValue vbFromUnicode))
'column widths automatically set form
If FieldLen (iCol) & lt; FieldLen1 Then
'table column width is equal to a long long field
WdTable. Columns (iCol). PreferredWidth=8 * FieldLen1 'Word table
'array Fieldlen (iCol) maximum field length in the
FieldLen (iCol)=FieldLen1
The Else
'table column width is equal to the current field width
WdTable. Columns (iCol). PreferredWidth=8 * FieldLen (iCol)
End the If
'to write field values in a table cell
ICol, wdTable. Cell (iRow) Range. InsertAfter (FieldValue)
Centered in the word 'set the cell
WdTable. Cell (iRow, iCol) Range. ParagraphFormat. Alignment=wdAlignParagraphCenter
End the Select

DoEvents
Next iCol
If iRow & gt; 2 Then
If Not, EOF Then MoveNext
End the If
DoEvents
Outstate1. The Caption="are exported, complete:" + CStr (Int (100 * iRow/iRowCount)) + "%" 'show export progress
Next iRow
Add (date) (month) (year)
'WdTable. Cell (iRowCount + 1, 1). The Range, InsertAfter (Format $(Now, "on dd mm yyyy years")) 'in the last line is add after (date) (month) (year)
WdTable. Rows (iRowCount + 1) Cells. The Merge 'merger last line
WdTable. Cell (iRowCount + 1, 1) Range. ParagraphFormat. Alignment=wdAlignParagraphRight

WdTable. Rows (1). Cells. The Merge 'merging first line form
If usetype="system administrator" Then
WdTable. Cell (1, 1). The Range, InsertAfter (" title ") 'merger after insert title
The Else
WdTable. Cell (1, 1). The Range, InsertAfter (usepart & amp; "Title") 'merger after insert title
End the If
WdTable. Cell (1, 1)=wdToggle 'Range. The Font. Bold set title to Bold
WdTable. Cell (1, 1). The Range. The Font, Font Size=14 'set the title to 14th
WdTable. Cell (1, 1)=wdAlignParagraphCenter 'Range. ParagraphFormat. Alignment Settings centered title
WdApp. Selection. Tables (1) Rows.=wdAlignRowCenter 'Alignment Settings form center


. MoveFirst
WdApp. Visible=True 'shows Word tables
Set wdApp return control to the Word=Nothing '
End With
Outstate1. Visible=False
The main, Enabled=True
The Exit Sub

Not_installword: 'when the computer didn't loaded word processing
MsgBox "export error! Please check whether the computer is equipped with not less than the Word version Word2000!" & CRH (13) & amp; CRH (10) & amp; "And then check the error record if there is a problem!"
Outstate1. Visible=False
The main, Enabled=True
End Sub
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Author: limshirley
Source: CSDN
Original: https://blog.csdn.net/limshirley/article/details/70210425
Copyright statement: this article original articles for bloggers, reproduced please attach link to blog!
  • Related