I create instance of Document class
Microsoft.Office.Interop.Word.Document
How i can insert table and text into special position in Document ?
For example after or before one bookmark
To add text in word doc, you can use
Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
for table:
Word.Range tableLocation = this.Range(ref start, ref end);
this.Tables.Add(tableLocation, 10, 11);
check this link for text and table.
Set rangeStart = ActiveDocument.Bookmarks("YourFirstBookmark").Range
Set rangeEnd = ActiveDocument.Bookmarks("YourEndBookmark").Range
var range= Range(rangeStart.Start, rangeEnd.End)
Related
I need to add underlined column headers to the header objects in a document. I am using C# and Microsoft.Office.Interop.Word
The pertinent parts of the code look like this...
foreach (Word.HeaderFooter header in wordSection.Headers)
{
int[] fiscalYears = RetrieveFiscalYears(docProfile);
string paddingFY = new String(' ', 8);
Word.Paragraph colParagraph = wordRng.Paragraphs.Add();
int year;
for (int i = fiscalYears.Length - 1; i >= 0; i--)
{
year = fiscalYears[i];
colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
//begin underline
colParagraph.Range.InsertAfter(paddingFY + year.ToString() + paddingFY);
//end underline
}
colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
colParagraph = wordRng.Paragraphs.Add();
colParagraph.set_Style(wordDoc.Styles["ColumnHeadings"]);
}
Basically it needs to look similar to ...
Expended Estimated Budgeted
2015 2016 2017
--------- ---------- --------
In the body of the document, my for loop looks like
foreach (int year in fiscalYears)
{
wordApp.Selection.TypeText(MARKUP_WORD_TAB);
wordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
wordApp.Selection.TypeText(paddingFY + year.ToString() + paddingFY);
wordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineNone;
}
But the when I use the selection object, it writes to the body of the document, not to the header/footer objects. I might be able to get around this by using the SeekHeader and making it the focus, but that offers its own challenges...
I've tried using the colParagraph.Range.Font.Underline object, but that underlines the entire line, not just the words that make up the column headings.
I've tried using a find object, but the execute doesn't find the text for some reason.
Appreciate any guidance you can provide.
I had to move the set style above the for loop and set a new range based on the paragraph range and move its start and end positions. Then apply the underlining to the new range.
So now it looks similar to ....
colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
colParagraph = wordRng.Paragraphs.Last; //reset the range to include the tab so the style can be applied.
colParagraph.set_Style(wordDoc.Styles["ColumnHeadings"]);
int year;
int start = colParagraph.Range.Text.Length - 1;
string yrHeading = string.Empty;
Word.Range underlineRange = null;
for (int i = 0 ; i < fiscalYears.Length; i++)
{
year = fiscalYears[i];
colParagraph = wordRng.Paragraphs.Last; //reset the range to include the last fiscal year that was entered.
start = colParagraph.Range.Text.Length - 1;
colParagraph.Range.InsertAfter(yrHeading);
colParagraph.Range.InsertAfter(MARKUP_WORD_TAB);
underlineRange = colParagraph.Range.Duplicate;
underlineRange.MoveStart(Word.WdUnits.wdCharacter, start);
underlineRange.MoveEnd(Word.WdUnits.wdCharacter, -2); //-2 = /t/r for tab & paragraph characters
underlineRange.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
}
colParagraph = wordRng.Paragraphs.Add();
using Word = Microsoft.Office.Interop.Word;
public static void CreateWord()
{
Word.Application objWord = new Word.Application();
Word.Document objDoc = objWord.Documents.Add();
Word.Paragraph objPara;
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Bold text aligned center";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objPara.Range.Bold = 1;
objPara.Range.InsertParagraphAfter();
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Regular text aligned left";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objPara.Range.Bold = 0;
objPara.Range.InsertParagraphAfter();
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Regular text aligned center\nwith something on the next line";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objPara.Range.Bold = 0;
objPara.Range.InsertParagraphAfter();
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Regular text aligned left, with some Bold text here\nand some regular text on next line";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objPara.Range.Bold = 0;
objPara.Range.InsertParagraphAfter();
objWord.Visible = true;
objWord.WindowState = Word.WdWindowState.wdWindowStateNormal;
}
The code above creates a Word document and writes text to it, but it looks like this:
I'm assuming that text in Microsoft.Office.Interop.Word is not supposed to act like that, or text should be added in some other way, but I'm failing to find any good information on how to do this.
So, my question is - how to add text from c# to a newly created word document with the right properties, so the result looks like this:
P.S. tips about creating tables using .Interop.Word would also be appreciated.
The key to working with content in Word documents is to understand how to work with Range objects. There's more than one way to achieve a goal of this nature. The following code snippet is my preference: collapsing the Range for each step (think of it like pressing the right-arrow key to "collapse" a selection to a point), rather than mixing Add, InsertAfter and similar methods.
For each change of formatting the Range must be "collapsed". So if you have bold in the middle of a line, that's a separate step. If numerous paragraphs should have the same formatting, they can be combined.
Environment.NewLine and \n can be used interchangeably.
Generally, assign text content, then format the Range.
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
doc.Paragraphs.Add();
Word.Range objRange = doc.Content;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Bold text aligned center" + Environment.NewLine;
objRange.Bold = 1;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Regular text aligned left" + Environment.NewLine;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objRange.Bold = 0;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Regular text aligned center\nwith something on the next line" + Environment.NewLine;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objRange.Bold = 0;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Regular text aligned left, with some ";
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Bold text here";
objRange.Bold = 1;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "\nand some regular text on next line" + Environment.NewLine;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objRange.Bold = 0;
I want to import data from excel sheet with richtext i.e. font family
But I can get only bold and italic style using below given code
Microsoft.Office.Interop.Excel.Range Range = (Microsoft.Office.Interop.Excel.Range)Cell;
int TextLength = Range.Text.ToString().Length;
for (int CharCount = 1; CharCount <= TextLength; CharCount++)
{
Microsoft.Office.Interop.Excel.Characters charToTest = Range.get_Characters(CharCount, 1);
bool IsBold = (bool)charToTest.Font.Bold;
bool IsItalic = (bool)charToTest.Font.Italic;
// other formatting tests here
}
Can anybody help me to get the rich text data from Excel Sheet using Interop or any other method in c#.net
Thanks in advance
How about:
Range someRange = activeWorksheet.Range["A1", "A1"];
var fontFamily = someRange.Font.Name;
I'm kinda new to building word add-ins with C# and I have a question that regards strings and font colors. I want to add a red string to an existing string in a cell on word 2010,
something like this:
Word.Range r = document.Range(0, 0);
r.Text = "text";
r.Font.ColorIndex = WdColorIndex.wdRed;
tbl.Cell(RowNum, RightColumn).Range.Text += r;
Any ideas?
Well, I figured something out, I did this:
int TblRng = tbl.Cell(RowNum, RightColumn).Range.End;
Range r = document.Range(TblRng-1, TblRng+1);
r.Text = Final;
r.Font.ColorIndex = WdColorIndex.wdRed;
int TblRng = tbl.Cell(RowNum, RightColumn).Range.End; - this defined the new range at the end of the location where I needed to add the red string.
If you want to replace an existing string with new red then use bellow code
object findText = stringText;
if (Range.Find.Execute(ref findText))
{
Range.Text = NewString; // replace old string with New
Range.Font.Color = WdColor.wdColorRed; // Set color of the string
}
im programming in C# and i need to add a simple phrase on the last page of each ms-word doc.
i have tried this
Word.Paragraph paragraph;
int totalPages;
Word.Range range;
range = doc.Content.Duplicate;
totalPages = range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
range = range.GoTo(Word.WdGoToItem.wdGoToPage, Type.Missing, totalPages, Type.Missing);
paragraph = doc.Paragraphs.Add(range);
paragraph.Range.Font.Name = "Arial";
paragraph.Range.Font.Size = 7;
paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
paragraph.Range.Text = encrypt;
but this code are adding a pharse in the last but one page .
How about something like this (you may need to fix the C# syntax):
doc.Content.InsertParagraphAfter();
Word.Paragraph paragraph = doc.Paragraphs(doc.Paragraphs.Count());
paragraph.Range.Font.Name = "Arial";
paragraph.Range.Font.Size = 7;
paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
paragraph.Range.Text = encrypt;