Curabitur magna orci, varius in urna ac, tempor mattis ipsum. Nam mauris eros, dignissim at maximus eget, ornare ut enim. Nulla suscipit urna nec purus pellentesque maximus. Phasellus scelerisque tortor risus, nec scelerisque lacus faucibus vel. Praesent ut lectus feugiat, placerat ipsum sollicitudin, mattis enim. Selection.TypeText Text:="Praesent ac diam sit amet ex fermentum aliquet. Duis varius tellus id lorem convallis scelerisque." & Chr(11) & Chr(11) Donec iaculis ipsum lorem, a ullamcorper augue condimentum sit amet. Pellentesque et turpis sit amet mauris aliquam rhoncus. Nullam sagittis eros ac risus lacinia scelerisque sed sed nulla. Nullam ac porta lorem, ut fermentum elit. Suspendisse auctor dapibus augue, nec viverra risus pretium nec. Selection.TypeText Text:="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Selection.TypeText Text:="Dear Vendor: " & Chr(11) & Chr(11) Selection.MoveDown Unit:=wdLine, Count:=10 Some standard Lorem Ipsum text filler is used for the text blocks in this example. Here the page has a salutation with 2 line breaks and have 2 blocks of text added for the letter body.
Recordset fields start at column number 6 - 1 (the array begins with 0), so the code needs to subtract 4 from the Field Count of 5 through 7 to populate table rows 1 through 3.ĪctiveDocument.Tables(t).Cell(k - 4, 1).Range.InsertBefore rs.Fields(k) Write a For loop to iterate through table rows 1 thru 3 (fields 5 thru 7) to populate the address.
Selection.TypeText Text:="August 8, 2017" & Chr(11)Ī Range:=Selection.Range, NumRows:=6, NumColumns:=1, _ĭefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _ĪctiveDocument.Tables(t).Columns.PreferredWidth = InchesToPoints(8)ĪctiveDocument.Tables(t).Rows.Height = InchesToPoints(0.2)ĪctiveDocument.Tables(t).Borders(wdBorderLeft).Visible = FalseĪctiveDocument.Tables(t).Borders(wdBorderRight).Visible = FalseĪctiveDocument.Tables(t).Borders(wdBorderTop).Visible = FalseĪctiveDocument.Tables(t).Borders(wdBorderBottom).Visible = FalseĪctiveDocument.Tables(t).Borders(wdBorderHorizontal).Visible = FalseĪctiveDocument.Tables(t).Borders(wdBorderVertical).Visible = False Declare the variables and ADODB connections and set the Data Source.ĭim labelrows, labelcolumns, i As Integerĭim j As Integer, k As Integer, t As Integerĭim rs As ADODB.Recordset, rsCount As ADODB.Recordsetĭim sDataSource As String, sDataTable As String 0 And Len(labelrows) > 0 ThenĪ = InchesToPoints(0.5)Ī = InchesToPoints(0.5)Ī = InchesToPoints(0.75)Ī = InchesToPoints(0.75) This way the group could be called in SQL in Word to retrieve 1 ID at a time.Ĭreate the VBA code to Merge the Address and Row data into MS WordĬreate a new Sub in Word VBA that takes an Int as a Variable. An ID field was added to each Group \ Vendor in Column A using a Sequential Integer. The Excel file used for this Word data source originated from a query from a SQL Server database - AdventureWorks 2014. Later, I realized that I could use a VBA Data merge with SQL and then code 2 tables: one with a Single column of data and one iteration through a loop for the Address, a second one of variable rows based on the Count of the recordset to create the second, formatted table. A standard Mail Merge in Word was used by taking the variable data and putting it into a Single row with the addressee and fields for Data Rows 1 through 10 for each field - very messy and not pretty. MS Word Letter with Variable Data Rows in Body Merge: BackgroundĪ couple of years ago I needed to mail merge a list of Addressees into a letter and somehow include their account data - which was variable in nature (1 row of data or 10 rows of data per addressee).