I have a Word document that starts with one table on page one, and then depending on the data in an Excel file, I want to start inserting tables in subsequent pages, and just before each table, I want to insert a text blurb or caption. I’m using a bookmark to insert this text. After inserting the table, I want to add a page break, so that I can start the next table insertion on the next page.
The result I get from the code below places the tables and bookmarks in incorrect order, on seemingly random pages, and in seemingly random order, and the tables are not dimensioned properly (not 6X5 as they should be, but somehow they’re 3×3). How can I control the tables, bookmarks, and page breaks to order the document properly?
'FROM EXCEL Dim wd as New Word.Document Dim doc as Word.Document Dim Rng as Range Dim d as Variant dim datMin, datMax as Date datMin = "04/01/2020" datMax = "04/05/2020" Set doc = wd.Documents.Open("myFile") Set Rng = doc.Range(0, 0) For d = datMin To datMax Set Rng = Rng.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=(d - datMin 2)) 'Go to top of page doc.Bookmarks.Add "D" & d - datMin 2, Rng 'BOOKMARK NAMES ARE "D2", "D3", etc. Rng.Bookmarks("D" & d - datMin 2).Range.Text = d 'Place Text in bookmark doc.Tables.Add Rng, 6, 5 'Add table Rng.InsertBreak wdPageBreak 'Add pagebreak Next d