Home > Software engineering >  About the vba to realize the excle data import word templates output a batch file to print.
About the vba to realize the excle data import word templates output a batch file to print.

Time:02-09

Everyone a great god, and I need to excel data import here word templates and a single output file collection (template) batch printing, consulted some programs on the net, but always not achieve them, please teach a great god,



The Sub process card ()

'
'process card batch macro
'
'


'open the Data. XLSX file
Dim MyFile As Object
The Set MyFile=CreateObject (" Scripting. FileSystemObject ")

Dim As FilePath String
FilePath=ActiveDocument. Path & amp; "\ Data. XLS"

'if there is no Data. XLSX file
If Not MyFile. FileExists (FilePath)=True Then
'exit
MsgBox "could not find file: Data. XLSX," the Title:="Error"
The Exit Sub
End the If

'read Data. XLSX file
Dim ExcelObject As Object
The Set ExcelObject=GetObject (FilePath)
Set the Table=ExcelObject. Sheets (1). UsedRange ()


Dim WordApp As Object 'definition of Word Application
The Set WordApp=CreateObject (" Word. Application ")
WordApp. Visible=True
Dim WordDoc As Object 'definition document
Dim WordDoc1 As Object 'definition document


The Set WordDoc=WordApp. Documents. The Add
WordDoc. SaveAs2 (ActiveDocument. Path & amp; "\ Data. Doc")
WordDoc. Close
The Set WordDoc1=WordApp. Documents. The Open (ActiveDocument. Path & amp; "\ process card template. Docx")



'loop
For I=4 To a Table. Rows. Count
'empty variable
For Each Var In ActiveDocument. Variables
Var. Delete
Next

'add a Name to the Name of the variable, it is the value of the Data. The line in XLSX first (I) the content of the column 1
ActiveDocument. Variables. The Add Name:="XMMC", Value:=Table. The Cells (2, 2). The Text

ActiveDocument. Variables. The Add Name:="GZL", Value:=Table. The Cells (2, 7). The Text

ActiveDocument. Variables. The Add Name:="gy," Value:=Table. The Cells (2, 10). The Text

ActiveDocument. Variables. The Add Name:="jh," Value:=Table. The Cells (2, 13). The Text

ActiveDocument. Variables. The Add Name:="JGMC", Value:=Table. The Cells (I, 2). The Text

ActiveDocument. Variables. The Add Name:="th", Value:=Table. The Cells (I, 5). The Text

ActiveDocument. Variables. The Add Name:="second life", the Value:=Table. The Cells (I, 9). The Text


'update Word document
ActiveDocument. Fields. The Update




Next
End Sub

CodePudding user response:

There is something wrong with the order.

Set WordDoc=WordApp. Documents. The Add (template) 'here you can create a template format WORD
Populate the data here, and then save and print
And then close the document
Then cycle to Set up new WORD document Set WordDoc=WordApp. Documents. The Add position (template)
Then continue to repeat

CodePudding user response:

Specific creation method view my post a EXCEL basis, creating and using form part is the general
  •  Tags:  
  • VBA
  • Related