简体   繁体   中英

Word Interop Cell custom text selection

In my case I need to have custom text in certain cell of the table. All cell must have vertical and horizontal alignment. Second-Last line must be Italic, and last line in this cell must be bold and green-colored. It must be like:

Some regular text: some text
Some other text: some text

Italic text:
BOLD GREEN TEXT

And all this must be in one cell. I have try to write first lines, then change cell.Range parameters and add next lines, but range methods changed all cell style, unfortunately. My code: (t1 - my table)

 Word.Table t1 = worddocument.Tables.Add(wordrange, 8, 3, ref defaultTableBehavior, ref autoFitBehavior);
                    t1.Cell(1, 2).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    t1.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                    t1.Cell(1, 2).Range.Text = "Tester: " + ini.IniReadValue("Main", "Name") + "\nDate: " + DateTime.Today.ToShortDateString() + "\n";
                    t1.Cell(1, 2).Range.Italic = 1;
                    t1.Cell(1, 2).Range.Text = "Italic text"; 

Also I have tried to create some paragraphs, but nothing good has happened.

private Word.Paragraphs wordparagraphs;
private Word.Paragraph wordparagraph;

object oMissing = System.Reflection.Missing.Value;
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
wordparagraphs = t1.Cell(1, 2).Range.Paragraphs;
wordparagraph = (Word.Paragraph)wordparagraphs[1];
t1.Cell(1, 2).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
t1.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordparagraph.Range.Text = "Tester: " + ini.IniReadValue("Main", "Name") + "\nDate: " + DateTime.Today.ToShortDateString() + "\nComponent: " + tabControl1.SelectedTab.Name + "\n";
wordparagraph = (Word.Paragraph)wordparagraphs[2];
wordparagraph.Range.Italic = 1;
wordparagraph.Range.Text = "Italic text"; 

In the result "Italic text" is overlapped with second line of first paragraph. So how to change some text in one cell?

So, I do this with paragraphs. Maybe it will be usefull for somebody.

t1.Cell(1,2).Range.ParagraphFormat.LineSpacing = 12; 
t1.Cell(1, 2).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
t1.Cell(1, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
object oMissing = System.Reflection.Missing.Value;
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
wordparagraphs = t1.Cell(1, 2).Range.Paragraphs;
    wordparagraphs[1].Range.Text = "Tester: " + ini.IniReadValue("Main", "Name");
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
    wordparagraphs[2].Range.Text = "Date: " + DateTime.Today.ToShortDateString();
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
    wordparagraphs[3].Range.Text = "Component: " + tabControl1.SelectedTab.Text;
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
    //freespace?
t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
wordparagraphs[5].Range.Italic = 1;
wordparagraphs[5].Range.Text = "Task status:";

t1.Cell(1, 2).Range.Paragraphs.Add(ref oMissing);
wordparagraphs[6].Range.Bold = 1;
wordparagraphs[6].Range.Italic = 0;
if (impStatus.SelectedIndex == 0)
    wordparagraphs[6].Range.Font.ColorIndex = Word.WdColorIndex.wdGreen;
                       else 
    wordparagraphs[6].Range.Font.ColorIndex = Word.WdColorIndex.wdRed;
    wordparagraphs[6].Range.Font.Size = 18;
    wordparagraphs[6].Range.Text = impStatus.Text.ToUpper() ;

also if somebody have better variant - you can tell me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM