none
Executing condition in ms word RRS feed

  • Question

  • hi,

    plz give me the code in c# or vsto to execute condition like below.needs a class.

    Controls are content controls of word document

    "if(($Control1 > value)||($Control2<2)...)

    {

    //some actions here

    }

    else (some Condition like above)

    {

    actions here

    }

    else{actions here}"

    needs solution ASAP plz

    Wednesday, August 6, 2014 8:48 AM

Answers

  • Hi Anand,

    You could find the specified content control by its title or tag if you have set these properties.

    Here is a Word VSTO sample code for your reference:

    int value1 = 0, value2 = 0;
                foreach (Microsoft.Office.Interop.Word.ContentControl nativeControl in Globals.ThisAddIn.Application.ActiveDocument.ContentControls)
                {
                    if (nativeControl.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                    {
                        if (nativeControl.Title == "Control2")
                        {
                            value1 = Convert.ToInt32(nativeControl.Range.Text);
                        }
                        else if (nativeControl.Title == "Control3")
                        {
                            value2 = Convert.ToInt32(nativeControl.Range.Text);
                        }
                    }
                }
                if (value1 > 50 || value2 <= 100)
                {
                    //some actions here
                }
                else if (value1 <= 50)
                {
                    //some actions here
                }
                else
                {
                    //some actions here
                }
    Hope this helps.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 7, 2014 6:18 AM
    Moderator

All replies

  • Hi Anand,

    You could find the specified content control by its title or tag if you have set these properties.

    Here is a Word VSTO sample code for your reference:

    int value1 = 0, value2 = 0;
                foreach (Microsoft.Office.Interop.Word.ContentControl nativeControl in Globals.ThisAddIn.Application.ActiveDocument.ContentControls)
                {
                    if (nativeControl.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                    {
                        if (nativeControl.Title == "Control2")
                        {
                            value1 = Convert.ToInt32(nativeControl.Range.Text);
                        }
                        else if (nativeControl.Title == "Control3")
                        {
                            value2 = Convert.ToInt32(nativeControl.Range.Text);
                        }
                    }
                }
                if (value1 > 50 || value2 <= 100)
                {
                    //some actions here
                }
                else if (value1 <= 50)
                {
                    //some actions here
                }
                else
                {
                    //some actions here
                }
    Hope this helps.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, August 7, 2014 6:18 AM
    Moderator
  • hi George,

    i am working in office in interop now.i am having a rich text box on my design in which the conditions like i given above need to typed. and executed on button click.

    plz check wheather it is feasible through interop c#.if yes plz send me the snippets 

    Friday, August 8, 2014 6:33 AM
  • Hi Anand

    Yes, you can use C#. George's sample code is C# - did you try it?


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, August 10, 2014 3:38 PM
    Moderator
  • Hi Anand,

    If you are not developing a VSTO solution and you have referenced Microsoft.Office.Interop.Word, you only need to change:

    Globals.ThisAddIn.Application.ActiveDocument.ContentControls

    to:

    Word.Application wrdApp = new Word.Application();
    Word.Document doc = wrdApp.Documents.Open("file path");
    
    doc.ContentControls
    Hope this helps.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 11, 2014 2:58 AM
    Moderator