使用VBA自动创buildWord模板

我正在创build一个word文档模板,并处于一个十字路口。 我想用从MATLAB输出中填充的MATLAB和Excel表格创build的数字填充文档。 数字被组织到文件夹中,Excel表格被组织在Excel模板的工作表中,如下所示:

在这里输入图像描述

我在这里问了几个关于自动更新这些表格和数字的问题,现在有了这样的代码:

MS Word中的链接表

在MS Word中链接的图像和表格

报告很长,但篇幅有所不同。 这些报告logging了机器testing。 有的客户testing1台机器,有的testing5台机器。 对于5台机器,报告有100个表和400个数字。

例如,2台机器的报告结构如下:

文字1

图1.1

图1.2

文字2

表1.1

表1.2

图2.1

图2.2

我想以编程方式创build报告。 用户可以将Word模板,Excel模板和文件结构复制并粘贴到其工作目录中。 Excel模板中将有一个工作表,提供有关testing的信息。 即要testing的机器数量。 该模板将被构build为一台机器。

VBA将从Excel模板中抽取要testing的机器数量。 然后,它将在Word文件中索引数字和表格,将它们复制到Word文件中正确位置的指定数量的机器,并将它们链接到正确的源文件位置。 如果一个迭代的testing运行,这是我将使用上面张贴的代码来更新数字和表格。

什么是最简单的方法来设置? 什么方法会使生成和刷新表数据最快? 从我已经完成的阅读,这听起来像设置表导入为图片可能会更快,而不是链接数据, 如此应用程序。 我想代码要快速,安全,健壮,而不是依赖于任何这样的插件。 可能我可能需要这样的东西,但似乎有点矫枉过正。

任何帮助将不胜感激 – 我正在努力掌握Word VBA,字段代码和书签之间的关系,并最好地使用它们对我有利。

这个问题是自动化的理想select。 对我来说,似乎你应该可以有一个基本的模板,并填写信息几乎纯粹基于Excel电子表格与机器信息。

Word中的书签在这里是你的朋友。 您可以使用它们来初始放置您的表格和graphics,并在新信息可用时更新它们(尽pipe这需要额外的努力)。 我已经做了一些工作,将数据从Excel导入到Word中作为表格,并且肯定会推荐将表格导入为图片。 你的文件大小会迅速膨胀出来,而那些想从电子表格中提取数据的人会想用生锈的茶匙刺你。

从你提供的信息我可能会开始你的代码在Excel中的Excel模板作为活动工作簿。 这是我设置它的方式:

  • 从显示function区选项卡的加载项开始。 这可以用于任何“机器”excel模板。
  • 使用OLE自动化来打开Word并从Word模板创build一个新文档(网上有很多信息来做到这一点)
  • 从Excel模板中读取文档的结构并设置文档。 既然你知道布局,你可以填充所有graphics和表格的占位符(书签)以及标题。
  • 循环遍历所有的表格,并首先插入它们。 将Excel表格数据从Excel插入Word的技巧是将表格作为分隔文本插入,然后将其转换为表格。
  • 循环并插入所有的数字。
  • 如果使用书签封装数字并向表中添加书签,则可以根据需要单独更新它们。

请注意,这些选项本身并不重要。 如果你想申请额外的格式,如粗体标题,合并的单元格或表格分割页面,那么它是相当多的工作。

您可以使用字段代码来顺序更新表格和graphics编号,再次使用书签来提供交叉引用。

提供很多代码的问题相当广泛,但下面的示例subs和函数应该足以让您开始。 如果你还有其他问题,你应该为他们提出一个新的问题。

随着Word文档(从模板创build,已经填充书签定义表位置 )和所有表的名称的input,下列函数将填充到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 

此function将删除书签上的任何现有表格,并确保新数据有新的书签:

 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 

以下两个函数将构build一个包含数据的string,然后将其转换为一个表格:

 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 

我会亲自使用Matlab代码来创build一个LaTeX文件,其中包含所有包含图像和数据的文件名。

在开发过程中,不要忘记定期检查生产的胶乳是否被htlatex或oolatex接受。

乳胶有很长的学习曲线,但如果你有一个月,你将成功。

链接关于oolatex包括图像的文件名: https ://groups.google.com/forum/#!topic/comp.text.tex/p–jBb7MIuQ