This problem is ideal for automation. It seems to me that you should have a basic template and fill out the information largely solely on the basis of an Excel spreadsheet with information about the computer.
Bookmarks in Word are you friend. You can use them to initially post your tables and figures, as well as update them if new information becomes available (although this requires additional effort). I did a little work on importing data from Excel into Word in the form of tables and definitely recommend not importing the tables as images. The size of your file will quickly go outside, and people who want to electronically extract data from tables will want to inflict shock teaspoons on you.
From the information you provided, I probably start my code in Excel using an Excel template as an active workbook. So I installed it:
- Start with the add-in that displays the ribbon tab. This can be used for any excel template for a machine.
- Use OLE automation to open Word and create a new document from a Word template (there is a lot of information on the web for this)
- Read in the document structure from the Excel template and customize the word document. Since you know the layout, you can fill out placeholders (bookmarks) for all figures and tables along with captions.
- Scroll through all the tables and insert them first. The trick to inserting tabular data from Excel into Word is to insert a table as delimited text and then convert it to a table.
- Scroll and paste all the numbers.
- If you encapsulate bookmarked digits and add a bookmark to the tables, you can update them as needed.
Note that none of these options is trivial in itself. If you want to apply additional formatting, for example, bold headings, merged cells or tables that are paginated, then this is much more work.
You can use field codes to sequentially update table and number numbers and bookmarks again to provide cross-references.
The question is quite wide to provide a lot of code, but for an example you should use the examples and functions below. If you have additional questions, you should ask them a new question.
When you enter a Word document (created from a template and already filled with bookmarks that determine the location of the tables) and the names of all the tables you have, the following functions will populate these tables in 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
This function will delete any existing table in the bookmark and provide a new bookmark for new data:
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
The following two functions will build a row containing the data, and then convert it to a table for you:
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