Method for making format contracts with VB Word template database
Overview: There are often customized format reports, such as print contracts, goods list, memo, etc., using third-party reporting software can implement but become cumbersome, actually utilize Word's automation programming, using VBA can complete similar Function, and very practical.
step one,
Word template production:
In the first line is the contract title "[Bookmark 1 Contract Title XXXXXXXX Contract]"
second line
*****************************
Contract number: [Bookmark 2 Contract No.]
Signing unit: [Bookmark 3 Signing Unit]
Signing address: [Bookmark 4 Signing Address]
Signing Date: [Bookmark 5 Signing Date]
.....
The first line of the form is inserted into the bookmark 4 in the first column of the first line of the table.
Table of the table
...
Quantity of goods name [Bookmark 6 List]
'Implement code as follows
DIM CN AS New Adodb.Connection
Dim Adors as new adod.recordset
DIM WordTemps as new word.application
Private sub flow_load ()
IF cn.state = 1 THEN
Cn.close
END IF
Cn.cursorLocation = aduseclient
Cn.open "provider = microsoft.jet.Oledb.4.0; data source =" & app.path & "/db1.mdb"
End Sub
'Start exporting data
Private submmand1_click ()
DIM STRSQL AS STRING
DIM REC AS INTEGER
DIM I as integer
Wordtemps.Documents.add app.path "/ cargo contract.doc", false
Wordtemps.Selection.goto WDGOTOBOOKMARK,, "Contract Title"
Wordtemps.Serection.Typetext "About the transaction contract for winter goods"
Wordtemps.Selection.goto WDGOTOBOOKMARK,,, Contract No.
Wordtemps.SeLection.Typetext "2004000001"
Wordtemps.Selection.goto WDGOTOBOOKMARK,, Signing Unit
Wordtemps.Serection.Typetext "Hongda Technology Company, Tiantian Technology Company"
WordTemps.Selection.goto WDGOTOBOOKMARK,,, "Signing Address"
Wordtemps.Serection.Typetext "Beijing Zhongguancun Building"
WordTemps.Selection.goto WDGOTOBOOKMARK,, Sign Time
Wordtemps.Serection.Typetext fromat (now, "YYYY-MM-DD")
strsql = "SELECT * FROM MATRIXS"
Adors.open Strsql, CN, AdopenKeyset, AdlockOptimistic
REC = Adors.Recordcount
IF REC <1 THEN
Msgbox "No Product Record!", Vbokonly, "Tips"
Adors.Close
EXIT SUB
Else
Adors.Movefirst
Wordtemps.Selection.goto WDGOTOBOOKMARK,, "List" for i = 1 to REC
WordTemps.SeLection.Typetext adors! Name
WordTemps.Selection.Moveright Unit: = wdcharacter, count: = 1 'right shift one
Wordtemps.SeLection.Typetext Adors! Quantity
WordTemps.Selection.Moveright Unit: = wdcharacter, count: = 1 'right shift one
Wordtemps.Serection.Typetext Adors! Specifications
Adors.MoveNext
If adors.eof = false kil
Wordtemps.Selection.inSertrowSbelow 1 'table wrap
END IF
Next I
Adors.Close
WordTemps.visible = true 'Display Word Window
END IF
End Sub
Author: soho_andy (ice)