Unable to merge a Winword document with an includeText in the header section


#1

Hi,

We have a problem with a WinWord file. In the header section we have an INCLUDETEXT refering to au bookmark in another Doc file.
We tried many options but we can’t import the bookmark content with the data merged.

Thanks for your response or solutions.

Daniel Dupont (Seiitra)


#2

For instance,
A MainDocument.doc contains this in the header section:
{INCLUDETEXT “d:\AsposeRepro\{QUOTE entete{MERGEFIELD A001}{MERGEFIELD A004 #00}.doc}” E}
this should insert the bookmark “E” existing in the document d:\AsposeRepro\enteteE11.doc.

A datafile contaning these datas:
A001|A002|A003|A004
E|Cabinet|PARIS|11

Another file d:\AsposeRepro\enteteE11.doc containing references to MERGEFIELDs {MEGERFIELD A002}


#3

@admin.grenoble

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.


#4

Hi Tahir,

Here are the files you expected as an example:
maquette.doc is the main document to merge
data.don is the data file
enteteE11.doc is the document to insert inside maquette.doc
resultat.docx is the merged document using ASPOSE
sample.doc is the document merged manually.
program.cs is the source code in c#.
AsposeRepro.zip (324.0 KB)


#5

@admin.grenoble

In your code, the IncludeText method does not insert the sub document into header and footer. Please insert the sub document at the place of IncludeText field using DocumentBuilder.InsertDocument method after performing the mail merge for sub document.


#6

In the main document Word, inside the header and footer there is the INCLUDETEXT script.

In the method IncludeText c#, I tried to uncomment the " docBuilder.InsertDocument(subDoc, ImportFormatMode.KeepDifferentStyles);"

But I still have the problem.
Can you give me an example of script that works with the document I gave you please.

Thanks


#7

is it possible to insert only a part of a document identified by a bookmark?
I’m able to import the whole subDoc containing the merged values but I cannot import the content of a bookmark (ie: enteteE11.doc contains 2 bookmark E and P)


#8

I found this topic on your Free support.

It seems to work but I don’t have the content of paragraphs in the header, but only empty lines.

Here is the changes I made on my program.cs.

(at line 224, I tried to save each bookmark content in a separate file and theses files have no text merged whereas the temporary file save in line 214 has got all the text merged.Program.zip (3.1 KB)


#9

In DebugMode, in the while loop I can see each Node of type Paragraph containing the text I want to insert.
But at the end, I only have empty pagraphs.


#10

@admin.grenoble

Following code example shows how to perform mail merge operation for sub document and insert it into main document.

LoadOptions options = new LoadOptions();
options.LoadFormat = LoadFormat.Doc;
options.PreserveIncludePictureField = true;
Document subDoc = new Document(MyDir + "enteteE11.doc", options);
if (subDoc.Range.Fields.Where(f => f.Type == FieldType.FieldMergeField).Count() > 0)
{
    subDoc.MailMerge.CleanupOptions = Aspose.Words.MailMerging.MailMergeCleanupOptions.RemoveEmptyParagraphs;
    subDoc.MailMerge.UnconditionalMergeFieldsAndRegions = true;
    subDoc.MailMergeSettings.ViewMergedData = true;
    subDoc.UpdateFields();
    subDoc.MailMerge.Execute(new string[] { "A001", "A002", "A003", "A004" }, new object[] { "E", "Cabinet J. LESIEUR & Cie", "PARIS", "11" });
}

Document doc = new Document(MyDir + "maquette.doc");
DocumentBuilder builder = new DocumentBuilder(doc);


foreach (Field field in doc.Range.Fields)
{
    switch (field.Type)
    {
        case FieldType.FieldIncludeText:
            builder.MoveToField(field, true);
            builder.InsertDocument(subDoc, ImportFormatMode.KeepSourceFormatting);
            field.Remove();
            break;
        default:
            break;
    }
}

doc.Save(MyDir + "19.6.docx");

You can use following code example to achieve this requirement.

LoadOptions options = new LoadOptions();
options.LoadFormat = LoadFormat.Doc;
options.PreserveIncludePictureField = true;
Document subDoc = new Document(MyDir + "enteteE11.doc", options);
if (subDoc.Range.Fields.Where(f => f.Type == FieldType.FieldMergeField).Count() > 0)
{
    subDoc.MailMerge.CleanupOptions = Aspose.Words.MailMerging.MailMergeCleanupOptions.RemoveEmptyParagraphs;
    subDoc.MailMerge.UnconditionalMergeFieldsAndRegions = true;
    subDoc.MailMergeSettings.ViewMergedData = true;
    subDoc.UpdateFields();
    subDoc.MailMerge.Execute(new string[] { "A001", "A002", "A003", "A004" }, new object[] { "E", "Cabinet J. LESIEUR & Cie", "PARIS", "11" });
}

subDoc.Save(@"D:\AsposeRepro\include.docx");

Document doc = new Document(MyDir + "maquette.doc");

foreach (Field field in doc.Range.Fields)
{
    switch (field.Type)
    {
        case FieldType.FieldIncludeText:
            FieldIncludeText fieldInclude = (FieldIncludeText)field;
            fieldInclude.SourceFullName = @"D:\AsposeRepro\include.docx";
            fieldInclude.BookmarkName = "E";
            fieldInclude.Update();
            break;
        default:
            break;
    }
}

doc.Save(MyDir + "19.6.docx");

#11

Thank you very much, this works great.
FYI, HEre is the final code.

if (subDoc.Range.Fields.Where(f => f.Type == FieldType.FieldMergeField).Count() > 0)
{
subDoc.MailMerge.CleanupOptions = Aspose.Words.MailMerging.MailMergeCleanupOptions.RemoveEmptyParagraphs;
subDoc.MailMerge.UnconditionalMergeFieldsAndRegions = true;
subDoc.MailMergeSettings.ViewMergedData = true;
subDoc.UpdateFields();
subDoc.MailMerge.Execute(table);
subDoc.UpdateFields();
FileInfo fi = new FileInfo(fIt.SourceFullName);
string subPathTmp = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fi.Name);
subDoc.Save(subPathTmp);
fIt.SourceFullName = subPathTmp;
fIt.Update();
fIt.Unlink();
File.Delete(subPathTmp);
}
else
{
fIt.Update();
fIt.Unlink();
}


#12

@admin.grenoble

It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.