使用VBA将Access子窗体导出到Word

时间:2016-04-19 08:52:00

标签: vba ms-access ms-word access-vba

我有一个主要表格,其中包含订单号,客户名称,地址,联系方式等详细信息。此主窗体包含一个子窗体,该窗体包含链接主窗体的特定订单号的项目。

我需要将主表单数据和子表单数据导出到word。我使用以下代码将主要表单中的数据使用书签发送到word。我无法并且不确定如何使用子表单数据执行此操作。

由于

'This array contains the placeholders in the MS Word template
docVars = Array("<company>", "<att>", "<date>", "<ref>", "<project>", "<Insert scope here>")

'This array contains the MS Access variables
accessVars = Array(CustomerName, CustomerAttn, OrderDate, OrderNo, Project, Notes)

'error checking --- do arrays have the same number of elements?
If (UBound(docVars) = UBound(accessVars)) Then
    If (IsNull(accessVars(1)) Or IsNull(accessVars(4))) Then
        MsgBox ("Project or Att field cannot be empty!!!")
    Else
        Set objWord = CreateObject("Word.Application")
        Set quoteDoc = objWord.Documents.Add(Template:="C:\folder\OrderTemplate.dotx", NewTemplate:=False, DocumentType:=0)

        For i = 0 To (UBound(docVars))
            With quoteDoc.Application.selection.Find
                .Text = docVars(i)
                .Replacement.Text = Left(accessVars(i), 254)
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With

        quoteDoc.Application.selection.Find.Execute Replace:=2
        Next i
        quoteDoc.Application.Visible = True
    End If

0 个答案:

没有答案