Powerbuilder / Mailmerge / .txt Datasource

Greeting all.

Evaluating the product.

“Hello World” - works great, already try it.

Having a problem with syntax for mailmerge.

Need to:

Open existing MailMerge word document and merge into data from existing tab separated with headers text document.

Any help will be greatly appretiated.

Thx

Hi there,

Thanks for your inquiry. Please refer to the following articles:

About Mail Merge

How to Execute Mail Merge

Could you please share your input document along with data source file here for testing? We will investigate the issue on our side and provide you more information.

Hi Tahir.

Thank you for getting back to me.

No, there is no problems or issues.

As i said earlier, i am just evaluating a product.

Attaching both files.

Here is the simpified sample of PB script using ‘Word.Application’

oleobject mm_object
mm_object = CREATE OLEObject
mm_object.ConnectToNewObject(‘Word.Application’) 
mm_object.Documents.Open(‘c:\account.docx’, TRUE, AsStatement!)
mm_object.ActiveDocument.MailMerge.OpenDataSource(c:‘as_merge.txt’, 0, FALSE, TRUE, TRUE, FALSE) 
mm_object.ActiveDocument.MailMerge.Execute
mm_object.WordBasic.EditReplace(’\t’, ‘^t’, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, AsStatement!)
mm_object.WordBasic.EditReplace(’\n’, ‘^l’, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, AsStatement!)
mm_object.WordBasic.EditReplace(’\q’, ‘"’, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, AsStatement!)
mm_object.ActiveDocument.Saved = TRUE
mm_object.ActiveWindow.Close
mm_object.DisConnectObject()
DESTROY mm_object

Thank you

Hi there,

Thanks for your feedback. Please check the documentation links shared in my previous post. You can also achieve your requirements using MailMergeSettings. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + @"Account.docx");
string dataSource = @"C:\Temp\as_merge.txt";
doc.MailMergeSettings.DataType = MailMergeDataType.TextFile;
doc.MailMergeSettings.MainDocumentType = MailMergeMainDocumentType.FormLetters;
doc.MailMergeSettings.DataSource = dataSource;
doc.MailMergeSettings.Query = string.Format(@"SELECT * FROM {0}", dataSource);
Regex whiteSpaceRegex = new Regex(@"\s+");
using (StreamReader reader = new StreamReader(dataSource, Encoding.UTF8))
{
    string firstLine = reader.ReadLine();
    string[] fieldNames = firstLine.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
    for (int i = 0; i < fieldNames.Length; i++)
    {
        OdsoFieldMapData mapData = new OdsoFieldMapData();
        mapData = new OdsoFieldMapData();
        mapData.Column = i;
        mapData.MappedName = fieldNames[i];
        mapData.Name = whiteSpaceRegex.Replace(fieldNames[i], "_");
        mapData.Type = OdsoFieldMappingType.Column;
        doc.MailMergeSettings.Odso.FieldMapDatas.Add(mapData);
    }
}
doc.Save(MyDir + @"17.2.0.docx");

Thank you Tahir.

I will give it a shot.

Sincerely

Vlad