I’m working on a program in C# that uses Microsoft Word 14.0 Object Library to create a .doc file, add paragraphs to it and saves it. There is a small form with a button that does described actions (see the code below). This part has no problems.
Problem:
Current text in created word file will be the following:
Some text beff = 3.0
What I need to accomplish, is creating a paragraph, which has subscript characters inside.(in paragraph above letters "eff" should be subscripted):
The final document would contain around 100 of lines like above, with different characters that are subscripted.
I found a way to subscript the whole paragraph with line,
paragraph1.Range.Font.Subscript = 1;
but found no way to implement it on separate characters.
I’m also aware that there are subscript letters and numbers in Unicode that I could use, but, unfortunately, Unicode does not have full alphabet in subscript format, so that is not an option either.
Question: Is there a way for me to accomplish the Goal and insert something like “eff“ in subscript inside a paragraph in a freshly created Word Document?
Sample code:
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();
}
You can play around with the range's start and end point to get that job done, and even find the offset.
Hope it helps.
Create a Word document and add text with subscipt/superscript and unzip the .docx to examine it's XML content you will notice that the text containing the subscript/superscript is placed in a separate run element.
One way to achieve this is OpenXML SDK.Once you've downloaded and installed the SDK you can use the following code:
EDIT:
And here's a Interop solution:
In Word create a Word Macro which simulates the process you want. When done look at the VBA code it creates. That code will give you the what/how on the working of the interops which need to be called.