Ms access 使用VBA从Access在Word文档中创建表

Ms access 使用VBA从Access在Word文档中创建表,ms-access,ms-word,vba,Ms Access,Ms Word,Vba,我正在尝试从Access数据库在Word文档模板中创建表 这段代码可以从Word本身正常运行,并根据需要创建表。我想知道是否可以从Access运行此代码,并指向要在其中创建表的特定word文档 Dim numberOfTables As Integer Dim iCount As Integer numberOfTables = InputBox("How many tables to make?", "Tables") For iCount = 0 To numberOfTables -

我正在尝试从Access数据库在Word文档模板中创建表

这段代码可以从Word本身正常运行,并根据需要创建表。我想知道是否可以从Access运行此代码,并指向要在其中创建表的特定word文档

Dim numberOfTables As Integer
Dim iCount As Integer

numberOfTables = InputBox("How many tables to make?", "Tables")

For iCount = 0 To numberOfTables - 1

    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
        3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With Selection.Tables(1)
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        '.ApplyStyleRowBands = True 'Office 2010
        '.ApplyStyleColumnBands = False 'Office 2007
    End With

    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph

Next iCount
将numberOfTables设置为整数
Dim I以整数形式计数
numberOfTables=InputBox(“要制作多少个表?”,“表”)
对于iCount=0到numberOfTables-1
ActiveDocument.Tables.Add范围:=Selection.Range,NumRows:=2,NumColumns:=_
3,DefaultTableBehavior:=wdWord9TableBehavior,AutoFitBehavior:=_
自动安装
有选择。表(1)
如果.样式为“表格网格”,则
.Style=“表格网格”
如果结束
.ApplyStyleHeadingRows=真
.ApplyStyleLastRow=False
.ApplyStyleFirstColumn=True
.ApplyStyleLastColumn=False
“.ApplyStyleRowBands=True”Office 2010
“.ApplyStyleColumnBands=False”Office 2007
以
Selection.EndKey单位:=wdStory
选择.类型段落
下一个iCount

您需要做的是首先从Access打开Word的新实例。这是通过以下命令完成的:

Set wrdApp = CreateObject("Word.Application")
然后,要使其可见并添加文档,请从此点开始使用此对象:

wrdApp.Visible = True
Set myDoc = wrdApp.Documents.Add   'Here you should also keep the new document as an object so you can directly refer to it
或者,如果使用模板,则需要将其打开:

wrdApp.Visible = True
Set myDoc = wrdApp.Documents.Open ("C:\database\template.docx")
然后是您需要根据上述内容相应修改的代码:

For iCount = 0 To numberOfTables - 1

    myDoc.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
        3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With myDoc.ActiveWindow.Selection.Tables(1)  
'Note here that for the Selection object you need to refer to the active window
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        '.ApplyStyleRowBands = True 'Office 2010
        '.ApplyStyleColumnBands = False 'Office 2007
    End With

    myDoc.ActiveWindow.Selection.EndKey Unit:=wdStory
    myDoc.ActiveWindow.Selection.TypeParagraph

Next iCount
iCount=0到numberOfTables-1的

myDoc.Tables.Add Range:=Selection.Range,NumRows:=2,NumColumns:=_
3,DefaultTableBehavior:=wdWord9TableBehavior,AutoFitBehavior:=_
自动安装
使用myDoc.ActiveWindow.Selection.Tables(1)
'请注意,对于选择对象,您需要参考活动窗口
如果.样式为“表格网格”,则
.Style=“表格网格”
如果结束
.ApplyStyleHeadingRows=真
.ApplyStyleLastRow=False
.ApplyStyleFirstColumn=True
.ApplyStyleLastColumn=False
“.ApplyStyleRowBands=True”Office 2010
“.ApplyStyleColumnBands=False”Office 2007
以
myDoc.ActiveWindow.Selection.EndKey单位:=wdStory
myDoc.ActiveWindow.Selection.typeparation
下一个iCount

这应该让您开始。

您需要问问自己,如何从外部计算出
ActiveDocument
Selection
。你引用的是单词对象模型吗?是的,有可能。但你需要知道:1)你认为Word已经运行了吗?2) 您是否认为目标文档已打开?3) 如果文档未打开,如何找到要打开文档的文件(路径)?4) 打开文档后,如何找到表应该插入的位置?没有完整的信息,我们无法帮助您……Word尚未运行。2) 目标文档尚未打开。3) 文件路径将位于我的C驱动器上,例如C:\database\template.docx。4) 我假设range对象可以用来设置表应该插入的位置。很抱歉回复得太晚。这似乎正是我想要的,但是文档已经存储在一个文件路径为C:\database\template.docxIn的文件夹中。在这种情况下,您需要使用
Set myDoc=wrdApp.Documents.Open
然后您想要
myDoc.SaveAs
以避免覆盖模板。第二行出现错误`myDoc.Tables.Add Range…`需要运行时错误424对象。我不确定我遗漏了什么您需要在打开后添加您的文档,例如:
Set myDoc=wrdApp.Documents.Open(“C:\database\template.docx”)
@mekzek我用打开模板的代码更新了答案。