Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PowerShell: ForEach ($y in $x.licenses | where {....} 3 55
Query in AngularJS 8 47
Json and ajax 1 22
Starting to use Git with Visual  Studio Online 1 23
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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