Tuesday, January 11, 2011

C#: Serialize an ASP WebControl

In one of my other posts I offered a solution to be able to serialize an object (pretty much any object) into string (http://kodesharp.blogspot.com/2009/10/c-serialize-object-to-xml-string.html). However that method does not work for web controls as there are more to the object like viewstate etc. So this method needs to be used instead, for being able to serialize WebControls:

public static string GetSerializedXmlStringForWebControls(WebControl webControl)
{
StringBuilder sb = new StringBuilder();
HtmlTextWriter tw = new HtmlTextWriter(new StringWriter(sb));
webControl.RenderControl(tw);
return tw.InnerWriter.ToString();
}

Wednesday, January 5, 2011

C#: Find all ContentControls in a Word document

public static List GetAllContentControls(ref Word.Document document)
{
List ccArray = new List();
//Body
for (int i = 1; i < document.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(document.ContentControls.get_Item(ref objIdx));
}
//Headers
foreach (Word.Section section in document.Sections)
{
for (int i = 1; i < section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.ContentControls.get_Item(ref objIdx));
}
for (int i = 1; i < section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.ContentControls.get_Item(ref objIdx));
}
for (int i = 1; i < section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ContentControls.get_Item(ref objIdx));
}
}
//Footers
foreach (Word.Section section in document.Sections)
{
for (int i = 1; i < section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages].Range.ContentControls.get_Item(ref objIdx));
}
for (int i = 1; i < section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.ContentControls.get_Item(ref objIdx));
}
for (int i = 1; i < section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ContentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.ContentControls.get_Item(ref objIdx));
}
}
//Textboxes
for (int j = 1; j < document.Shapes.Count + 1; j++)
{
object idx = j;
Word.Shape shape = document.Shapes.get_Item(ref idx);
if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
{
Word.ContentControls contentControls = shape.TextFrame.TextRange.ContentControls;
for (int i = 1; i < contentControls.Count + 1; i++)
{
object objIdx = i;
ccArray.Add(contentControls.get_Item(ref objIdx));
}
}
}
return ccArray;
}