I wrote code that goes through word and Powerpoint documents and finds certain flags (example would be <> and changes the value to a value set by my .net application.
The problem is that, for example, the <> flag is changed 3 out of 8 times in the document, rather than all 8 occurances. This happens for multiple flags (there are about 25 in all) and we are not sure why.
I am writing the code in C# and using both Aspose.Word and Aspose.PowerPoint. The code is below.
try
{
IEnumerator selectedFiles = CreateDocs.CheckedItems.GetEnumerator();
while(selectedFiles.MoveNext())
{
if (selectedFiles.Current.ToString().EndsWith(".doc") | selectedFiles.Current.ToString().EndsWith(".dot") )
{
Aspose.Word.License license = new Aspose.Word.License();
license.SetLicense(“Aspose.Word.lic”);
//Word word = new Word();
Document doc = new Document(INPUTPATH + selectedFiles.Current);
//get all fields from document
string[] fieldsArray = doc.MailMerge.GetFieldNames();
foreach (string strField in fieldsArray)
{
//need to match column in datatable to field in document
if (dv.Table.Columns.Contains(strField))
{
doc.MailMerge.MappedDataFields.Add(strField,dv.Table.Columns[strField].ColumnName);
}
}
doc.MailMerge.Execute(dv);
doc.MailMerge.Execute(dv);
doc.Save(OUTPUTPATH + selectedFiles.Current.ToString().Remove(selectedFiles.Current.ToString().Length - 3,3) + “doc”);
}
if (selectedFiles.Current.ToString().EndsWith(".ppt"))
{
Presentation pres = new Presentation(INPUTPATH + selectedFiles.Current);
for(int i = 0; i < pres.Slides.Count; i++)
{
Slide slide = pres.Slides;
for(int j = 0; j < slide.Shapes.Count; j++)
{
Shape shape = slide.Shapes[j];
if (shape is TextFrame)
{
foreach(Paragraph para in ((TextFrame)shape).Paragraphs)
{
foreach(Portion port in para.Portions)
{
// Regex reg = new Regex("<<((.|\n)?)>>", RegexOptions.IgnoreCase);
Regex reg = new Regex("<<((.|\n))>>", RegexOptions.IgnoreCase);
foreach(Match objMatch in reg.Matches(port.Text))
{
string pptField = objMatch.ToString().Substring(2,objMatch.Length-4);
if(dv.Table.Columns.Contains(pptField))
{
string newPort = port.Text.Replace(objMatch.ToString(),dr[pptField].ToString());
port.Text = newPort;
}
}
}
}
}
}
for(int j = 0; j < slide.Placeholders.Count; j++)
{
Placeholder holder = slide.Placeholders[j];
if (holder is TextHolder)
{
foreach(Paragraph para in ((TextHolder)holder).Paragraphs)
{
foreach(Portion port in para.Portions)
{
Regex reg = new Regex("<<((.|\n)*)>>", RegexOptions.IgnoreCase);
foreach(Match objMatch in reg.Matches(port.Text))
{
string pptField = objMatch.ToString().Substring(2,objMatch.Length-4);
if(dv.Table.Columns.Contains(pptField))
{
string newPort = port.Text.Replace(objMatch.ToString(),dr[pptField].ToString());
port.Text = newPort;
}
}
}
}
}
}
}
pres.Write(OUTPUTPATH + selectedFiles.Current);
}
Thanks for any help.