Changing cell background color


#1

Hi,

I'm doing a prototype of a web site with the evaluation version of Words, and can use aspose to do nearly everything I need.

Part of the web site looks through a word document for placeholders ('<<text>>'). Certain placeholders results in a code that means that if that placeholder is in a table cell, that cell should have a red or green background.

I can search and replace the placeholders with their respective values, but don't know how to: a) Determine if the text is part of a cell, and (b) how to change it's background.

Any suggestion would be much appreciated,

Steve


#2

Using Document.Range.Replace with ReplaceEvaluator will do the job perfectly in this case.

private void SearchAndFormat(Document doc)

{

// replace is used to do search and format in this case

doc.Range.Replace(new Regex("MakeMeGreen"), new ReplaceEvaluator(ReplaceEvaluator2), false);

}

private ReplaceAction ReplaceEvaluator2(object sender, ReplaceEvaluatorArgs e)

{

Cell cell = (Cell)e.MatchNode.GetAncestor(typeof(Cell));

if (cell != null)

{

cell.CellFormat.Shading.BackgroundPatternColor = Color.Green;

}

// we don't need to replace anything

return ReplaceAction.Skip;

}

As you can see in this example Replace method can serve not only for replacements but also as a powerful search-and-format engine.


#3

miklovan,

Many thanks for the swift reponse- it's been a great help!

Cheers,

Steve

P.S. I guess that there are quite a few developers who exclusively use vb.net rather than c#. Is it worth having a quick primer document somewhere outlining conversion tips (covering items like typeof vs. GetType)?


#4

Yes, it will be nice to post examples in both languages. Currently I am having handful of problems posting code in even one, because our post editor destroys code formatting when I paste and I need to set indents manually all over the code Sad [:(].

Usually I post examples in C# but I convert them to VB.NET if the user explicitly states that he needs VB.NET code.

For quick C# to VB.NET conversion I recommend using the following online tool:

http://kamalpatel.net/ConvertCSharp2VB.aspx

Strange that MS still don't offer conversion capabilities in Visual Studio. Should not be a big problem for them I think.

Best regards,