Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-10-23
4
Medium Priority
?
959 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

609 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