Solved

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

Posted on 2013-10-23
4
936 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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