?
Solved

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

Posted on 2013-10-23
4
Medium Priority
?
951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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