13

My program needs to generate highly simple reports in the Office .doc format (non-XML), and certain parts of the document need to be bold. I've been looking at the documentation for defining ranges, which is partly what my code derives from at the moment. This part of the documentation doesn't really give me enough details to implement this in general in my document. Here is my code so far:

object miss = System.Reflection.Missing.Value;
object Visible = true;
object start = 0;

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Document report = WordApp.Documents.Add(ref miss, ref miss, ref miss, ref miss);

String header = "Bold Header: ";
Range headerRange = report.Range(ref start, ref miss);
headerRange.Text = header;
headerRange.Font.Bold = -1;

String data = "Information underneath the header";
Range dataRange = report.Range();
dataRange.Text = data;
dataRange.Font.Bold = 1;

object filename = "test.doc";

report.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
object routeDocument = true;
WordApp.Visible = true;

This produces a word document with only the text **Information underneath the header**. This is a simple example.

My document won't get much more complicated than this, but I'm hoping to generate Word documents based on variable amounts of data, with bold text and non-bold text dispersed throughout.

Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
  • I think you want this: http://msdn.microsoft.com/en-us/library/aa192495%28v=office.11%29.aspx#wordobject_link7 It's a `Selection` object, not a range. – Robert Harvey Jul 19 '12 at 15:38
  • Is there any other documentation or examples on that besides [this](http://msdn.microsoft.com/en-us/library/6b9478cs%28v=vs.80%29.aspx)? Once again, the examples for these aren't all that great. – Ricardo Altamirano Jul 19 '12 at 15:49

3 Answers3

10

Here's an answer that I came up with that will allow you to have part of a string bold and regular in the same string.

What I was doing was automated, but the same applies if you know what you're doing. Keep in mind too that the Bold is only an int, there is no boolean true/false (for some reason).

As per Ricardo's excellent point, I'll post the code here as well:

private void InsertMultiFormatParagraph(string psText, int piSize, int piSpaceAfter = 10) {
    Word.Paragraph para = mdocWord.Content.Paragraphs.Add(ref mobjMissing);

    para.Range.Text = psText;
    // Explicitly set this to "not bold"
    para.Range.Font.Bold = 0;
    para.Range.Font.Size = piSize;
    para.Format.SpaceAfter = piSpaceAfter;

    object objStart = para.Range.Start;
    object objEnd = para.Range.Start + psText.IndexOf(":");

    Word.Range rngBold = mdocWord.Range(ref objStart, ref objEnd);
    rngBold.Bold = 1;

    para.Range.InsertParagraphAfter();
}

Obviously, if you are trying to abstract this even further, you can add a parameter for the char or string so you can change what is being used to set the bold start/stop.

One thing to note that was discussed in the comments of the other thread was that for some reason, Bold is only an int. There is no bool value for setting that. It's weird, I know.

Community
  • 1
  • 1
krillgar
  • 12,596
  • 6
  • 50
  • 86
  • If you would, post the code from that answer into your answer here so we can discuss it in ways relevant to this question. – Ricardo Altamirano Jul 23 '12 at 13:27
  • No worries. I'm trying to work this code into a test application at the moment, but I'll post how it works when completed in the next few days. – Ricardo Altamirano Jul 23 '12 at 14:21
  • I accepted your answer because it does do what I asked about in my original question. I believe my needs will actually be quite complex and therefore may require q more complex approach, relative to the level of sophistication present in Interop.Word, but so far your idea is the best I have. – Ricardo Altamirano Jul 23 '12 at 15:18
  • When I had to implement this, I was generating a report from an XML generated structure of the data. I was only writing a couple lines at most at a time, especially through this method. Good luck! – krillgar Jul 23 '12 at 17:36
1

You can simply use Paragraph object to customize formatting of different text blocks. Example code as below:

object DocumentEndIndex = "\\endofdoc";
object endDocument = wordDocument.Bookmarks.get_Item(ref DocumentEndIndex).Range;
Paragraph para = wordDocument.Content.Paragraphs.Add(ref endDocument);
para.Range.Text = text;
para.Range.set_Style(ref headingLevel);
// do format the text with para.Range object as you want
para.Range.InsertParagraphAfter();

Hope this helps.

Duy Pham
  • 35
  • 2
  • 11
-1

It's an old question, but as I faced the same problem and this didn't help me for modifications in header or footer, but helped me figure how to do it, here is my solution:

Word.Paragraph p = c2.Range.Paragraphs.Add(ref missing);
p.Range.Text = "your trip at " + sejour.Location;
SetTextColor(p.Range, Word.WdColor.wdColorWhite,0, p.Range.Text.Length - 1);
SetTextSize(p.Range, (float)14, 0, p.Range.Text.Length - 1);
SetTextSize(p.Range, (float)16, p.Range.Text.Length - 2 - sejour.Location.Length, sejour.Location.Length);

public void SetTextColor( Word.Range range, Microsoft.Office.Interop.Word.WdColor color, int start, int length)
{
    Word.Range rng = range;
    rng.Start = range.Start + start;
    rng.End = range.Start + start + length;
    rng.Font.Color = color;
}

public void SetTextSize(Word.Range range, float size, int start, int length)
{
    Word.Range rng = range;
    rng.Start = range.Start + start;
    rng.End = range.Start + start + length;
    rng.Font.Size = size;
}
Jarosław Kończak
  • 3,387
  • 2
  • 19
  • 36
tzarad
  • 1
  • 1