Make Code Fast Execute - replace For loops - c#

It takes lot of time to execute these loops due to for loop implementation
How can I replace it to be more fast, the under laying table do not have much records too, plus I have made the primary keys too , but still the for loops are slow
public List<BusinessLayer.Transactions.CDANumberTracking> GetPOUnusedCDANumberTrackingItems(string code)
{
List<BusinessLayer.Transactions.CDANumberTracking> results = new List<BusinessLayer.Transactions.CDANumberTracking>();
List<Entity.Transactions.CDANumberTracking> SoUsedBagList = new List<Entity.Transactions.CDANumberTracking>();
List<Entity.Transactions.POCDANumberTracking> rejects = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
List<Entity.Transactions.POCDANumberTracking> returns = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
List<Entity.Transactions.POCDANumberTracking> rejectList = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
List<Entity.Transactions.POCDANumberTracking> returnRejectList = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
List<Entity.Transactions.POCDANumberTracking> SearchList = new List<SalesOrderModule.Entity.Transactions.POCDANumberTracking>();
try
{
if (!InOpenLookup)
(Connection as SQL).BeginTransaction();
DataLayer.Tables.PLSPOCDANumberTrackingDNew sampleTable = new SalesOrderModule.DataLayer.Tables.PLSPOCDANumberTrackingDNew(this.Connection);
sampleTable.SearchCriteria[0].Value = code.Trim();
sampleTable.SearchCriteria[1].Value = (int)0;
List<Entity.Transactions.POCDANumberTracking> results1 = sampleTable.Reads(false);
if (results1.Count > 0)
{
rejectList.AddRange(results1);
}
DataLayer.Tables.PLSPOCDANumberTrackingReturnD sampleTable2 = new SalesOrderModule.DataLayer.Tables.PLSPOCDANumberTrackingReturnD(this.Connection);
sampleTable2.SearchCriteria[0].Value = code.Trim();
List<Entity.Transactions.POCDANumberTracking> results2 = sampleTable2.Reads(false);
if (results2.Count > 0)
{
returnRejectList.AddRange(results2);
}
DataLayer.Tables.PLSPOCDANumberTrackingD sampleTable3 = new SalesOrderModule.DataLayer.Tables.PLSPOCDANumberTrackingD(this.Connection);
sampleTable3.SearchCriteria[0].Value = code.Trim();
SearchList = sampleTable3.Reads(false);
DataLayer.Tables.PSOMCDANumberTrackingD sampleTable4 = new SalesOrderModule.DataLayer.Tables.PSOMCDANumberTrackingD(this.Connection, null);
sampleTable4.SearchCriteria[3].Value = code.Trim();
sampleTable4.SearchCriteria[6].Value = false;
SoUsedBagList = sampleTable4.Read(false);
//process data...
Entity.Transactions.POCDANumberTracking temp;
foreach (Entity.Transactions.POCDANumberTracking rejectItem in rejectList)
{
for (int i = rejectItem.From; i <= rejectItem.To; i++)
{
temp = new SalesOrderModule.Entity.Transactions.POCDANumberTracking();
temp.From = i;
temp.To = i;
temp.Code = rejectItem.Code.Trim();
temp.GrnNo = rejectItem.GrnNo.Trim();
temp.WbcNo = rejectItem.WbcNo.Trim();
rejects.Add(temp);
}
}
//returns
foreach (Entity.Transactions.POCDANumberTracking returnItem in returnRejectList)
{
for (int i = returnItem.From; i <= returnItem.To; i++)
{
temp = new SalesOrderModule.Entity.Transactions.POCDANumberTracking();
temp.From = i;
temp.To = i;
temp.Code = returnItem.Code.Trim();
temp.GrnNo = returnItem.GrnNo.Trim();
temp.WbcNo = returnItem.WbcNo.Trim();
returns.Add(temp);
}
}
Entity.Transactions.CDANumberTracking temp2;
Entity.Transactions.CDANumberTracking temp3;
Entity.Transactions.POCDANumberTracking temp4;
foreach (Entity.Transactions.POCDANumberTracking searchItem in SearchList)
{
for (int i = searchItem.From; i <= searchItem.To; i++)
{
temp = null;
temp3 = null;
temp4 = null;
//check if the bag is on reject list
temp = rejects.Find(delegate(Entity.Transactions.POCDANumberTracking tc) { return (tc.From == i && tc.WbcNo.Trim().ToUpper() == searchItem.WbcNo.Trim().ToUpper() && tc.GrnNo.Trim().ToUpper() == searchItem.GrnNo.Trim().ToUpper()); });
if (temp != null)
continue;
//check if the bag is on return list
temp4 = returns.Find(delegate(Entity.Transactions.POCDANumberTracking tcc) { return (tcc.From == i && tcc.GrnNo.Trim().ToUpper() == searchItem.GrnNo.Trim().ToUpper()); });
if (temp4 != null)
continue;
//check if the bag is alredy used in So module...
temp3 = SoUsedBagList.Find(delegate(Entity.Transactions.CDANumberTracking cda) { return (cda.Code.Trim().ToUpper() == searchItem.Code.Trim().ToUpper() && cda.BagNo == searchItem.From); });
if (temp3 != null)
continue;
temp2 = new SalesOrderModule.Entity.Transactions.CDANumberTracking();
temp2.BagNo = i;
temp2.Code = searchItem.Code.Trim();
temp2.LineNo = 0;
temp2.Location = string.Empty;
temp2.WbcNo = string.Empty;
temp2.ID = null;
temp2.IsReturned = false;
temp2.IsSelected = false;
temp2.ItemNo = string.Empty;
temp2.Status = SalesOrderModule.Entity.ModifyStatus.New;
results.Add(BusinessLayer.Transactions.CDANumberTracking.GetCDANumberTracking(this, temp2, null));
}
}
if (!InOpenLookup)
(Connection as SQL).EndTransaction();
}
catch (Exception er)
{
if (!InOpenLookup)
(Connection as SQL).Rollback();
throw er;
}
return results;
}
the for loop under second for each need to placed ... need some help

You should factor out of the inner loop everything you can. As the code stands right now, you are unecessarily repeating the following operations:
returnItem.Code.Trim();
returnItem.GrnNo.Trim();
returnItem.WbcNo.Trim();
I have nowhere near enough information to judge if this will have any performance impact.
Other suspects are new SalesOrderModule.Entity.Transactions.POCDANumberTracking() and returns.Add(temp). If returns is somekind of ordered list, then this could have a considerable performance hit. If its a simple List then it shouldn't and there isn't much you could do to improve it anyways.
Concerning the constructor, only you know how expensive it is but there is not much you can do to avoid it either.
All that said, your code would look something like this:
Entity.Transactions.POCDANumberTracking temp;
foreach (Entity.Transactions.POCDANumberTracking returnItem in returnRejectList)
{
var code = returnItem.Code.Trim();
var grnNo = returnItem.GrnNo.Trim();
var wbcNo = returnItem.WbcNo.Trim();
for (int i = returnItem.From; i <= returnItem.To; i++)
{
temp = new SalesOrderModule.Entity.Transactions.POCDANumberTracking();
temp.From = i;
temp.To = i;
temp.Code = code;
temp.GrnNo = grnNo;
temp.WbcNo = wbcNo;
returns.Add(temp);
}
}

I have to limit the entries coming under the foreach loops, that is best way by debugging the code
so the data layer codes referring in the
sampleTable.Reads(false);
sampleTable2.Reads(false);
sampleTable3.Reads(false);
sampleTable4.Reads(false);
need to modified by including the Item for search (I mean the SQL STATEMENTS)

Related

c# Interop: How to Selection.Find to Find in Current Selection to doc

List<FillInLocation> locationRes = null;
if (range == null) return locationRes;
range.Application.Selection.Find.ClearFormatting();
range.Application.Activate();
System.Threading.Thread.Sleep(50);
range.Select();
Selection sel = range.Application.Selection;
Find find = sel.Range.Find;
find.Text = #"\(  {1,}\)";
find.Replacement.Text = string.Empty;
find.Forward = true;
find.Wrap = WdFindWrap.wdFindStop;
find.Format = true;
find.MatchWildcards = true;
find.Font.Underline = WdUnderline.wdUnderlineNone;
int iStart = 0, iEnd = 0;
List<int> rangeEnds = new List<int>();
while (find.Execute())
{
iStart = sel.Range.Start;
iEnd = sel.Range.End;
if (rangeEnds.Contains(iEnd)) continue;
if (iEnd <= iStart) continue;
if (locationRes == null) locationRes = new List<FillInLocation>();
locationRes.Add(new FillInLocation(iStart, iEnd));
}
return locationRes;
Interop: How to Selection.Find to Find in Current Selection to doc
I want to specify range to query specific text instead of all query

How to insert to database in bulk instead of inserting it 1 by 1

How do you make the list receives more than 1 value based on the quantity of the SelectedList.Count.
Code:
for (int i = 0; i < SelectedList.Count; i++)
{
lastSeriesNo++;
string assetcodegen = string.Format("{0}-{1}-{2}", List[i].AssetCategoryID, CurrentApplication.Now.Year, lastSeriesNo.ToString().PadLeft(5, '0'));
AssetCodeOfficial[i] = assetcodegen;
var list = (from x in ctx.DataContext.AssetRegistryEntities
where x.AssetCode == SelectedList[i].AssetCode
select x
).AsEnumerable();
foreach (var asset in list)
{
asset.SeriesNumber = (short)lastSeriesNo;
asset.Status = 'A';
asset.IsTemp = false;
asset.UpdatedBy = CurrentApplication.CurrentUserId;
asset.UpdatedDate = asset.AssetCreatedDate = CurrentApplication.Now;
AssetCodetemp[i] = asset.AssetCode;
depreciationInMonths = asset.DepnInMonths;
ctx.DataContext.SubmitChanges();
}
}
Thank you all guys for the help, I manage to fix my problem. It already saves the data to the database as bulk not 1 by 1 saving.
So I use lambda expression for my list and use the .addRange to add item to the list.
list.AddRange(ctx.DataContext.AssetRegistryEntities.Where(x=>x.AssetCode.Trim() == SelectedList[i].AssetCode.Trim()));
Code:
List<NXpert.FixedAsset.DataAccess.AssetRegistryEntity> list = new List<NXpert.FixedAsset.DataAccess.AssetRegistryEntity>();
for (int i = 0; i < SelectedList.Count; i++)
{
lastSeriesNo++;
string assetcodegen = string.Format("{0}-{1}-{2}", List[i].AssetCategoryID, CurrentApplication.Now.Year, lastSeriesNo.ToString().PadLeft(5, '0'));
AssetCodeOfficial[i] = assetcodegen;
list.AddRange(ctx.DataContext.AssetRegistryEntities.Where(x=>x.AssetCode.Trim() == SelectedList[i].AssetCode.Trim()));
AssetCodetemp[i] = list[i].AssetCode;
}
foreach (var asset in list)
{
asset.SeriesNumber = (short)lastSeriesNo;
asset.Status = 'A';
asset.IsTemp = false;
asset.UpdatedBy = CurrentApplication.CurrentUserId;
asset.UpdatedDate = asset.AssetCreatedDate = CurrentApplication.Now;
depreciationInMonths = asset.DepnInMonths;
}
ctx.DataContext.SubmitChanges();

How do I make my generic method a way to choose which columns to return from mongoDB using C#?

This is my method that returns all columns of the mongo, I need to adapt it to send parameters to choose the return columns.
public virtual async Task<ListaFiltrada<TEntidade>> GetAllAsync(Paginacao paginacao = null,
Expression<Func<TEntidade, bool>> filtro = null, params Ordenacao<TEntidade>[] ordenacao)
{
if (!paginacao)
paginacao = new Paginacao();
var filtroEmBranco = new BsonDocument();
var quantidade = await MongoCollection.CountDocumentsAsync(filtroEmBranco);
var filtroMongo = filtro == null ? filtroEmBranco : Filtro.Where(filtro);
var cursor = MongoCollection.Find(filtroMongo);
if (ordenacao != null && ordenacao.Length > 0)
{
var ordem = new SortDefinition<TEntidade>[ordenacao.Length];
for (int i = 0; i < ordenacao.Length; i++)
if (ordenacao[i].Ascendente)
ordem[i] = Ordenacao.Ascending(ordenacao[i].Campo);
else
ordem[i] = Ordenacao.Descending(ordenacao[i].Campo);
cursor.Sort(Ordenacao.Combine(ordem));
}
return new ListaFiltrada<TEntidade>
{
Total = quantidade,
Pagina = paginacao.Pagina,
TotalFiltrado = await cursor.CountDocumentsAsync(),
Lista = await cursor.Skip(paginacao.Pular).Limit(paginacao.Quantidade).ToListAsync(),
};
}
I think of using some generic object as a parameter to send choose the columns that I want to return in the list.
If anyone has any questions or suggestions, I am available for more information :D
Well guys, with the help of a good friend of mine Julius, I was able to do this in a very quiet way. Follow and, I attach the resolution
public virtual async Task<ListaFiltrada<TEntidade>> GetAllAnyColumnAsync(Paginacao paginacao = null, Expression<Func<TEntidade, bool>> filtro = null,
Expression<Func<TEntidade, TEntidade>> projectionExpression = null, params Ordenacao<TEntidade>[] ordenacao)
{
if (!paginacao)
paginacao = new Paginacao();
var filtroEmBranco = new BsonDocument();
var quantidade = await MongoCollection.CountDocumentsAsync(filtroEmBranco);
var filtroMongo = filtro == null ? filtroEmBranco : Filtro.Where(filtro);
var cursor = projectionExpression == null ? MongoCollection.Find(filtroMongo) : MongoCollection.Find(filtroMongo).Project(projectionExpression);
if (ordenacao != null && ordenacao.Length > 0)
{
var ordem = new SortDefinition<TEntidade>[ordenacao.Length];
for (int i = 0; i < ordenacao.Length; i++)
if (ordenacao[i].Ascendente)
ordem[i] = Ordenacao.Ascending(ordenacao[i].Campo);
else
ordem[i] = Ordenacao.Descending(ordenacao[i].Campo);
cursor.Sort(Ordenacao.Combine(ordem));
}
if (ordenacao != null && ordenacao.Length > 0)
{
var ordem = new SortDefinition<TEntidade>[ordenacao.Length];
for (int i = 0; i < ordenacao.Length; i++)
if (ordenacao[i].Ascendente)
ordem[i] = Ordenacao.Ascending(ordenacao[i].Campo);
else
ordem[i] = Ordenacao.Descending(ordenacao[i].Campo);
cursor.Sort(Ordenacao.Combine(ordem));
}
return new ListaFiltrada<TEntidade>
{
Total = quantidade,
Pagina = paginacao.Pagina,
TotalFiltrado = await cursor.CountDocumentsAsync(),
Lista = await cursor.Skip(paginacao.Pular).Limit(paginacao.Quantidade).ToListAsync(),
};
}
I leave here a super save for my brother Julis who saved me with this help.
I hope this code helps somebody someday.
#Peace

how to order result by displaying each job type by order

I have a search query which search for all jobs in the database and than displays them in accordance to the most recent ones filtering the data by date as follows:
result = db.AllJobModel.Where(a => a.JobTitle.Contains(searchTitle) && a.locationName.Contains(searchLocation)).ToList());
result = (from app in result orderby DateTime.ParseExact(app.PostedDate, "dd/MM/yyyy", null) descending select app).ToList();
result = GetAllJobModelsOrder(result);
after that I have a method GetAllJobModelsOrder which displays jobs in order which seems to be work fine but in my case its not ordering jobs so I need to understand where I am wrong:
private List<AllJobModel> GetAllJobModelsOrder(List<AllJobModel> result)
{
var list = result.OrderBy(m => m.JobImage.Contains("job1") ? 1 :
m.JobImage.Contains("job2") ? 2 :
m.JobImage.Contains("job3") ? 3 :
m.JobImage.Contains("job4") ? 4 :
m.JobImage.Contains("job5") ? 5 :
6)
.ToList();
return list;
}
The result I get is about 10 jobs from job1 and than followed by other jobs in the same order what I would like to achieve is to filter the most recent jobs than display one job from each type of a job.
An example of the input would be as follows:
AllJobModel allJobModel = new AllJobModel
{
JobDescription = "Description",
JobImage = "Job1",
JobTitle = "title",
locationName = "UK",
datePosted = "15/06/2020",
}
The output that I get is as follows:
In where result should be mixed from different jobs.
Excepted resulta as follows a specific order of job source--1. TotalJob[0] :: 2. MonsterJob[0] :: 3. Redd[0] :: 4. TotalJob[ 1 ] :: 5. MonsterJob[ 1 ] ::6. Redd[ 1 ]:
I have tried the following solution but list data structure seems to be not stroing data in order:
private List<AllJobModel> GetAllJobModelsOrder(List<AllJobModel> result)
{
string lastItem = "";
List<AllJobModel> list = new List<AllJobModel>();
int pos = -1;
while(result.Count != 0)
{
for(int i = 0; i < result.Count; i++)
{
if(result[i].JobImage.Contains("reed") && (lastItem == "" || lastItem == "total" || lastItem == "monster"))
{
pos++;
list.Insert(pos,result[i]);
lastItem = "reed";
result.Remove(result[i]);
break;
}else if (result[i].JobImage.Contains("total") && (lastItem == "reed" || lastItem == "monster" || lastItem == "" ))
{
pos++;
list.Insert(pos, result[i]);
lastItem = "total";
result.Remove(result[i]);
break;
}else if(result[i].JobImage.Contains("monster.png") && (lastItem =="total" || lastItem == "reed" || lastItem == "" ))
{
pos++;
list.Insert(pos, result[i]);
lastItem = "monster";
result.Remove(result[i]);
break;
}else if(result[i].JobImage.Contains("reed") &&( lastItem == "reed"))
{
if(result.FirstOrDefault(a=>a.JobImage.Contains("total") || a.JobImage.Contains("monster")) != null)
{
lastItem = "total";
break;
}
else
{
pos++;
list.Insert(pos, result[i]);
lastItem = "reed";
result.Remove(result[i]);
break;
}
}
else if (result[i].JobImage.Contains("total") && (lastItem == "total"))
{
if (result.FirstOrDefault(a => a.JobImage.Contains("reed") || a.JobImage.Contains("monster")) != null)
{
lastItem = "monster";
break;
}
else
{
pos++;
list.Insert(pos, result[i]);
lastItem = "total";
result.Remove(result[i]);
break;
}
}
else if (result[i].JobImage.Contains("monster") && (lastItem == "monster"))
{
if (result.FirstOrDefault(a => a.JobImage.Contains("total") || a.JobImage.Contains("reed")) != null)
{
lastItem = "reed";
break;
}
else
{
pos++;
list.Insert(pos, result[i]);
lastItem = "monster";
result.Remove(result[i]);
break;
}
}
}
}
return list;
}
also what I found is that the value of the elements in the list is different from what its actual value. So I am not sure if this is an error with my code or with visual studio:
you can use the below concept here. I am sorting a list based on the numeric value at the end of each string value using Regex.
List<string> ls = new List<string>() { "job2", "job1", "job4", "job3" };
ls = ls.OrderBy(x => Regex.Match(x, #"\d+$").Value).ToList();
Certainly the requirement was not as straight forward as initially thought. Which is why I am offering a second solution.
Having order by and rotate the offering "Firm" in a type of priority list for a set number of times and then just display whatever comes after that requires a custom solution. If the one algorithm you have works and you are happy then great. Otherwise, I have come up with this algorithm.
private static List<AllJobModel> ProcessJobs(List<AllJobModel> l)
{
var noPriorityFirst = 2;
var orderedList = new List<AllJobModel>();
var priorityImage = new string[] { "TotalJob", "MonsterJob", "Redd" };
var gl = l.OrderBy(o => o.datePosted).GroupBy(g => g.datePosted).ToList();
foreach (var g in gl)
{
var key = g.Key;
for (int i = 0; i < noPriorityFirst; i++)
{
foreach (var j in priorityImage)
{
var a = l.Where(w => w.datePosted.Equals(key) && w.JobImage.Equals(j)).FirstOrDefault();
if (a != null)
{
orderedList.Add(a);
var ra = l.Remove(a);
}
}
}
foreach (var j in l.ToList())
{
var a = l.Where(w => w.datePosted.Equals(key)).FirstOrDefault();
if (a != null)
{
orderedList.Add(a);
var ra = l.Remove(a);
}
}
}
return orderedList;
}
To test this process I have created this Console app:
static void Main(string[] args)
{
Console.WriteLine("Start");
var l = CreateTestList();
var lJobs = ProcessJobs(l);
lJobs.ForEach(x => Console.WriteLine($"{x.datePosted} : {x.JobImage}"));
}
private static List<AllJobModel> CreateTestList()
{
var orderedList = new List<AllJobModel>();
var l = new List<AllJobModel>();
var priorityImage = new string[] { "TotalJob", "MonsterJob", "Redd" };
var rand = new Random();
var startTime = DateTime.Now;
for (int i = 0; i < 20; i++)
{
var j = rand.Next(0, priorityImage.Length);
var h = rand.Next(0, 4);
var a = new AllJobModel();
a.JobDescription = "Desc " + i;
a.JobImage = priorityImage[j];
a.JobTitle = "Title" + i;
a.locationName = "UK";
a.datePosted = startTime.AddDays(h);
l.Add(a);
}
for (int i = 0; i < 20; i++)
{
var j = rand.Next(0, priorityImage.Length);
var h = rand.Next(0, 4);
var a = new AllJobModel();
a.JobDescription = "Desc " + i;
a.JobImage = "Job " + i;
a.JobTitle = "Title" + i;
a.locationName = "UK";
a.datePosted = startTime.AddDays(h);
l.Add(a);
}
return l;
}

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

Categories

Resources