The following was gold code until I upgraded to the latest version. I was previously at 2.x which I bought in July of 2005. I have included the small sub where the error occurs and the sub that calls it directly below it.
I am simply copying a word document that is a template and then merging in data for each person being mailed and then merging an undetermined number of photos belonging to each person. The final document is the target document.
Private Sub AppendDoc(ByVal dstDoc As Aspose.Words.Document, ByVal srcDoc As Aspose.Words.Document)
While srcDoc.Sections.Count > 0
Dim section As Aspose.Words.Section = srcDoc.Sections(0)
'The section must first be removed before it can be inserted into a document.
srcDoc.Sections.RemoveAt(0)
dstDoc.Sections.Add(section) 'ERRORS ON THIS LINE
End While
End Sub
Private Sub MailSubjects(ByRef ds As wsOrgEvent.dsMailEvent)
Dim license As Aspose.Words.License = New Aspose.Words.License
license.SetLicense("Aspose.Words.lic")
Dim SourceDocS As New Aspose.Words.Document(m_MailTemplatePathS)
Dim TargetDocS As Aspose.Words.Document
Dim dsChanges As New wsOrgEvent.dsMailEvent
Dim rwS As wsOrgEvent.dsMailEvent.tblSubjectRow
Dim rwAd As wsOrgEvent.dsMailEvent.tblSubjectAddressRow
Dim rwE As wsOrgEvent.dsMailEvent.tblEventRow
Dim dvS As New DataView
Dim iPerson_ID As Int32
Try
dvS.Table = ds.tblSubject
dvS.Sort = "SeqNo"
'Used to determine number of forms to print per address.
Dim iFormImageCnt As Int16 = spnImagesS.Value
Dim iFormImageCntr As Int16 = 0
Dim iFormsPerDocCntr As Int16 = 0
Dim iSFotoCnt As Int16
Dim iSFotoIndex As Int16
Dim iPrintDocCntr As Int16 = 0
Dim iFormsCntr As Int32
Dim iFormsCnt As Int16
Dim iTotalFormsCntr As Int16
Dim rowsFotos() As wsOrgEvent.dsMailEvent.tblSubjectFotoRow
'Set DateTime string used in naming file
Dim sNow As String
sNow = Now.ToString
sNow = sNow.Replace(" ", "_")
sNow = sNow.Replace(":", "-")
sNow = sNow.Replace("/", "-")
'Initialize Print doc dhere
TargetDocS = SourceDocS.Clone
TargetDocS.Sections.Clear()
'Create labordernbr\subjects folder under template folder to save print documents to
If Not System.IO.Directory.Exists(m_MailTemplateDirectoryS + "\" + m_LabOrderNbr) Then
System.IO.Directory.CreateDirectory(m_MailTemplateDirectoryS + "\" + m_LabOrderNbr)
If Not System.IO.Directory.Exists(m_MailTemplateDirectoryS + "\" + m_LabOrderNbr + "\Subjects") Then
System.IO.Directory.CreateDirectory(m_MailTemplateDirectoryS + "\" + m_LabOrderNbr + "\Subjects")
End If
m_TargetDirectoryS = m_MailTemplateDirectoryS + "\" + m_LabOrderNbr + "\Subjects"
Else
If Not System.IO.Directory.Exists(m_MailTemplateDirectoryS + "\" + m_LabOrderNbr + "\Subjects") Then
System.IO.Directory.CreateDirectory(m_MailTemplateDirectoryS + "\" + m_LabOrderNbr + "\Subjects")
End If
m_TargetDirectoryS = m_MailTemplateDirectoryS + "\" + m_LabOrderNbr + "\Subjects"
End If
'execute merge here
Dim sPath As String
'there is no way to know in advance exactly the total forms that will be printed because not everyone has the same number of photos.
rwE = ds.tblEvent.Rows(0)
For Each rwS In dvS.Table.Rows
iSFotoIndex = 0
rowsFotos = ds.tblSubjectFoto.Select("Person_ID = " + rwS.Person_ID.ToString)
iSFotoCnt = rowsFotos.Length
'determine the number of forms to print
If iSFotoCnt <= spnImagesS.Value Then
iFormsCnt = 1
iFormImageCnt = iSFotoCnt 'if form image count > subject foto count, then we pretend each form has only the foto count of each subject
'this prevents iSFotoIndex from going out of range
Else
If iSFotoCnt Mod spnImagesS.Value = 0 Then
iFormsCnt = iSFotoCnt / spnImagesS.Value
Else
iFormsCnt = (iSFotoCnt / spnImagesS.Value) + 1
End If
End If
For Each rwAd In ds.tblSubjectAddress.Select("Person_ID = " + rwS.Person_ID.ToString)
For iFormsCntr = 1 To iFormsCnt
Dim OrderDoc As Document = SourceDocS.Clone
OrderDoc.MailMerge.Execute(rwS)
OrderDoc.MailMerge.Execute(rwAd)
OrderDoc.MailMerge.Execute(rwE)
Dim db As New DocumentBuilder(OrderDoc)
iFormImageCntr = 1
Do While iFormImageCntr <= iFormImageCnt
sPath = rowsFotos(iSFotoIndex).Path.Replace("Email", "Mail")
db.MoveToMergeField("Path")
db.InsertImage(sPath)
db.MoveToMergeField("RollNbr")
db.Write(rowsFotos(iSFotoIndex).RollNbr)
db.MoveToMergeField("FotoNbr")
db.Write(rowsFotos(iSFotoIndex).FotoNbr)
iSFotoIndex += 1 'this indexe is reset between subjects and between each address
iFormImageCntr += 1
Loop
iFormsPerDocCntr += 1
Select Case iFormsPerDocCntr < spnFormPerDocS.Value
Case True
Call AppendDoc(TargetDocS, OrderDoc)
iTotalFormsCntr += 1
Case False
Call AppendDoc(TargetDocS, OrderDoc)
iTotalFormsCntr += 1
TargetDocS.Save(m_TargetDirectoryS + "\DMS-" + m_LabOrderNbr + "-Subjects-" + iPrintDocCntr.ToString + "_" + sNow + ".doc")
If ckbPrintTest.Checked Then
MessageBox.Show("Print Test was checked. Please, review your saved document!", "Saved Test Document!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
iPrintDocCntr += 1
'set up new print document
TargetDocS = SourceDocS.Clone
TargetDocS.Sections.Clear()
iFormsPerDocCntr = 0
End Select
Next 'form
iSFotoIndex = 0 'reset for next address
Next 'address
If rwS.Mailed = False Then rwS.Mailed = True
Next 'subject
dsChanges = ds.GetChanges
If Not dsChanges Is Nothing Then
Call wsOrgEvent.UpdateMailedFlag(dsChanges)
ds.AcceptChanges()
End If
MessageBox.Show(iTotalFormsCntr.ToString + " forms generated and " + iPrintDocCntr.ToString + " documents saved!", "Mail Results!", MessageBoxButtons.OK)
Catch ex As Exception
MsgBox(iPerson_ID.ToString + ": " + ex.Message)
End Try
End Sub