Mail Merge with Page Breaks

Last post 09-10-2010, 11:24 AM by AndreyN. 23 replies.
Page 2 of 2 (24 items)   < Previous 1 2
Sort Posts: Previous Next
  •  09-09-2010, 10:13 AM 257846 in reply to 257839

    Re: Mail Merge with Page Breaks

    If I comments out the following it runs, and when the document opens it shows the TOC, but then it quickly dissapears along with the merge data in the header of the document.

    ' Insert TC field to build TOC. Insert TC only if this is the first time when City occur.
    'If mCity <> currentCity Then
    ' Get current cell and move builder to the beginning of it.
    ' Dim cell As Cell = DirectCast(builder.CurrentParagraph.GetAncestor(NodeType.Cell), Cell)
    ' builder.MoveTo(cell.FirstParagraph.FirstChild)
    ' builder.InsertField(String.Format("TC ""{0}"" \l 1", args.FieldValue))
    'End If
     
  •  09-09-2010, 10:29 AM 257848 in reply to 257846

    Re: Mail Merge with Page Breaks

    This seems wierd. The TOC and page numbers show if I print, but if I just look at the document, they dont show...

    Do you know if its possible to Sort a TOC by the Company field ascending? I know this really makes it an index. They just need a quick reference as to where a company name is in the document.
     
  •  09-09-2010, 5:45 PM 257906 in reply to 257848

    Re: Mail Merge with Page Breaks

    Hi Ryan,

    Thanks for this additonal information.

    That runtime error is occuring because there are Cell classes in both Aspose.Words and Aspose.Cells but the wrong types are being matched in your code. To fix this you should give the fully qualified names of the classes as to avoid any confusion. i.e instead of just Cell have instead Aspose.Words.Tables.Cell.

    Regarding your question on sorting, there is no easy way to sort a TOC as it is the order of the entries are suppost to reflect the positition relative to the order they appear in the document. We can provide you with some code to do this using an index if you need this functionality.

    If things still aren't working with your program above could you paste your full code here and I will take a closer look.

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  09-09-2010, 5:57 PM 257908 in reply to 257906

    Re: Mail Merge with Page Breaks

    Attachment: Present (inaccessible)
    I modified the template and code to use an index. What I'm confused about is he index is not automatically built. After the word document is created and opened, it says index has no entries found (or something like that), but if you right click on the index and select update, it appears. Can Aspose.Words automatically do this? I attached my template.

    Dim doc As New Document("d:\hart11\templates\canvassing.doc")
    Dim adapterTemplate1 As New SQLDataAdapter("SELECT '' AS SalesAmount, ViewContact.CompanyTxt AS Company, '' AS EmploymentThisSite, case when len(ltrim(rtrim(ViewContact.FaxDirectTxt)))='10' then ''+SUBSTRING(ViewContact.FaxDirectTxt,1,3)+'.'+''+SUBSTRING(ViewContact.FaxDirectTxt,4,3)+'.'+SUBSTRING(ViewContact.FaxDirectTxt,7,4) else ViewContact.FaxDirectTxt end AS Fax, '' AS Ownership, case when len(ltrim(rtrim(ViewContact.PhoneDirectTxt)))='10' then ''+SUBSTRING(ViewContact.PhoneDirectTxt,1,3)+'.'+''+SUBSTRING(ViewContact.PhoneDirectTxt,4,3)+'.'+SUBSTRING(ViewContact.PhoneDirectTxt,7,4) else ViewContact.PhoneDirectTxt end AS Phone, '' AS [Plant/FacilitySize], case when len(ltrim(rtrim(ViewContact.PhoneGeneralTxt)))='10' then ''+SUBSTRING(ViewContact.PhoneGeneralTxt,1,3)+'.'+''+SUBSTRING(ViewContact.PhoneGeneralTxt,4,3)+'.'+SUBSTRING(ViewContact.PhoneGeneralTxt,7,4) else ViewContact.PhoneGeneralTxt end AS Phone2, ViewContact.WebURL AS WebAddress, '' AS YearEstablished, '' AS OwnsRents, ViewContact.Address1Txt AS Address1, ViewContact.Address2Txt AS Address2, ViewContact.CityTxt As City, ViewContact.StateCode AS State, ViewContact.PostalCodeTxt As Zip, ViewContact.ShippingAddress1Txt AS AltAddress, ViewContact.ShippingCityTxt As AltCity, ViewContact.ShippingStateCode AS AltState, ViewContact.ShippingPostalCodeTxt AS AltZip, ViewContact.CountyTxt AS County, ViewContact.ContactSIC1Code AS SIC1, '' AS SIC1Description, ViewContact.ContactSIC2Code AS SIC2, '' AS Sic2Description, ViewContact.ContactSIC3Code AS SIC3, '' AS Sic3Description, ViewContact.ContactParentCompanyTxt AS Parent, ViewContact.PrefixTxt AS Prefix, ViewContact.FirstTxt AS First, ViewContact.LastTxt As Last, ViewContact.SuffixTxt AS Suffix, ViewContact.TitleTxt AS Title, ViewContact.DivisionTxt AS Division, Contact.CanvassingTemplate1 FROM ViewContact INNER JOIN Contact ON Contact.ContactID = ViewContact.ParentContactID INNER JOIN ExportDetail ON ExportDetail.ExportPrimaryRecordID = ViewContact.ContactID INNER JOIN ExportMaster ON ExportMaster.ExportID = ExportDetail.ExportID WHERE ExportDetail.ExportID='" & Request.QueryString("ID") & "' UNION SELECT [Sales Amount] AS SalesAmount, Company, EmploymentThisSite, case when len(ltrim(rtrim(REPLACE(Fax,'-',''))))='10' then ''+SUBSTRING(REPLACE(Fax,'-',''),1,3)+'.'+''+SUBSTRING(REPLACE(Fax,'-',''),4,3)+'.'+SUBSTRING(REPLACE(Fax,'-',''),7,4) else Fax end AS Fax, Ownership, case when len(ltrim(rtrim(REPLACE(Phone,'-',''))))='10' then ''+SUBSTRING(REPLACE(Phone,'-',''),1,3)+'.'+''+SUBSTRING(REPLACE(Phone,'-',''),4,3)+'.'+SUBSTRING(REPLACE(Phone,'-',''),7,4) else Phone end AS Phone, [Plant/FacilitySize], case when len(ltrim(rtrim(REPLACE(TollFree,'-',''))))='10' then ''+SUBSTRING(REPLACE(TollFree,'-',''),1,3)+'.'+''+SUBSTRING(REPLACE(TollFree,'-',''),4,3)+'.'+SUBSTRING(REPLACE(TollFree,'-',''),7,4) else TollFree end AS Phone2, WebAddress, YearEstablished, OwnsRents, MailAddress AS Address1, '' AS Address2, MailCity AS City, MailState AS State, MailZipPlusExtension AS Zip, StreetAddress AS AltAddress, StreetCity AS AltCity, StreetState AS AltState, StreetZip AS AltZip, StreetCounty AS County, PrimarySic AS SIC1, PrimarySicDescription AS SIC1Description, Sic2 AS SIC2, Sic2Description, Sic3 AS SIC3, Sic3Description, ParentName AS Parent, RankedExecutive1Prefix AS Prefix, RankedExecutive1FirstName AS First, RankedExecutive1LastName AS Last, RankedExecutive1Suffix AS Suffix, RankedExecutive1TitleDescription AS Title, '' AS Division, '' AS CanvassingTemplate1 FROM ContactHarris INNER JOIN ExportMaster ON ExportMaster.ExportID = ContactHarris.ExportID WHERE ContactHarris.ExportID='" & Request.QueryString("ID") & "' AND ContactHarris.Duplicate='' ORDER BY City ASC", myConnection)
    Dim dtTemplate1 As New DataTable()
    adapterTemplate1.Fill(dtTemplate1)
    dtTemplate1.TableName = "Data"
    doc.MailMerge.FieldMergingCallback = New HandleMergeFieldCanvassing()
    doc.MailMerge.ExecuteWithRegions(dtTemplate1)
    doc.MailMerge.Execute(New String() {"Canvassed", "Salesperson"}, New Object() {rcbExportNameTxt.Text, rcbPreparedFor.Text})
    doc.UpdatePageLayout())
    doc.Save(SavePath)


    Private Class HandleMergeFieldCanvassing
    Implements IFieldMergingCallback
    Private Sub FieldMerging(args As FieldMergingArgs) Implements IFieldMergingCallback.FieldMerging
    Dim builder As New DocumentBuilder(args.Document)
    If args.FieldName = "City" Then
    Dim currentCity As String = args.FieldValue.ToString()

    builder.MoveToField(args.Field, False)

    mRecordIndex += 1
    If (Not String.IsNullOrEmpty(mCity) AndAlso mCity <> currentCity) OrElse mRecordIndex > 2 Then
    mRecordIndex = 0
    builder.CurrentParagraph.ParagraphFormat.PageBreakBefore = True
    End If

    mCity = currentCity
    End If
    If args.FieldName = "Company" Then
    builder.MoveToField(args.Field, False)
    builder.InsertField(String.Format("XE ""{0}"" \b", args.FieldValue))
    End If
    End Sub

    Private Sub ImageFieldMerging(e As ImageFieldMergingArgs) Implements IFieldMergingCallback.ImageFieldMerging
    ' Do nothing.
    End Sub

    Private mCity As String
    Private mRecordIndex As Integer
    End Class
     
  •  09-09-2010, 9:01 PM 257926 in reply to 257908

    Re: Mail Merge with Page Breaks

    Hi Ryan,

    Thanks for posting your code here.

    Yes, you can automatically populate index and TOC fields by calling doc.UpdateFields() first, then doc.UpdatePagelayout().

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  09-09-2010, 9:20 PM 257939 in reply to 257926

    Re: Mail Merge with Page Breaks

    I added the logic below. Still says the following when the document is opened: No index entries found. 


      doc.UpdateFields()
      doc.UpdatePageLayout()

    Does anything else look out of place to you?
     
  •  09-10-2010, 5:07 AM 258011 in reply to 257939

    Re: Mail Merge with Page Breaks

    Hi

     

    Thanks for your inquiry. TOC is properly generated on my side. Could you please specify which version of Aspose.Words you use in your application? I used the latest version for testing.

     

    Best regards,


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  09-10-2010, 7:21 AM 258032 in reply to 258011

    Re: Mail Merge with Page Breaks

    Is there any difference between a TOC and index. I do not have a TOC on my page, only an index... No TC entries, using XE.
     
  •  09-10-2010, 11:24 AM 258229 in reply to 258032

    Re: Mail Merge with Page Breaks

    Hello

     

    Thanks for your interest in Aspose.Words. Please just open your document using MS Word and press Alt-F9 to hide/show the field code. You will see TOC field code (Table of Content).

     

    Best regards,


    Andrey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
Page 2 of 2 (24 items)   < Previous 1 2
View as RSS news feed in XML