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

好久不见. 提交于 2019-12-12 01:16:42

问题


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

            }
        }
    }

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

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


回答1:


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


来源:https://stackoverflow.com/questions/14960266/mailmerge-with-visto-how-to-get-each-record-of-mailmerge-datasource-datafields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!