简体   繁体   English

mailsto与visto如何获取MailMerge.DataSource.DataFields的每条记录

[英]mailmerge with visto how to get each record of MailMerge.DataSource.DataFields

I' have a Project that needs to do a mailmerge, I'm performing this with VSTO. 我有一个需要执行邮件合并的项目,我正在使用VSTO执行此操作。 I need to check if all records on the MailMerge.DataSource.DataFields are ok. 我需要检查MailMerge.DataSource.DataFields上的所有记录是否都可以。 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 });

            }
        }
    }

My problem is that dynamic fields = Doc.MailMerge.DataSource.DataFields; 我的问题是dynamic fields = Doc.MailMerge.DataSource.DataFields; allways get the first record. 始终获得第一记录。

How can I do to get datafields for the active record ? 如何获取活动记录的datafields

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: 得到的数据源字段集合在设置FirstRecord和Lastrecord时不会移动指针,必须使用activerecords,使用WdMailMergeActiveRecord枚举来移动它,如下所示:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM