hi,
here is the code we use to loop through a grid and create the letters and append them
its only letting me attach one template file
Private Sub btnPrintNow_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnPrintNow.Click
Dim grdItem As DataGridItem
Dim objLetter As New clsLetter
Dim myCheckBox As CheckBox
Dim intResult As Integer
Dim intIdentity As Integer
Dim blnShowDoc As Boolean
Dim dstDoc As New Document
Dim srcDoc As Document
Dim strProfession As String
Dim strLetterType As String
Dim strDocName As String
Dim decRecID As Decimal
Dim decPrintID As Decimal
Dim strPath As String
Dim param As String = Me.Request.Params(0)
Dim strProfessionFromDataGrid As String
''license file for Aspose
'Dim license As License = New License
'license.SetLicense("C:\Scheduler V1.0.0.1\SchedulerWeb\bin\Aspose.Word.lic")
Try
If CheckDate() = False Then Exit Sub 'kp 3/11/2006 SCHE1130
If CheckTimeOut_Std() = False Then Exit Try
If grdGrid.Items.Count = 0 Then
ASPNET_MsgBox(Me, "No records to print!")
Exit Sub
End If
Dim myItem As DataGridItem
Dim myBool As Boolean = False
For Each myItem In grdGrid.Items
If CType(myItem.Cells(14).Controls(1), CheckBox).Checked = True Then
myBool = True
Exit For
End If
Next
If myBool = False Then
ASPNET_MsgBox(Me, "No records selected to print")
Exit Sub
End If
decPrintID = objLetter.GetPrintID(Me.VALUE1, Me.VALUE2, CType(Application("strVD"), String))
dstDoc.Sections.Clear()
For Each grdItem In grdGrid.Items
If grdItem.ItemType = ListItemType.Item Or grdItem.ItemType = ListItemType.AlternatingItem Then
myCheckBox = CType(grdItem.Cells(14).Controls(1), CheckBox)
If myCheckBox.Checked = True Then
'print now
blnShowDoc = True
' Dim str As String = CType(grdItem.Cells(14).Controls(0), Label).Text
'create the document
'get required fields for creating the letter
Me.LETTERTYPE = CType(grdItem.Cells(3).Controls(1), Label).Text
Select Case Me.LETTERTYPE
Case enumLetterType.enumClient
strLetterType = "CLIENT"
Case enumLetterType.enumEpisode
strLetterType = "EPISODE"
Case enumLetterType.enumAppointment
strLetterType = "APPOINTMENT"
Case enumLetterType.enumBulkAppointment
strLetterType = "BULK"
Me.LETTERTYPE = enumLetterType.enumBulkAppReprint
Case enumLetterType.enumBulkAppointmentNextVisit
strLetterType = "BULK"
Me.LETTERTYPE = enumLetterType.enumBulkNVReprint
Case enumLetterType.enumBulkEpisode
strLetterType = "BULK"
Me.LETTERTYPE = enumLetterType.enumBulkEpiReprint
End Select
strProfession = CStr(CType(grdItem.Cells(17).Controls(1), Label).Text)
strDocName = CType(grdItem.Cells(8).Controls(1), Label).Text
decRecID = CType(grdItem.Cells(1).Controls(1), Label).Text
strPath = strProfession + "/" + strLetterType
srcDoc = fun_Create_Letter(strDocName, strPath, decRecID)
AppendDoc(dstDoc, srcDoc)
'set the properties
With objLetter
.LetterID = CType(grdItem.Cells(0).Controls(1), Label).Text
.TemplateID = CType(grdItem.Cells(2).Controls(1), Label).Text
.ID = CType(grdItem.Cells(1).Controls(1), Label).Text
.GeneratedDate = CStr(Date.MinValue) 'SET TO NULL IN WEBSERVICE
.PrintedDate = CStr(Date.Today)
.GeneratedBy = 0 'SET TO NULL IN WEBSERVICE
.PrintedBy = Me.UNUMBER
.Timestamp = CType(grdItem.Cells(13).Controls(1), Label).Text
.PrintID = decPrintID
.Batch = 0
.Profession = CStr(CType(grdItem.Cells(17).Controls(1), Label).Text)
End With
intResult = objLetter.UpdateLetter(Me.VALUE1, Me.VALUE2, CType(Application("strVD"), String))
'set existing doc to nothing
srcDoc = Nothing
End If
End If
Next
If blnShowDoc = True Then
' strProfessionFromDataGrid = CStr(CType(grdItem.Cells(17).Controls(1), Label).Text)
'protect so doc cannot be edited
dstDoc.Protect(ProtectionType.AllowOnlyFormFields)
'display the resultant doc
dstDoc.Save( _
String.Format("{0}.doc", param), _
SaveFormat.FormatDocument, _
SaveType.OpenInWord, Response)
BindTheGrid(Me.GridSearchMode) 'Usual Search
Else
ASPNET_MsgBox(Me, "All letter records created successfully")
End If
' BindTheGrid(Me.GridSearchMode)
Catch ex As Exception
ASPNET_MsgBox(Me, "Unable to print selected letters : " & ex.Message)
Finally
objLetter.Dispose()
End Try
End Sub
'*****************************************************************************
''**********************ASPOSE FUNCTIONS/SUBS*******************
'*****************************************************************************
Private Function App_Path() As String
Try
Return System.AppDomain.CurrentDomain.BaseDirectory()
Catch ex As Exception
Throw ex
End Try
End Function
Private Function OpenDoc(ByVal fileName As String) As Document
Try
Dim myDoc As Document
Return New Document(String.Format("{0}\{1}", curPath, fileName))
Catch ex As Exception
Throw ex
End Try
End Function
Private Sub AppendDoc(ByVal dstDoc As Document, ByVal srcDoc As Document)
Try
While srcDoc.Sections.Count > 0
Dim section As 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)
End While
Catch ex As Exception
Throw ex
End Try
End Sub
Private Function fun_Create_Letter(ByVal strDocName As String, ByVal strPath As String, ByVal decID As Decimal) As Document
'Open the template document
Me.curPath = App_Path() + "Templates/" + strPath ' Directory for doc's
Dim doc As Document = OpenDoc(strDocName)
Dim intResult As Integer
Dim dsResults As New DataSet
Try
If objLetter Is Nothing Then
objLetter = New clsLetter
End If
objLetter.ID = decID
intResult = objLetter.SelectLetterData(Me.VALUE1, Me.VALUE2, _
CType(Application("strVD"), String), Me.LETTERTYPE, dsResults)
If intResult > 0 Then
'Perform the mail merge - with a DATA-TABLE!!!!
doc.MailMerge.Execute(dsResults.Tables(0))
'This is useful if you want to delete any MERGEFIELD records that might
'be left at the end of the table.
doc.MailMerge.DeleteFields()
Return doc
End If
Catch ex As Exception
Throw ex
End Try
End Function