How to add subscript characters to paragraphs using Word Automation?

Im working on a C # program that uses the Microsoft Word 14.0 object library to create a .doc file, add paragraphs to it and save it. There is a small form with a button that describes the actions (see code below). This part has no problems.

Problem:

The current text in the created word file will be as follows:

Some texts beff = 3.0

What I need to do creates a paragraph that internally has subscript characters (in the paragraph above, the letters "eff" should be indexed):

enter image description here

The resulting document will contain about 100 lines, as indicated above, with different characters that are indexed.

I found a way to index the entire paragraph using a string,

paragraph1.Range.Font.Subscript = 1; 

but did not find a way to implement it on separate characters.

I also know that Unicode has letters and numbers in alphabetical order, but, unfortunately, Unicode does not have a complete alphabet in substring format, so this is also not an option.

Question: Is there a way to achieve the goal and insert something like "eff" in the index inside a paragraph in a newly created Word Document?

Code example:

 private void btnReport_Click(object sender, EventArgs e) { Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oDoc = oWord.Documents.Add(); var paragraph1 = oDoc.Content.Paragraphs.Add(); paragraph1.Range.Text = "Some text beff = 3.0"; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Word document|*.doc"; saveFileDialog1.Title = "Save the Word Document"; if (DialogResult.OK == saveFileDialog1.ShowDialog()) { string docName = saveFileDialog1.FileName; if (docName.Length > 0) { object oDocName = (object)docName; oDoc.SaveAs(ref oDocName); } } oWord.Quit(); } 
+6
source share
3 answers

Create a Word document and add text with subscipt / superscript and unzip .docx to examine its XML content, you will notice that the text containing the index / superscript is placed in a separate launch element.

One way to achieve this is with the OpenXML SDK . After you have downloaded and installed the SDK, you can use the following code:

 using System; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; namespace OpenXML { class Program { static void Main(string[] args) { using (var doc = WordprocessingDocument.Create("C:\\Subscript.docx", WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = doc.AddMainDocumentPart(); mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph p = body.AppendChild(new Paragraph()); p.AppendChild(AddRun(false, "Some text b ")); p.AppendChild(AddRun(true, "eff")); p.AppendChild(AddRun(false, "= 3.0")); } Console.WriteLine("Done..."); Console.ReadLine(); } public static Run AddRun(bool isSubscript, string text) { Run run = new Run(); if (isSubscript) { var props = new RunProperties(); var fontSize = new FontSizeComplexScript() { Val = "20" }; var vAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Subscript }; props.Append(fontSize); props.Append(vAlignment); run.Append(props); } run.Append(new Text(text)); return run; } } } 

EDIT:

And here is the Interop solution:

  using WordNS = Microsoft.Office.Interop.Word; WordNS.Document doc = _application.ActiveDocument; WordNS.Paragraph p = doc.Paragraphs.Add(); p.Range.Text = "Some text beff = 3.0"; int start = p.Range.Text.IndexOf("eff"); int end = p.Range.Text.IndexOf("="); WordNS.Range range = doc.Range(start, end); range.Select(); WordNS.Selection currentSelection = _application.Selection; currentSelection.Font.Subscript = 1; doc.SaveAs2("C:\\SubscriptInterop.docx"); 
+4
source

You can play with the start and end points of the range to do the job, and even find the offset.

 Range range = paragraph1.Range; range.Text = "Some text beff = 3.0"; if (range.Text.Contains("eff =")) { range.Start = range.Start + range.Text.IndexOf("eff ="); range.End = range.Start + 3; range.Font.Subscript = 1; } 

Hope this helps.

+3
source

In Word, create a Word macro that mimics the process you want. When this is done, look at the VBA code that it creates. This code will give you what / how it will work with the handlers that need to be called.

+2
source

All Articles