none
Apply text effects to the text in word using c# RRS feed

  • Question

  • Hi,

    I need to apply the Reflection and Glow effect to the selected text in word 2010 using c#. In VBA, we are able to access the properties in the font object, whereas, it is not available in VSTO. kindly help.

    Sunday, September 1, 2013 4:36 PM

All replies

  • Hi Vijayathii,

    According to your description, I wrote some sample for your reference.

    There are two ways to apply the Reflection effect to the selected text.

     
    private void button1_Click(object sender, EventArgs e)
    {
        //1.set the Type property, which includes settings for the preset Reflection types
        Globals.ThisAddIn.Application.ActiveWindow.Selection.Font.Reflection.Type = Microsoft.Office.Core.MsoReflectionType.msoReflectionType2;
        //2.set Reflection properties such as Offset, Blur to get custom effect.
        var reflection = Globals.ThisAddIn.Application.ActiveWindow.Selection.Font.Reflection;
        reflection.Offset = 2.4f;
        reflection.Blur = 0.5f;
        reflection.Size = 100;
        reflection.Transparency = 0.5f;
    }

    To apply the Glow effect to the selected text by setting the properties.

    private void button2_Click(object sender, EventArgs e)
    {
        var glow = Globals.ThisAddIn.Application.ActiveWindow.Selection.Font.Glow;
        glow.Color.ObjectThemeColor = Microsoft.Office.Interop.Word.WdThemeColorIndex.wdThemeColorAccent1;
        glow.Radius = 10;
        glow.Transparency = 0.8f;
    }


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 3, 2013 7:54 AM
    Moderator
  • Could you please show us the VBA code?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 3, 2013 9:05 AM
    Moderator
  • Hi Luna,

    Thanks for your help. I am able to access the Glow and Reflection property in the Ribbon class using Globals.ThisAddIn, where as I am not able to access these properties in the custom class library.

    public void SetFontStyle(object selectedRange)
            {
                Word.Selection currSelection = null;
                Word.Range currRange = null;

                currSelection = selectedRange as Word.Selection;
                if(currSelection == null)
                    return;

                currRange = currSelection.Range;          

                currRange.Font.Bold = 1;
                currRange.Font.Italic = 1;     
             
                currRange.Select();            
            }

    In the above code sample, I am able to do all the font related style except the glow, reflection and outline.

    Tuesday, September 3, 2013 4:43 PM
  • Hi Cindy,

    Here is the VBA code:

    ActiveWindow.Selection.Font.Reflection.Type = msoReflectionType2

    Tuesday, September 3, 2013 4:45 PM
  • <<where as I am not able to access these properties in the custom class library>>

    What do you mean by "custom class library"? We need to be able to repro your situation in order to help you...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 3, 2013 7:25 PM
    Moderator
  • Hi Cindy,

    Add the new Class Library project into the existing word addin project. keep the class as a singleton.

    using Word = Microsoft.Office.Interop.Word;

    Public class Helper

    {

    private Helper _instance;

    private Word.Application _wordApp;

    private

    private Helper()

    {

    }

    public Helper GetInstance()

    {

          if(_helper == null)

          _helper = new Helper();

          return _helper;

    }

    public Word.Application Application

    {

          get

          {

                return _wordApp;

          }

          set

          {

                _wordApp = Value;

          }

    }

           public void SetFontStyle()
            {
                Application.ActiveWindow.Selection.Font.Bold = 1;      
            }

    }

    }

    In the above code, we are creating the instance of this helper class in the ribbon class and assign the this.Application to Application property in order to access the word application in the helper class. We are able to apply the bold, italic and other formatting using the above method, except the Reflection, Glow and Outline. Hope it is clear.

    Thursday, September 5, 2013 11:25 AM
  • The code you've pasted has errors. Please provide useable code and please put it into a CODE box, rather than pasting it into your message (second button from the right in the message toolbar).

    Please also provide code that illustrates how to correctly use your class from within the add-in.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 5, 2013 12:50 PM
    Moderator
  • Hi Cindy,

    Sorry for the incorrect code. 1) Create a new Word 2010 Addin project with a name WordSolution and add the following code.

    ThisAddin.cs
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Word;

    using OfficeHelper;

    namespace WordSolution
    {
        public partial class ThisAddIn
        {
            private WordHelper wdHelper;

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                wdHelper = WordHelper.GetWordHelperInstance();
                wdHelper.Application = this.Application;
            }

            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }

            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
            {
                return new WordRibbon();
            }

            #region VSTO generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    2) Add the Ribbon xml with a name WordRibbon and add the following code.

    WordRibbon.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab id="wdRibbon" label="Test Ribbon">
            <group id="wdGroup" label="Test Group">
              <button id="btnTest" label="Set Font Style" onAction="RibbonCallback"/>
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>

    WordRibbon.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Text;
    using Office = Microsoft.Office.Core;

    using OfficeHelper;

    namespace WordSolution
    {
        [ComVisible(true)]
        public class WordRibbon : Office.IRibbonExtensibility
        {
            private Office.IRibbonUI ribbon;
            private WordHelper wdHelper;

            public WordRibbon()
            {
                wdHelper = WordHelper.GetWordHelperInstance();
            }

            #region IRibbonExtensibility Members

            public string GetCustomUI(string ribbonID)
            {
                return GetResourceText("WordSolution.WordRibbon.xml");
            }

            #endregion

            #region Ribbon Callbacks
            //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

            public void Ribbon_Load(Office.IRibbonUI ribbonUI)
            {
                this.ribbon = ribbonUI;
            }

            public void RibbonCallback(Office.IRibbonControl control)
            {
                switch (control.Id)
                {
                    case "btnTest":
                        wdHelper.SetFontStyle();
                        break;
                }
            }

            #endregion

            #region Helpers

            private static string GetResourceText(string resourceName)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                string[] resourceNames = asm.GetManifestResourceNames();
                for (int i = 0; i < resourceNames.Length; ++i)
                {
                    if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                        {
                            if (resourceReader != null)
                            {
                                return resourceReader.ReadToEnd();
                            }
                        }
                    }
                }
                return null;
            }

            #endregion
        }
    }

    3) Add the new class library project to the current solution with the name OfficeHelper. Add the new class with the name WordHelper.cs.

    WordHelper.cs using System;
    using System.IO;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;


    namespace OfficeHelper
    {
        public class WordHelper
        {
            private static WordHelper _wordHelper;
            private Word.Application _wordApplication;

            private WordHelper()
            {

            }

            public static WordHelper GetWordHelperInstance()
            {
                if (_wordHelper == null)
                {
                    _wordHelper = new WordHelper();
                }

                return _wordHelper;
            }

            public Word.Application Application
            {
                get
                {
                    if (_wordApplication == null)
                        _wordApplication = new Word.Application();

                    return _wordApplication;
                }

                set
                {
                    _wordApplication = value;
                }
            }

            public void SetFontStyle()
            {
                Application.ActiveWindow.Selection.Font.Bold = 1;

                //The following code will throw error

                /*
                Application.ActiveWindow.Selection.Font.Reflection.Type = Microsoft.Office.Core.MsoReflectionType.msoReflectionType2;
                Application.ActiveWindow.Selection.Font.Glow;
                Application.ActiveWindow.Selection.Font.Outline;
                 */
            }
        }
    }




    • Edited by Vijayathii Saturday, September 7, 2013 4:29 PM Error in code
    Thursday, September 5, 2013 4:25 PM
  • The code is still giving errors, before I even run it. _wordApplication, for example, is unknown (not declared anywhere).

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 6, 2013 3:26 PM
    Moderator
  • I have pasted the correct coce in the existing code block. Kindly use it.
    Saturday, September 7, 2013 4:30 PM
  • The reason you're code is not working is quite simple, and if you'd really looked at Luna's code you'd have seen it. You have to define properties, these aren't methods.

               Application.ActiveWindow.Selection.Font.Glow.Radius = 0.5f;
               Application.ActiveWindow.Selection.Font.Outline = -1;



    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 10, 2013 10:57 AM
    Moderator
  • Hi Cindy,

    I have changed the code as below, however, I am getting the following error

    Application.ActiveWindow.Selection.Font.Reflection.Type = Microsoft.Office.Core.MsoReflectionType.msoReflectionType2;

    Application.ActiveWindow.Selection.Font.Glow.Radius = 0.5f;

    Error 1 'Microsoft.Office.Interop.Word.Font' does not contain a definition for 'Reflection' and no extension method 'Reflection' accepting a first argument of type 'Microsoft.Office.Interop.Word.Font' could be found (are you missing a using directive or an assembly reference?) 

    Error 2 'Microsoft.Office.Interop.Word.Font' does not contain a definition for 'Glow' and no extension method 'Glow' accepting a first argument of type 'Microsoft.Office.Interop.Word.Font' could be found (are you missing a using directive or an assembly reference?)

    Tuesday, September 10, 2013 4:17 PM
  • Hi Vijayathii,

    I think you need to add Office reference to your project.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 11, 2013 1:43 AM
    Moderator
  • Hi Jeffrey,

    I have already added the Microsoft.Office.Interop.Word and Office in the reference the class library project. We are able to access the Reflection and Shadow properties only within the Office addin project and not in any another class library project.

    Wednesday, September 11, 2013 3:46 AM
  • Hi Vijayathii,

    Would you mind sharing your sample project with us through SkyDrive?



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, September 11, 2013 5:06 AM
    Moderator
  • It sounds almost as if the project is trying to use an older version of the Interop.Word libraries. Reflection and Glow weren't available to Word before 2007, as I recall.

    What I find odd is that only(?) the Helper class is having this problem.

    Which version of the .NET Framework is your Addin using? If it's 4.0, check whether "Embed Interop Types" is activated for the Word reference. If it is, try setting that to false and see if it makes any difference?

    I ran my test with the 3.5 Framework, so this problem wouldn't show up - if it turns out this is the issue.

    You also want to check the namespace of your Helper class: namespace OfficeHelper. Is this the same namespace used by the rest of your solution? If not, that could be an issue. My code all runs in the same Namespace.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 11, 2013 7:21 AM
    Moderator
  • Hi Jeffrey,

    I have uploaded  the sample solution in my SkyDrive. Please use the following link to download the solution.

    https://skydrive.live.com/embed?cid=FDD99F247F4AB7C3&resid=FDD99F247F4AB7C3%21116&authkey=ABQ3YoZGParS6to

    Wednesday, September 11, 2013 12:50 PM
  • Hi Cindy,

    I am using Office 2010 (14.0) Interop library, .Net Framework 4.0, Embed Interop Types is true. I have uploaded my solution in SkyDrive and use the following url to download the solution.

    https://skydrive.live.com/embed?cid=FDD99F247F4AB7C3&resid=FDD99F247F4AB7C3%21116&authkey=ABQ3YoZGParS6to

    Wednesday, September 11, 2013 12:53 PM
  • Hi Jeffrey,

    Have you checked the solution which I have uploaded into my SkyDrive. Sorry for the trouble.

    Friday, September 20, 2013 2:24 PM
  • Hi Cindy, Any update on these issue? Sorry for the trouble.
    Friday, September 20, 2013 2:26 PM