【问题标题】:Access VBA - duplicate Word table访问 VBA - 重复的 Word 表
【发布时间】: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 文档的代码部分会有所帮助。

标签: ms-access ms-word vba


【解决方案1】:

实际上,我认为无法复制/粘贴表格。 有人说我可能会更容易用 HTML 创建一个新表并将其添加到第一个表下。

Source = "<table style=""display:inline;border-collapse:collapse;font-size:1em;width:100%"" border=""1""><tbody>"

        First line of the table (ID and Title)
        Source = Source & "<tr><td style=""vertical-align:top"" class=""ms-rtetablecells""><div>(Story ID) - Title:</div></td>"
        Source = Source & "<td style=""vertical-align:top"" class=""ms-rtetablecells"" colspan=2><div>" & "testing cell" & "</div></td></tr>"
        Source = Source & "</tbody></table>"

所以现在我有了我的桌子(仍然需要填满它,但没关系)。

但我还是有问题。如何将其粘贴到模板表下方?

【讨论】:

    【解决方案2】:

    我找到的唯一解决方案是在 html 上创建一个新表,然后将该表写入一个 TextFile,最后将该文件复制到我的 word 模板中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-10
      • 2017-02-12
      • 1970-01-01
      • 2011-11-13
      • 2023-03-21
      • 1970-01-01
      • 2019-02-02
      • 2018-11-08
      相关资源
      最近更新 更多