none
Insert conditional IF in word documents trough c# RRS feed

  • Question

  • Hi,

    I need to create a c# application that inserts a conditional if and show or hide an entire paragraph based on the evaluation result. 

    The conditional expression must have this form : <<database query result>> = 1 "Show paragraph" "Hide paragraph"

    I've been searching all over the internet and could not find a way on how to implement that.

    Is it possible to achieve that in c# ?

    Kind regards!

    Bogdan

    Monday, September 9, 2019 8:53 AM

Answers

  • Hi Bogdan-Sorin, 

    Thank you for posting here.

    For your question, you want to create a c# application that inserts a conditional if.

    I make a test on my side, and you can refer to the following code.

            public static void Main(string[] args)
            {
                MSWord.Application wordApp;
                MSWord.Document wordDoc;
                object path = @"your path";
    
                wordApp = new MSWord.ApplicationClass();
                wordApp.Visible = true;
                Object Nothing = Missing.Value;
                wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Text = "111";
                wordDoc.Paragraphs.Last.Range.Text += "222";
                wordDoc.Paragraphs.Last.Range.Text += "333";
                Console.WriteLine("Enter the number you want to hide which lines");
                int number = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter the yes/no you want to hide or show");
                string booljudge = Console.ReadLine();
                if (booljudge == "yes")
                {
                    HideText(wordDoc.Paragraphs[number].Range);
                }
                else if(booljudge == "no")
                {
                    UnhideText(wordDoc.Paragraphs[number].Range);
                }     
                object format = MSWord.WdSaveFormat.wdFormatDocument;
                wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, 
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, 
                    ref Nothing, ref Nothing);
                Console.WriteLine("success");
                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                Console.ReadKey();
            }
            static void HideText(MSWord.Range rng)
            {
                rng.Font.Hidden = 1; 
            }
            static void UnhideText(MSWord.Range rng)
            {
                rng.Font.Hidden = 0;  
            }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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, September 10, 2019 9:31 AM
    Moderator

All replies

  • Is it possible? Sure.

    But what's wrong with a VBA macro doing this?

    And: What's your use-case?

    Monday, September 9, 2019 9:03 AM
  • Hi Bogdan-Sorin, 

    Thank you for posting here.

    For your question, you want to create a c# application that inserts a conditional if.

    I make a test on my side, and you can refer to the following code.

            public static void Main(string[] args)
            {
                MSWord.Application wordApp;
                MSWord.Document wordDoc;
                object path = @"your path";
    
                wordApp = new MSWord.ApplicationClass();
                wordApp.Visible = true;
                Object Nothing = Missing.Value;
                wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                wordDoc.Paragraphs.Last.Range.Text = "111";
                wordDoc.Paragraphs.Last.Range.Text += "222";
                wordDoc.Paragraphs.Last.Range.Text += "333";
                Console.WriteLine("Enter the number you want to hide which lines");
                int number = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter the yes/no you want to hide or show");
                string booljudge = Console.ReadLine();
                if (booljudge == "yes")
                {
                    HideText(wordDoc.Paragraphs[number].Range);
                }
                else if(booljudge == "no")
                {
                    UnhideText(wordDoc.Paragraphs[number].Range);
                }     
                object format = MSWord.WdSaveFormat.wdFormatDocument;
                wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, 
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, 
                    ref Nothing, ref Nothing);
                Console.WriteLine("success");
                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                Console.ReadKey();
            }
            static void HideText(MSWord.Range rng)
            {
                rng.Font.Hidden = 1; 
            }
            static void UnhideText(MSWord.Range rng)
            {
                rng.Font.Hidden = 0;  
            }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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, September 10, 2019 9:31 AM
    Moderator