本文介绍了与邮件合并如何获取MailMerge.DataSource.DataFields的每个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要进行邮件合并的项目,我正在使用VSTO执行此操作.我需要检查MailMerge.DataSource.DataFields上的所有记录是否都正确.我正在用这段代码来做到这一点.

I' have a Project that needs to do a mailmerge, I'm performing this with VSTO. I need to check if all records on the MailMerge.DataSource.DataFields are ok. i'm doing that with this code.

    public void verificarPersonas(Word.Document Doc)
    {
        ThisAddIn ThisAddIn = Globals.ThisAddIn;
        List<Personas> miListaPersonas = new List<Personas>();
        decimal nRecords = Doc.MailMerge.DataSource.RecordCount;
        if (nRecords == 0)
        {
            cambiarEstado("Empty db  or documento does'n prepared for mail merge", false);
        }
        else
        {
            string fieldIdentificacion = persParm("Identificacion");
            string fieldNombre = persParm("Nombres");
            string fieldApellido = persParm("Apellidos");
            string fieldEmail = persParm("Email");
            string fieldDireccion = persParm("Direccion");
            string fieldTelefono = persParm("Telefono");

            if (String.IsNullOrEmpty(fieldIdentificacion) || String.IsNullOrEmpty(fieldNombre))
            {
                cambiarEstado("", false);
                return;
            }
            else
            {
                for (int i = 1; i <= nRecords; i++)
                {
                    Doc.MailMerge.DataSource.FirstRecord = i;
                    Doc.MailMerge.DataSource.LastRecord = i;

                    //   Here Allways get the first record
                    dynamic fields = Doc.MailMerge.DataSource.DataFields;
                    //    ________________________________
                    Personas personaActual = new Personas();
                    personaActual.IdPersona = 0;
                    try
                    {
                        personaActual.Identificacion = fields(fieldIdentificacion).value;
                        personaActual.Nombres = fields(fieldNombre).value;
                        personaActual.Apellidos = (String.IsNullOrEmpty(fieldApellido) ? "" : fields(fieldApellido).value);
                        personaActual.Email = (String.IsNullOrEmpty(fieldEmail) ? "" : fields(fieldEmail).value);
                        personaActual.Direccion = (String.IsNullOrEmpty(fieldDireccion) ? "" : fields(fieldDireccion).value);
                        personaActual.Telefono = (String.IsNullOrEmpty(fieldTelefono) ? "" : fields(fieldTelefono).value);

                        miListaPersonas.Add(personaActual);
                    }
                    catch (Exception e)
                    {
                        cambiarEstado(""+e.Message, false);
                        return;
                    }
                }

                string listaPersonasJson = JsonConvert.SerializeObject(miListaPersonas);
                string respuesta = wt.getWebData("Personas", "verificarPersonasVSTO", new { personas = listaPersonasJson });

            }
        }
    }

我的问题是dynamic fields = Doc.MailMerge.DataSource.DataFields;总是获得第一条记录.

My problem is that dynamic fields = Doc.MailMerge.DataSource.DataFields; allways get the first record.

如何获取活动记录的datafields?

How can I do to get datafields for the active record ?

推荐答案

经过数小时的研究和尝试.得到的数据源字段集合在设置FirstRecord和Lastrecord时不会移动指针,必须使用activerecords,使用WdMailMergeActiveRecord枚举来移动它,如下所示:

After some hours of research and some tries. get that the fields collection of datasource dont move the pointer when you set FirstRecord and Lastrecord, it must be moved using activerecords, using WdMailMergeActiveRecord enumeration, sonething like this:

int nRecords = Doc.MailMerge.DataSource.RecordCount;

for (int i = 1; i <= nRecords; i++)
{
    Doc.MailMerge.DataSource.FirstRecord = i; //It doesn't work 
    Doc.MailMerge.DataSource.LastRecord = i; // it doesn't work
    Doc.MailMerge.DataSource.ActiveRecord = (i == 1 ?   
    Word.WdMailMergeActiveRecord.wdFirstDataSourceRecord :Word.WdMailMergeActiveRecord.wdNextDataSourceRecord);
    Doc.MailMerge.DataSource.ActiveRecord = (i == nRecords ? Word.WdMailMergeActiveRecord.wdLastDataSourceRecord : Doc.MailMerge.DataSource.ActiveRecord);
    dynamic fields = Doc.MailMerge.DataSource.DataFields;
}

这篇关于与邮件合并如何获取MailMerge.DataSource.DataFields的每个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:00