Refactoring from VBA to C#

M

Mike Davis

Hi all,

I have a C# console app that I'm building to automate some Word
functionality. I would like to add code to that app that will conditionally
color some table cell backgrounds based on the text within the cell.

My coding experience is generally limited, especially when trying to convert
from one language to another.

That said, I have the following VBA code that works fine within Word 2003 --
it's assigned to a button control:

---
Private Sub CommandButton1_Click()

Dim oTbl As Table
Dim oCel As Cell
Dim oRng As Range

For Each oTbl In ActiveDocument.Tables
For Each oCel In oTbl.Range.Cells
Set oRng = oCel.Range
oRng.End = oRng.End - 1

Select Case oRng.Text
Case "R"
oCel.Shading.BackgroundPatternColorIndex = wdRed
Case "Y"
oCel.Shading.BackgroundPatternColorIndex = wdYellow
Case "G"
oCel.Shading.BackgroundPatternColorIndex = wdBrightGreen
Case "E"
oCel.Shading.BackgroundPatternColorIndex = wdTurquoise
End Select

Next
Next

End Sub
---

I would like to convert this functionality to work within the C# console
application I mentioned, rather than relying on a user to click and trigger
the functionality. The console app is now running the mail merge
functionality I'm using anyway.

If anyone can assist or point me to a web resource for converting VBA to C#
..NET code, I would really appreciate it.

Regards,

Mike Davis
 
C

Cindy M.

Hi Mike,
If anyone can assist or point me to a web resource for converting VBA to C#
..NET code, I would really appreciate it.
I don't know of any such resource, unfortunately. But I can try to
"convert" this for you, off the top of my head (meaning I'm not
testing), so there could be syntax errors

private void FormatTheTable(Word.Application wdApp)
{
Word.Document oDoc = wdApp.ActiveDocument;
Word.Tables oTables = oDoc.Tables;
foreach(Word.Table oTbl in oTables)
{
Word.Cells oCells = oTbl.Range.Cells;
foreach(Word.Cell oCel in oCells)
{
Word.Range oRng = oCel.Range;
oRng.End = oRng.End - 1;
string rngText = oRng.Text;
//Note: you may want to explicitly extract the first
character as the value for the string?
switch (rngText)
{
case "R":
oCel.Shading.BackgroundPatternColorIndex =
Word.WdColorIndex.wdRed;
break;
case "Y":
//AND SO FORTH
}
}
}


Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
M

Mike Davis

Hi Cindy,

Thanks so much for your reply...

I was actually able to "hack" my way through a solution, which is posted
below. This is a code snippet, so would have to be placed
syntactically-correct context to work fully:

====
foreach (Table oTbl in document.Tables)
{
foreach (Cell oCellItem in oTbl.Range.Cells)
{
Range oRange;
oRange = oCellItem.Range;
oRange.End = oRange.End - 1;

switch (oRange.Text)
{
case "R":
oCellItem.Shading.BackgroundPatternColor =
WdColor.wdColorRed;
break;
case "Y":
oCellItem.Shading.BackgroundPatternColor =
WdColor.wdColorYellow;
break;
case "G":
oCellItem.Shading.BackgroundPatternColor =
WdColor.wdColorBrightGreen;
break;
case "E":
oCellItem.Shading.BackgroundPatternColor =
WdColor.wdColorTurquoise;
break;
}
}
}

====

On a quick read of your code, this looks essentially the same with a few
minor differences... I couldn't get the syntax of
..BackgroundPatternColorIndex to work, so I went with .BackgroundPatternColor
instead...

-- Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top