Solved

C# .NET Microsoft.Office.Interop.Word Issues

Posted on 2013-10-23
4
898 Views
Last Modified: 2013-10-28
I have a MS Word template (.dotx) that has several text boxes in it with pre-defined styles (ie: bold and 16pt Arial). Each text box has a unique Bookmark name.

In my code, I am able, without issue, to add text to these text boxes using the Bookmark names. However, when I try to change the font size, it doesn't change.

Here is my partial code. Note that line 22 works and line 23 does not. No matter what, when the Word Doc is created, the font-size of that textbox stays at 16pt.

            ApplicationClass oWordApp = new ApplicationClass();
            object oTemplate = "C:\\temp\\MyTemplate.dotx";
            object fileName = "C:\\temp\\MyOutput.docx";
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;

            Document oWordDoc = oWordApp.Documents.Add(ref oTemplate,
                ref missing, ref missing, ref missing);
            oWordDoc.Activate();

            for (int i = 0; i < 10; i++)
            {
                object oFix = "Fixture" + i;

                string fixtureName = "THE FIXTURE NAME";
                float fontSize = (fixtureName.Length <= 11) ? 16 :
                    (fixtureName.Length <= 13) ? 14 :
                    (fixtureName.Length <= 15) ? 12 :
                    (fixtureName.Length <= 17) ? 11 : 10;

                oWordDoc.Bookmarks.get_Item(ref oFix).Range.Text = fixtureName;
                oWordDoc.Bookmarks.get_Item(ref oFix).Range.Font.Size = fontSize;
            }

            oWordDoc.SaveAs(fileName);
            oWordApp.Application.Quit(ref missing, ref missing, ref missing);

            oWordApp = null;
            oWordDoc = null;

Open in new window

Any thoughts as to how to make this work properly?
0
Comment
Question by:infotechelg
  • 3
4 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 39595096
Select the range before applying font size:

oWordDoc.Bookmarks.get_Item(ref oFix).Range.Select();
oWordDoc.Bookmarks.get_Item(ref oFix).Range.Font.Size = fontSize;
0
 

Author Comment

by:infotechelg
ID: 39595318
EaswaranP,

Man, I was hoping that would work. But, unfortunately, the result is still the same.

                oWordDoc.Bookmarks.get_Item(ref oFix).Range.Text = fixtureName;
                oWordDoc.Bookmarks.get_Item(ref oFix).Range.Select();
                oWordDoc.Bookmarks.get_Item(ref oFix).Range.Font.Size = fontSize;

Open in new window

I also tried moving the new line of code around before the Range.Text, and I tried moving the Range.Text to the bottom. Still the same result.
0
 

Accepted Solution

by:
infotechelg earned 0 total points
ID: 39595549
I figured it out. What I ended up having to do is, in the Word Template, create new styles. Then, in my code, I did this:

                object oStyle = (fixtureName.Length <= 11) ? "Product16" :
                    (fixtureName.Length <= 13) ? "Product14" :
                    (fixtureName.Length <= 15) ? "Product12" :
                    (fixtureName.Length <= 17) ? "Product11" : "Product10";

                oWordDoc.Bookmarks.get_Item(ref oFix).Range.Text = fixtureName;
                oWordDoc.Bookmarks.get_Item(ref oFix).Range.set_Style(ref oStyle);
                oWordDoc.Bookmarks.get_Item(ref oFixType).Range.Text = fixtureType;

Open in new window

Where lines 1-4 are the style names I created. Then, in line 7, I used "set_Style" to apply that style to the text box in the Word document.
0
 

Author Closing Comment

by:infotechelg
ID: 39605121
I figured it out
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now