I'm REALLY new to this programming stuff, so please bear with me.
I've created the following code to perform a mailmerge to word from my excel spreadsheet, but when I run the code I am having a couple problems, and I have no idea where to start looking to correct them. I set up the Mailmerge Main Document as a Word Template with the Mergefields already filled in. The data is coming from the first sheet in my excel workbook which has the headers for each column in Row A, and the headers match the word Mergefields I entered in my word template. When I run the following code, even though there are currently only 60 or so rows of "records" the result in word is 34 pages of my 5160 Avery Labels. After it runs out of the actual records it creates 32 pages of blank labels which appear to be printing only the current date and appear to be pulling the data from blank cells in my "ActivityDate" column of my datasource.
Also, everytime I run the macro, I get a message saying that "a table in the document has become corrupted"
Can someone please tell me where in my code I've gone wrong or what I'm missing?
I'd also like to figure out how to only select certain records from my datasource to be merged, but I suspect that might have something to do with a query or some similar function that might be better addressed in another sub-forum (however any tips on where i might look or post would also be greatly appreciated)
Sub MaiMerge() Dim oApp As Word.Application Dim oMainDoc As Word.Document Dim wb As Excel.Workbook Dim sDBPath As String Dim sTPath As String Set wb = ActiveWorkbook sDBPath = wb.Path & "\CalendarStickers.xls" sTPath = wb.Path & "\CalendarStickers.dot" 'Create a new Word Session Set oApp = CreateObject("Word.Application") 'Start a new main document for the mail merge. Set oMainDoc = oApp.Documents.Add(sTPath) With oMainDoc.MailMerge .MainDocumentType = wdMailingLabels 'Set up the mail merge data source to CalendarStickers.xls. sDBPath = wb.Path & "\CalendarStickers.xls" .OpenDataSource Name:=sDBPath, _ SQLStatement:="SELECT * FROM `Dates$`" End With 'Perform the mail merge to a new document. With oMainDoc .MailMerge.Destination = wdSendToNewDocument .MailMerge.SuppressBlankLines = True With .MailMerge.DataSource .FirstRecord = wdDefaultFirstRecord .LastRecord = wdDefaultLastRecord End With .MailMerge.Execute Pause:=False End With 'Activate word and display document With oApp .Visible = True .ActiveWindow.WindowState = 0 .Activate End With End Sub