【发布时间】:2016-08-28 15:36:08
【问题描述】:
我正在尝试在我的 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
【问题讨论】:
-
您需要问自己如何从外部计算出
ActiveDocument和Selection。您是否引用了 Word 对象模型? -
是的,有可能。但是您需要知道: 1) 您是否假设 Word 已经在运行? 2)您是否假设目标文档已经打开? 3)如果文档没有打开,如何找到要打开的文档的文件(路径)? 4)一旦文档打开,如何找到应该插入表格的位置?如果没有完整的信息,我们无法为您提供帮助...
-
1) Word 尚未运行。 2) 目标文档尚未打开。 3)文件路径将在我的 C 驱动器上,例如。 C:\database\template.docx。 4)我假设范围对象可用于设置表格应插入的位置。