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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…