【发布时间】:2015-02-25 14:36:41
【问题描述】:
我正在开发一个访问应用程序。 在应用程序结束时,我需要用 Access 表中的数据填充 word 模板。
我的 word 模板有一个表格,表格中的一行将被填充。
问题是我需要每行一个表。所以我需要复制我的word模板的唯一表格。
如何复制表格并将其粘贴到下方?
这是我当前可以填充第一个表的代码
Dim appWord As Word.Application
Dim appWord2 As Word.Application
Dim doc As Word.Document
Dim template As Word.Document
Dim db As DAO.Database
Dim rs As DAO.Recordset
On Error Resume Next
Err.Clear
Set appWord = CreateObject("Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set appWord2 = CreateObject("Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord2 = New Word.Application
End If
Set db = CurrentDb()
'The final selected Building Blocks are in the table ConsultingOffer
Set rs = db.OpenRecordset("ConsultingOffer")
'Open the Word template
Set template = appWord.Documents.Open("C:\WordForms\Template.docx", , True)
'Open a new Word document which is a copy of the template
Set doc = appWord2.Documents.Add(template.FullName)
Dim iTable As Integer
iTable = 2
'Loop on all records to populate the word
Do While Not rs.EOF
With doc
.SelectContentControlsByTitle("Title").Item(1).Range.Text = rs.Fields("ModuleName")
.Tables(iTable).Cell(1, 2).Range.Text = rs.Fields("Comments")
.Tables(iTable).Cell(2, 3).Range.Text = rs.Fields("DeliverablesDescription")
.Tables(iTable).Cell(5, 2).Range.Text = rs.Fields("EffortRequired") & " man-days"
rs.MoveNext
End With
Loop
'Save our final document as "Proposal" + date
doc.SaveAs2 ("C:\WordForms\Proposal_" & Format(Now(), "ddmmmyyyy_h\hmm_ss") & ".docx")
'Open Word Application with our final document
appWord2.Visible = True
'Quit the word template
appWord.Quit True
Set template = Nothing
Set doc = Nothing
Set appWord = Nothing
Set appWord2 = Nothing
【问题讨论】:
-
我找到的所有复制粘贴都像“Word to another word”或“Word to excel”。
-
如果您显示访问 Word 文档的代码部分会有所帮助。