1 Reply Latest reply on May 25, 2009 1:38 AM by ChamnanKh

    CS4 Script Find and Change in C#

    ChamnanKh

      Anybody here can show me me the sample script Find/Change with C#?.

      It's hard for newbie like me!

       

      Thanks in advance.

       

        • 1. Re: CS4 Script Find and Change in C#
          ChamnanKh Level 1

          Finally I found the way with Find/Change in C#:

           

          public FindReplace(string FindText, string ReplaceText)
                  {
                      try
                      {

                          private ApplicationClass inDesign= new ApplicationClass();
                          Document document = inDesign.ActiveDocument;
                          UpdateStatus("Finding text..", 0);
                          object mv = System.Reflection.Missing.Value;
                          inDesign.FindTextPreferences = idNothingEnum.idNothing;
                          inDesign.ChangeTextPreferences = idNothingEnum.idNothing;
                          ((FindTextPreference)inDesign.FindTextPreferences).AppliedParagraphStyle = INLINE_GRAPHIC_STYLE;
                          ((FindTextPreference)inDesign.FindTextPreferences).FindWhat = FindText;
                          ((ChangeTextPreference)inDesign.ChangeTextPreferences).ChangeTo = ReplaceText;
                          Objects foundItems = document.FindText(mv);
                          if (foundItems.Count > 0)
                          {
                              DialogResult dialogResult =
                                  MessageBox.Show(
                                      "The processing will be replace \"" + FindText + "\" to \"" + ReplaceText + "\" with " +
                                      foundItems.Count + " found.\n Are you sure whant to do a replacement?",
                                      "Find And Replace", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                      MessageBoxDefaultButton.Button2);
                              if (dialogResult == DialogResult.Yes)
                              {
                                  document.ChangeText(mv);
                                  MessageBox.Show("Replacement Completed.", "Find And Replace", MessageBoxButtons.OK,
                                                  MessageBoxIcon.Information);
                              }
                          }
                          else
                          {
                              MessageBox.Show("Finding \"" + FindText + "\" is no found.", "Find And Replace", MessageBoxButtons.OK,
                                                  MessageBoxIcon.Information);
                          }
                          inDesign.FindTextPreferences = idNothingEnum.idNothing;
                          inDesign.ChangeTextPreferences = idNothingEnum.idNothing;
                      }
                      catch (Exception exp)
                      {
                          throw new Exception(exp.Message);
                      }
                     
                  }