Reputation: 15925
UPDATED ATTEMPT:
I've tried the following based on klugerama answer, but it doesn't seem to work for me:
var entries = new List<string>();
entries.Add("some text here, ");
entries.Add("some more text here, ");
entries.Add("and even more text here. ");
foreach (var entry in entries)
{
object newRangeStart = Globals.ThisDocument.bmkStart.Range.End;
Globals.ThisDocument.bmkStart.Range.InsertAfter(entry);
object newRangeEnd = Globals.ThisDocument.bmkStart.Range.End;
var newRange = Globals.ThisDocument.Range(ref newRangeStart, ref newRangeEnd);
if (entry == "some more text here, ")
{
newRange.Bold = -1;
}
}
First issue is that it inserts the text backwards, i.e:
and even more text here. some more text here, some text here,
instead of:
some text here, some more text here, and even more text here.
and the second issue is the original issue, in that bold isn't added. I am trying to get the output to become:
some text here, some more text here, and even more text here.
ORIGINAL QUESTION:
I am trying to create an action pane which gives the user lots of options. Based on the options selected, specific text gets inserted into the document. I have been able to create the action pane part and the text inserting part fairly quickly and easily. But the text is inserted without any formatting.
What is the correct way to insert text which easily allows me to add formatting to the text (pro grammatically) which will be inserted in the document?
At the moment, I am doing something like this to generate the text and insert it into the document which makes it difficult to apply formatting:
if (CheckBox1.Checked == true)
{
stMainText.Append("some text here, ");
}
if (CheckBox2.Checked == true)
{
stMainText.Append("some more text here, ");
}
if (CheckBox3.Checked == true)
{
stMainText.Append("and even more text here. ");
}
Globals.ThisDocument.bmkStart.Text = TheText.ToString();
If all 3 checkboxes were checked, the output would be something like:
some text here, some more text here, and even more text here.
Lets say I wanted the text to be inserted as follows (notice the bold):
some text here, some more text here, and even more text here.
How would I do that?
I am using VS2013 to create a word document level application.
Upvotes: 2
Views: 1485
Reputation: 1160
Question: What is the correct way to insert text which easily allows me to add formatting to the text (pro grammatically) which will be inserted in the document?
Answer: You need to extract the range of the appended text and style that range. This is solved with the out parameter of the provided function below.
/// <summary>
/// Appends text to a range
/// </summary>
/// <param name="range">The range to append text to.</param>
/// <param name="appendText">The text to append.</param>
/// <param name="appendedRange">The range of the appended text</param>
/// <returns>
/// The range of the combined old text and the appended text
/// </returns>
private Word.Range AppendToRange(Word.Range range, string appendText, out Word.Range appendedRange)
{
// Fetch indexes
object oldStartPosition = range.Start;
object oldEndPosition = range.End;
object newEndPosition = (int)oldEndPosition + appendText.Length;
// Append the text
range.InsertAfter(appendText);
// Define the range of the appended text
appendedRange = Range(ref oldEndPosition, ref newEndPosition);
// Return the range of the new combined range
return Range(ref oldStartPosition, ref newEndPosition);
}
The following code will append text at the end of the document and allow for styling of multiple parts of text inserts:
object startCharacter = 0;
object endCharacter = Globals.ThisDocument.Characters.Count;
Word.Range textRange = Range(ref startCharacter, ref endCharacter);
Word.Range styleRange;
textRange = AppendToRange(textRange , "some text here, ", out styleRange);
styleRange.Bold = 0;
styleRange.Font.Name = "Verdana";
styleRange.Font.Size = 10.0F;
textRange = AppendToRange(textRange , "some more text here, ", out styleRange);
styleRange.Bold = -1;
styleRange.Font.Name = "Times New Roman";
styleRange.Font.Size = 10.0F;
textRange = AppendToRange(textRange , "and even more text here.", out styleRange);
styleRange.Bold = 0;
styleRange.Font.Name = "Verdana";
styleRange.Font.Size = 20.0F;
Or with your code example from the original question:
// Assumes stMainText is of type Word.Range
Word.Range styleRange;
if (CheckBox1.Checked == true)
{
stMainText = AppendToRange(stMainText, "some text here, ", out styleRange);
// Example styling of the inserted text below
styleRange.Bold = 0;
styleRange.Font.Name = "Verdana";
styleRange.Font.Size = 20.0F;
}
if (CheckBox2.Checked == true)
{
stMainText = AppendToRange(stMainText, "some more text here, ", out styleRange);
// Example styling of the inserted text below
styleRange.Bold = -1;
styleRange.Font.Name = "Times New Roman";
styleRange.Font.Size = 10.0F;
}
if (CheckBox3.Checked == true)
{
stMainText = AppendToRange(stMainText, "and even more text here. ", out styleRange);
// Add necessary styling here
}
Upvotes: 2
Reputation: 5430
Here is Example. Try this:
Globals.ThisDocument.bmkStart.Bold = Globals.ThisDocument.WordTrue;
For making part of text bold here is example on MSDN.
Code from MSDN:
private void BookmarkFormattedText()
{
int WordTrue = 1;
this.Paragraphs[1].Range.InsertParagraphBefore();
this.Paragraphs[1].Range.InsertParagraphBefore();
this.Paragraphs[1].Range.Text = "This is text in the " + "first paragraph.";
this.Paragraphs[1].Range.Words[3].Bold = WordTrue;
Microsoft.Office.Tools.Word.Bookmark bookmark1 =
this.Controls.AddBookmark(this.Paragraphs[2].Range, "bookmark1");
bookmark1.FormattedText = this.Paragraphs[1].Range.Words[3];
}
UPDATE: I have modified klugerama answer
, as under:
void FormatText()
{
var entries = new List<string>();
entries.Add("some text here, ");
entries.Add("some more text here, ");
entries.Add("and even more text here. ");
foreach (var entry in entries)
{
object newRangeStart = Globals.ThisDocument.Paragraphs[1].Range.End-1;
Globals.ThisDocument.Paragraphs[1].Range.InsertAfter(entry);
object newRangeEnd = Globals.ThisDocument.Paragraphs[1].Range.End;
var newRange = Globals.ThisDocument.Range(ref newRangeStart, ref newRangeEnd);
if (entry == "some more text here, ")
{
newRange.Bold = 1;
//bookmark this range
Microsoft.Office.Tools.Word.Bookmark bmkStart;
bmkStart = this.Controls.AddBookmark(newRange, "RangeBookMark");
}
else
newRange.Bold = 0;
}
}
Output:
some text here, some more text here, and even more text here.
Upvotes: 0
Reputation: 601
Try using html to pro grammatically construct your text way you wanted and then create a word document out of the html file.
append what you want to bold with tags.
Please refer following thread on creating a word out of html document Convert HTML To Word Document
Upvotes: 0
Reputation: 3352
You'll need to define a different Range
object and set the Bold
property to True
for each insert. Since the bookmark doesn't automatically expand, you must select the inserted text, and create a new bookmark with the same name and add the selected text to that bookmark object. See this.
object True = -1;
foreach (var entry in YourCollectionOfEntries)
{
var bmkRange = Globals.ThisDocument.bmkStart.Range;
object newRangeStart = bmkRange.End;
bmkRange.InsertAfter(entry);
bmkRange.Select();
bmkRange = Globals.ThisDocument.Bookmarks.Add(Globals.ThisDocument.bmkStart.Name, Selection.Range);
object newRangeEnd = bmkRange.End;
var newRange = Globals.ThisDocument.Range(ref newRangeStart, ref newRangeEnd);
if (isBoldForThisEntry)
newRange.Bold = True;
}
Upvotes: 0