none
Create Serial Print / Mail Merge Document with SDK V2 RRS feed

  • Question

  • Hi everyone,

    i'd like to create a serial print document, but my first tries results in a corrupted package :( More it leads word to terminate with:

    Unhandled exception at 0x551da9a8 in WINWORD.EXE: 0xC0000005: Access violation reading location 0x00000000.

    Unfortunatelly there is almost no help about the generation of serial print document WITH SDK V2.
    Can anybody help me with this issue or tell me what's wrong with my code.

    Thanks so much...
    Thomas

    var mailMergeRecipientDataPart =  
        _doc.MainDocumentPart.DocumentSettingsPart.AddNewPart<MailMergeRecipientDataPart>();  
    string mailMergeRecipientDataPartId = _doc.MainDocumentPart.DocumentSettingsPart.GetIdOfPart(mailMergeRecipientDataPart);  
     
    var recipients = new Recipients();  
    recipients.AppendChild(new RecipientData() {Active = new Active() {Val = BooleanValues.One}});  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
    recipients.AppendChild(new RecipientData() { Active = new Active() { Val = BooleanValues.One } });  
     
    using (var writer = new StreamWriter(mailMergeRecipientDataPart.GetStream()))  
    {  
        writer.Write(recipients.OuterXml);  
          
        //This was just another try...  
        //writer.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n<wne:recipients xmlns:ve=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\"><wne:recipientData><wne:active wne:val=\"1\"/><wne:hash wne:val=\"1133467634\"/></wne:recipientData><wne:recipientData><wne:active wne:val=\"1\"/><wne:hash wne:val=\"-599880766\"/></wne:recipientData></wne:recipients>");  
        /* Should be something like 
            <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            <wne:recipients xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
                <wne:recipientData>
                    <wne:active wne:val="1"/>
                    <wne:hash wne:val="1133467634"/>
                </wne:recipientData>
                <wne:recipientData>
                    <wne:active wne:val="1"/>
                    <wne:hash wne:val="-599880766"/>
                </wne:recipientData>
            </wne:recipients>
       */ 
    }  
     
    var externalRelationship =  
        _doc.MainDocumentPart.DocumentSettingsPart.AddExternalRelationship(  
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/mailMergeSource",  
            new System.Uri(filename, System.UriKind.Absolute));  
    string dataSourceId = externalRelationship.Id;  
     
     
    var mailMerge = _doc.MainDocumentPart.DocumentSettingsPart.Settings.GetFirstChild<MailMerge>();  
     
    if (mailMerge != null)  
    {  
        _doc.MainDocumentPart.DocumentSettingsPart.Settings.RemoveChild(mailMerge);  
    }  
     
    mailMerge = new MailMerge(  
        new MainDocumentType() { Val = MailMergeDocumentValues.FormLetter },  
        new LinkToQuery(),  
        new DataType() { Val = MailMergeDataValues.TextFile },  
        new ConnectString() { Val = "" },  
        new Query() { Val = string.Format("SELECT * FROM {0}", filename) },  
        new DataSourceReference() { Id = dataSourceId },  
        new DataSourceObject(  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
                    new FieldMapData(new LanguageId() { Val = "de-DE" }),  
            new RecipientDataReference() { Id = mailMergeRecipientDataPartId }));  
     
     
     
    _doc.MainDocumentPart.DocumentSettingsPart.Settings.AppendChild(mailMerge);  
     
     
    _doc.MainDocumentPart.DocumentSettingsPart.Settings.Save(_doc.MainDocumentPart.DocumentSettingsPart);  
     

     

    Tuesday, March 3, 2009 7:10 PM

All replies

  • have you found solution to your problem? I am stuck at the same. 
    Thursday, March 30, 2017 12:02 AM
  • Our company had the same challenge. We have found a much better solution as you are considering. Using Docentric Toolkit we have prepared MS Word template documents which we fill with data at runtime and send them directly to printer. Template generation is described here: http://www.docentric.com/documentation/templating.

    Runtime code is really simple:

    // Instancing report engine, by assigning the data source 
    DocumentGenerator dg = new DocumentGenerator(DataAccess.GetOrderById(7)); 
    // Generating report by specifying the report template and the resulting report (as file paths) 
    dg.GenerateDocument("example.docx", "example_output.docx");
    // Printing the output document;
    Document document;
    document = Document.Load("example_output.docx");
    document.Print();


    Wednesday, May 3, 2017 7:19 PM