none
Set_style() is not setting up the style on text using interop C# RRS feed

  • Question

  • Hi guys,

    I have created one windows application in c#. Where i'm automating MsOffice.

    From the WindowsApplication i'm trying to set up the text styles(Heading1, heading2, heading3, Normal, .. etc) in the existing word document.

     public static bool ClearAllFormatting(IQuestion question, string filename, string text)
            {
                WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);
    
                try
                {
                    //Avoid screen flickering or unwanted alerts while initializing
                    wordApplication.ScreenUpdating = false;
                    WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
                    wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;
    
                    WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);
                    
                    WordInterop.Range range = wordDocument.Paragraphs[1].Range;
                    bool result = false;
                    if (range.Find.Execute(text))
                    {
                        //gets desired range here it gets last character to make superscript in range 
                        WordInterop.Range temprange = wordDocument.Range(range.Start, range.End);
                        temprange.Select();
                        string txt = temprange.Text;
                        object oCollapseEnd = WordInterop.WdCollapseDirection.wdCollapseEnd;
                        temprange.Collapse(oCollapseEnd);
                        object styleNormal = WordInterop.WdBuiltinStyle.wdStyleNormal;
                        temprange.set_Style(ref styleNormal);
                        //temprange.Find.set_Style(WordInterop.WdBuiltinStyle.wdStyleNormal);
                    }
    
                    wordDocument.Save();
                    object missing = Type.Missing;
    
                    object save_changes = false;
                    wordDocument.Close(ref save_changes, ref missing, ref missing);
                    return result;
                }
                catch (Exception)
                {
                    Cleanup(question.ObjectStore, true);
                }
                return false;
            }

    The above code is not setting up the style on specific text which is passing as parameter.

    Can anyone please help me. How to set_Style() on specific text.


    Thanks


    Sunday, May 14, 2017 6:38 AM

All replies

  • Hello,

    Line WordInterop.Range temprange = wordDocument.Range(range.Start, range.End); is actually getting the range same as line WordInterop.Range range = wordDocument.Paragraphs[1].Range; So the code above would set style for paragraph one.

    According to your code, do you want to find specific text in a paragraph and then superscript the last character of the found text?

    Please use

     if (range.Find.Execute(text))
                {
                    range.Characters.Last.Font.Superscript = 1;
                }
    
    
    Or use range.Characters.Last.Font.Superscript = (int)WdConstants.wdToggle; to toggle the superscript.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 16, 2017 8:31 AM
    Moderator
  • Thanks for your reply, but I don't want to superscript the last character, instead, I want to apply style(heading1, heading2, .. etc) on the find text.
    Wednesday, May 17, 2017 5:14 AM
  • Hello,

    You could use the following code to set the style of found text

                if (range.Find.Execute(test))
                {
                    range.Select();
                    object styleNormal = WdBuiltinStyle.wdStyleNormal;
                    Globals.ThisAddIn.Application.Selection.set_Style(ref styleNormal);
                }

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 18, 2017 9:31 AM
    Moderator