MailMergeSettings and preventing the Text File Connection Parameters dialogue

Hi

We have a situation where opening a document with mailmerge fields will prompt the user with a dialogue like this:

image.png (68.3 KB)

The actual dialogue will have “Other” selected, and the textbox prefilled with “;” - which is the correct format of the .csv file containing the actual merge data.

Looking at doc.MailMergeSettings I have found references to
doc.MailMergeSettings.DataSource, which contains the path to the .csv, e.g. “d:\temp\data.csv”
and
doc.MailMergeSettings.Query, which contains a simple query: “SELECT * FROM d:\temp\data.csv”

Also, doc.MailMergeSettings.Odso has attributes
doc.MailMergeSettings.Odso.ColumnDelimiter , which contains “;”
doc.MailMergeSettings.Odso.DataSourceType, which contains Aspose.Words.Settings.OdsoDataSourceType.Text
and
doc.MailMergeSettings.Odso.FirstRowContainsColumnNames, which is true

Those values represents the options in the dialogue, but how can i indicate to Word that the parameters are correct and that I don’t want to see the dialogue? Clicking “OK” will open the document and the mail merge will work as intended, but I would like to disable the dialogue.

Any ideas?

Best regards,

Jan Hansen

@jan_hansen,

I think, you can first read CSV data file into a DataTable object and then simply pass it to MailMerge.Execute method. Please tell if the following code is acceptable for you?

Document doc = new Document("C:\\Temp\\firstname.docx");
DataTable dataTable = ConvertCSVtoDataTable("C:\\Temp\\data.txt", ",");
doc.MailMerge.Execute(dataTable);
doc.Save("C:\\temp\\21.5.docx");

public static DataTable ConvertCSVtoDataTable(string strFilePath, string delimiter)
{
    DataTable csvData = new DataTable();

    try
    {
        using (TextFieldParser csvReader = new TextFieldParser(strFilePath))
        {
            csvReader.SetDelimiters(new string[] { delimiter });
            csvReader.HasFieldsEnclosedInQuotes = true;
            string[] colFields = csvReader.ReadFields();

            foreach (string column in colFields)
            {
                DataColumn datecolumn = new DataColumn(column);
                datecolumn.AllowDBNull = true;
                csvData.Columns.Add(datecolumn);
            }

            while (!csvReader.EndOfData)
            {
                string[] fieldData = csvReader.ReadFields();
                //Making empty value as null
                for (int i = 0; i < fieldData.Length; i++)
                {
                    if (fieldData[i] == "")
                    {
                        fieldData[i] = null;
                    }
                }
                csvData.Rows.Add(fieldData);
            }
        }
    }
    catch (Exception ex)
    {
    }

    return csvData;
}

Thank you for the detailed answer!
However, the actual mailmerge operation is handled by Word - the application only prepares the .docx document and the corresponding data source file (.csv).

We’re unable to identify how we can force word to just ignore the dialogue and proceed with whatever settings that are part of the .docx file, as indicated by the MailMergeSettings object.

@jan_hansen,

The following C# code of 21.5 version of Aspose.Words for .NET does not cause MS Word 2019 on my end to prompt the undesired message box:

string dataSource = @"C:\\temp\\data.txt";

Document doc = new Document("C:\\temp\\firstname.docx");

doc.MailMergeSettings.Clear();
doc.MailMergeSettings.DataType = MailMergeDataType.TextFile;
doc.MailMergeSettings.MainDocumentType = MailMergeMainDocumentType.FormLetters;
doc.MailMergeSettings.ViewMergedData = true;


doc.MailMergeSettings.DataSource = dataSource;
doc.MailMergeSettings.Query = string.Format(@"SELECT * FROM {0}", dataSource);

doc.Save("C:\\temp\\21.5.docx");

What version of MS Word are you observing this issue with? Can you please also create a standalone simple Console application (source code without compilation errors) along with Word and data files that helps us to reproduce your current problem on our end and attach it here for testing? Please do not include Aspose.Words DLL files in it to reduce the file size. We will then start further investigation into your issue and provide you more information.