这个问题非常适合自动化。在我看来,您似乎应该能够拥有一个基本模板,并且几乎完全基于带有机器信息的 Excel 电子表格来填写信息。
Word 中的书签是您的朋友。您可以使用它们最初放置表格和图形,并在新信息可用时更新它们(尽管这需要额外的努力)。我已经做了大量工作,将数据从 Excel 导入 Word 作为表格,并且绝对建议不要将表格作为图片导入。您的文件大小会迅速膨胀,想要从表格中以电子方式提取数据的人会想用生锈的茶匙刺伤您。
根据您提供的信息,我可能会在 Excel 中使用 Excel 模板作为活动工作簿开始您的代码。这是我设置它的方式:
- 从显示功能区选项卡的加载项开始。这可用于任何“机器”excel模板。
- 使用 OLE 自动化打开 Word 并从 Word 模板创建一个新文档(网上有很多信息可以做到这一点)
- 从 Excel 模板中读取文档的结构并设置 word 文档。由于您知道布局,因此您可以为所有图形和表格以及标题填充占位符(书签)。
- 循环遍历所有表并首先插入它们。将表格数据从 Excel 插入 Word 的技巧是将表格作为分隔文本插入,然后将其转换为表格。
- 循环并插入所有数字。
- 如果您使用书签封装图形并向表格添加书签,则可以根据需要单独更新它们。
请注意,这些选项本身都不是那么微不足道。如果你想应用额外的格式,如粗体标题、合并单元格或拆分页面的表格,那么它的工作量要大得多。
您可以使用域代码顺序更新表格和图形编号,并再次使用书签来提供交叉引用。
提供大量代码的问题相当广泛,但以下示例子程序和函数应该足以让您入门。如果您还有其他问题,您应该为他们提出一个新问题。
输入 Word 文档(从模板创建并已填充定义表格位置的书签)和您拥有的所有表格的名称,以下函数会将这些表格填充到 Word 中。
Sub PopulateTables(wdDoc As Word.Document, vTableArray As Variant)
Dim ii As Integer, rInputData As Range
'Loop through all the bookmarks
For ii = LBound(vTableArray) To UBound(vTableArray)
'Get the name of the current table from the list in the Excel template
sTableName = vTableArray(ii)
'Check if the bookmark exists in the document
If wdDoc.Bookmarks.Exists("tblplc_" & sTableName) Then
'Use the function to check if there is a table already at the bookmark
Call CheckTableBookMark(wdDoc, "tblplc_" & sTableName)
'Get the range of the information to be put into the table here.
'THIS WILL BE YOUR OWN CUSTOM FUNCTION
Set rInputData = GetMyInputData(sTableName)
'Insert the data into Word
Call CreateTableFromString(wdDoc.Bookmarks("tblplc_" & sTableName).Range, rInputData)
End If
Next ii
End Sub
此函数将删除书签处的任何现有表,并确保有新数据的新书签:
Sub CheckTableBookMark(wdDoc As Word.Document, sTargetBM As String)
'Function to delete any existing tables at a bookmark.
With wdDoc
.Activate
.Bookmarks(sTargetBM).Select
'If the bookmark has a table in it then we need to delete it
If .Bookmarks(sTargetBM).Range.Tables.Count > 0 Then
.Bookmarks(sTargetBM).Range.Tables(1).Delete
'If the bookmark was 'inside' the table it may have been deleted. Put it back in
If Not .Bookmarks.Exists(sTargetBM) Then
.Application.Selection.TypeParagraph
.Application.Selection.MoveLeft Unit:=wdCharacter, Count:=1
.Bookmarks.Add sTargetBM
Else
.Bookmarks(sTargetBM).Range.Select
.Application.Selection.TypeParagraph
End If
'Do custom formatting here as required.
.Bookmarks(sTargetBM).Range.Style = "Normal"
.Bookmarks(sTargetBM).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
End With
End Sub
以下两个函数将构建一个包含数据的字符串,然后为您将其转换为表格:
Sub CreateTableFromString(ByRef rWordRange As Word.Range, rFromRange As Range)
Dim tblWordTarget As Word.Table
'Build the data from the Excel Spreadsheet and set it to the word range
rWordRange.Text = BuildDataString(rFromRange)
Set tblWordTarget = rWordRange.ConvertToTable(vbTab, AutoFitBehavior:=wdAutoFitFixed, DefaultTableBehavior:=wdWord8TableBehavior)
'Do stuff with the table here (eg apply formatting etc)
Set tblWordTarget = Nothing
End Sub
Function BuildDataString(rFromRange As Range) As String
Dim sData As String, nrRow As Long, nrCol As Integer, iTotalColumns As Integer
'Convert the input range to a variable and determine the number of columns
vData = rFromRange.Value
iTotalColumns = UBound(vData, 2)
'Loop through all the elements in the array
For nrRow = LBound(vData, 1) To UBound(vData, 1)
For nrCol = 1 To iTotalColumns
'Depending on what type of data is encountered either add it to the string or substitute something
'You'll want to modify this as needed
If IsError(vData(nrRow, nrCol)) Then
sData = sData & "Error"
ElseIf vData(nrRow, nrCol) = "" Or vData(nrRow, nrCol) = 0 Or vData(nrRow, nrCol) = "-" Then
sData = sData & VBA.Chr$(150)
Else
sData = sData & vData(nrRow, nrCol - iIncrement)
End If
'Use tab delimiters for Word to know where the columns are
If nrCol < iTotalColumns Then sData = sData & vbTab
Next nrCol
'Add a carriage return for each new line
If nrRow < UBound(vData, 1) Then sData = sData & vbCr
Next nrRow
'Return the completed string
BuildDataString = sData
End Function