So, I have created a script/workbook for generating multiple invoices along with the meta data required by a piece of software we have built.
Basically the metadata contains receipt/PO and supplier data, and pushed out to five different CSV files. So this all works fine.
The next stage is taking information from the workbook and mail merging into a number of invoice layouts. I have 13 Layouts (so far) as 13 separate docx files, each time the VBA script is run, it create a number of each of these layouts using the generated data, splits them into separate documents and converts them to PDF. Now here is where my problem is, and i assume its something to do with how i've initialised the word document/application. If i select more than ~4 layouts (the user can choose how many layouts they want) i get the "microsoft excel is waiting for another application to complete an ole action" Error. My working theory is that i am no closing word down after each merge is carried out.
Initialised>>Generate metadata>>export CSV Master Data files>>Merge invoices into single word document>>split Document>>Convert to PDF
Below is the mail merge portion of my code, and this may simply be my inexperience with VBA and doing something wrong, but any help would be great.
Sub RunMailMerge()
Application.DisplayAlerts = False
Dim Path As String
Dim layoutNo As Integer
Dim wdDoc As Object
layoutNo = Worksheets("Config").Range("B8").Value
For i = 1 To layoutNo
Path = ActiveWorkbook.Path & "\Layout " & i & " " & Format(Date, "d mmm yyyy")
CreateFolder (Path)
Dim wdOutputName, wdInputName As String
wdOutputName = ActiveWorkbook.Path & "\Layout " & i & " " & Format(Date, "d mmm yyyy") & "\Invoice " & Format(Date, "d mmm yyyy")
wdInputName = ActiveWorkbook.Path & "\Layouts\merge" & i & ".docx"
On Error Resume Next
Set wdDoc = GetObject(wdInputName, "Word.document")
wdDoc.Application.Visible = False
With wdDoc.MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
.Execute Pause:=False
End With
wdDoc.Application.Visible = False
AllSectionsToSubDoc ActiveDocument, Path
wdApp.Close SaveChanges:=wdDoNotSaveChanges
Set wdDoc = Nothing
Next i
Application.DisplayAlerts = True
End Sub
Bookmarks